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.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
22 #include "../ring_generic.h"
27 int ad7606_reset(struct ad7606_state
*st
)
30 gpio_set_value(st
->pdata
->gpio_reset
, 1);
31 ndelay(100); /* t_reset >= 100ns */
32 gpio_set_value(st
->pdata
->gpio_reset
, 0);
39 static int ad7606_scan_direct(struct iio_dev
*indio_dev
, unsigned ch
)
41 struct ad7606_state
*st
= iio_priv(indio_dev
);
45 gpio_set_value(st
->pdata
->gpio_convst
, 1);
47 ret
= wait_event_interruptible(st
->wq_data_avail
, st
->done
);
51 if (st
->have_frstdata
) {
52 ret
= st
->bops
->read_block(st
->dev
, 1, st
->data
);
55 if (!gpio_get_value(st
->pdata
->gpio_frstdata
)) {
56 /* This should never happen */
61 ret
= st
->bops
->read_block(st
->dev
,
62 st
->chip_info
->num_channels
- 1, &st
->data
[1]);
66 ret
= st
->bops
->read_block(st
->dev
,
67 st
->chip_info
->num_channels
, st
->data
);
75 gpio_set_value(st
->pdata
->gpio_convst
, 0);
80 static int ad7606_read_raw(struct iio_dev
*indio_dev
,
81 struct iio_chan_spec
const *chan
,
87 struct ad7606_state
*st
= iio_priv(indio_dev
);
88 unsigned int scale_uv
;
92 mutex_lock(&indio_dev
->mlock
);
93 if (iio_ring_enabled(indio_dev
))
94 ret
= ad7606_scan_from_ring(indio_dev
, chan
->address
);
96 ret
= ad7606_scan_direct(indio_dev
, chan
->address
);
97 mutex_unlock(&indio_dev
->mlock
);
103 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
104 scale_uv
= (st
->range
* 1000 * 2)
105 >> st
->chip_info
->channels
[0].scan_type
.realbits
;
106 *val
= scale_uv
/ 1000;
107 *val2
= (scale_uv
% 1000) * 1000;
108 return IIO_VAL_INT_PLUS_MICRO
;
113 static ssize_t
ad7606_show_range(struct device
*dev
,
114 struct device_attribute
*attr
, char *buf
)
116 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
117 struct ad7606_state
*st
= iio_priv(indio_dev
);
119 return sprintf(buf
, "%u\n", st
->range
);
122 static ssize_t
ad7606_store_range(struct device
*dev
,
123 struct device_attribute
*attr
, const char *buf
, size_t count
)
125 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
126 struct ad7606_state
*st
= iio_priv(indio_dev
);
129 if (strict_strtoul(buf
, 10, &lval
))
131 if (!(lval
== 5000 || lval
== 10000)) {
132 dev_err(dev
, "range is not supported\n");
135 mutex_lock(&indio_dev
->mlock
);
136 gpio_set_value(st
->pdata
->gpio_range
, lval
== 10000);
138 mutex_unlock(&indio_dev
->mlock
);
143 static IIO_DEVICE_ATTR(range
, S_IRUGO
| S_IWUSR
, \
144 ad7606_show_range
, ad7606_store_range
, 0);
145 static IIO_CONST_ATTR(range_available
, "5000 10000");
147 static ssize_t
ad7606_show_oversampling_ratio(struct device
*dev
,
148 struct device_attribute
*attr
, char *buf
)
150 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
151 struct ad7606_state
*st
= iio_priv(indio_dev
);
153 return sprintf(buf
, "%u\n", st
->oversampling
);
156 static int ad7606_oversampling_get_index(unsigned val
)
158 unsigned char supported
[] = {0, 2, 4, 8, 16, 32, 64};
161 for (i
= 0; i
< ARRAY_SIZE(supported
); i
++)
162 if (val
== supported
[i
])
168 static ssize_t
ad7606_store_oversampling_ratio(struct device
*dev
,
169 struct device_attribute
*attr
, const char *buf
, size_t count
)
171 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
172 struct ad7606_state
*st
= iio_priv(indio_dev
);
176 if (strict_strtoul(buf
, 10, &lval
))
179 ret
= ad7606_oversampling_get_index(lval
);
181 dev_err(dev
, "oversampling %lu is not supported\n", lval
);
185 mutex_lock(&indio_dev
->mlock
);
186 gpio_set_value(st
->pdata
->gpio_os0
, (ret
>> 0) & 1);
187 gpio_set_value(st
->pdata
->gpio_os1
, (ret
>> 1) & 1);
188 gpio_set_value(st
->pdata
->gpio_os1
, (ret
>> 2) & 1);
189 st
->oversampling
= lval
;
190 mutex_unlock(&indio_dev
->mlock
);
195 static IIO_DEVICE_ATTR(oversampling_ratio
, S_IRUGO
| S_IWUSR
,
196 ad7606_show_oversampling_ratio
,
197 ad7606_store_oversampling_ratio
, 0);
198 static IIO_CONST_ATTR(oversampling_ratio_available
, "0 2 4 8 16 32 64");
200 static struct attribute
*ad7606_attributes
[] = {
201 &iio_dev_attr_range
.dev_attr
.attr
,
202 &iio_const_attr_range_available
.dev_attr
.attr
,
203 &iio_dev_attr_oversampling_ratio
.dev_attr
.attr
,
204 &iio_const_attr_oversampling_ratio_available
.dev_attr
.attr
,
208 static mode_t
ad7606_attr_is_visible(struct kobject
*kobj
,
209 struct attribute
*attr
, int n
)
211 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
212 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
213 struct ad7606_state
*st
= iio_priv(indio_dev
);
215 mode_t mode
= attr
->mode
;
218 (attr
== &iio_dev_attr_oversampling_ratio
.dev_attr
.attr
||
220 &iio_const_attr_oversampling_ratio_available
.dev_attr
.attr
))
222 else if (!st
->have_range
&&
223 (attr
== &iio_dev_attr_range
.dev_attr
.attr
||
224 attr
== &iio_const_attr_range_available
.dev_attr
.attr
))
230 static const struct attribute_group ad7606_attribute_group
= {
231 .attrs
= ad7606_attributes
,
232 .is_visible
= ad7606_attr_is_visible
,
235 static struct iio_chan_spec ad7606_8_channels
[] = {
236 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
237 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
238 0, 0, IIO_ST('s', 16, 16, 0), 0),
239 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
240 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
241 1, 1, IIO_ST('s', 16, 16, 0), 0),
242 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
243 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
244 2, 2, IIO_ST('s', 16, 16, 0), 0),
245 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
246 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
247 3, 3, IIO_ST('s', 16, 16, 0), 0),
248 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 4, 0,
249 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
250 4, 4, IIO_ST('s', 16, 16, 0), 0),
251 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 5, 0,
252 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
253 5, 5, IIO_ST('s', 16, 16, 0), 0),
254 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 6, 0,
255 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
256 6, 6, IIO_ST('s', 16, 16, 0), 0),
257 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 7, 0,
258 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
259 7, 7, IIO_ST('s', 16, 16, 0), 0),
260 IIO_CHAN_SOFT_TIMESTAMP(8),
263 static struct iio_chan_spec ad7606_6_channels
[] = {
264 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
265 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
266 0, 0, IIO_ST('s', 16, 16, 0), 0),
267 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
268 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
269 1, 1, IIO_ST('s', 16, 16, 0), 0),
270 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
271 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
272 2, 2, IIO_ST('s', 16, 16, 0), 0),
273 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
274 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
275 3, 3, IIO_ST('s', 16, 16, 0), 0),
276 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 4, 0,
277 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
278 4, 4, IIO_ST('s', 16, 16, 0), 0),
279 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 5, 0,
280 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
281 5, 5, IIO_ST('s', 16, 16, 0), 0),
282 IIO_CHAN_SOFT_TIMESTAMP(6),
285 static struct iio_chan_spec ad7606_4_channels
[] = {
286 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 0, 0,
287 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
288 0, 0, IIO_ST('s', 16, 16, 0), 0),
289 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 1, 0,
290 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
291 1, 1, IIO_ST('s', 16, 16, 0), 0),
292 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 2, 0,
293 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
294 2, 2, IIO_ST('s', 16, 16, 0), 0),
295 IIO_CHAN(IIO_IN
, 0, 1, 0, NULL
, 3, 0,
296 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
297 3, 3, IIO_ST('s', 16, 16, 0), 0),
298 IIO_CHAN_SOFT_TIMESTAMP(4),
301 static const struct ad7606_chip_info ad7606_chip_info_tbl
[] = {
303 * More devices added in future
308 .channels
= ad7606_8_channels
,
314 .channels
= ad7606_6_channels
,
320 .channels
= ad7606_4_channels
,
325 static int ad7606_request_gpios(struct ad7606_state
*st
)
327 struct gpio gpio_array
[3] = {
329 .gpio
= st
->pdata
->gpio_os0
,
330 .flags
= GPIOF_DIR_OUT
| ((st
->oversampling
& 1) ?
331 GPIOF_INIT_HIGH
: GPIOF_INIT_LOW
),
332 .label
= "AD7606_OS0",
335 .gpio
= st
->pdata
->gpio_os1
,
336 .flags
= GPIOF_DIR_OUT
| ((st
->oversampling
& 2) ?
337 GPIOF_INIT_HIGH
: GPIOF_INIT_LOW
),
338 .label
= "AD7606_OS1",
341 .gpio
= st
->pdata
->gpio_os2
,
342 .flags
= GPIOF_DIR_OUT
| ((st
->oversampling
& 4) ?
343 GPIOF_INIT_HIGH
: GPIOF_INIT_LOW
),
344 .label
= "AD7606_OS2",
349 ret
= gpio_request_one(st
->pdata
->gpio_convst
, GPIOF_OUT_INIT_LOW
,
352 dev_err(st
->dev
, "failed to request GPIO CONVST\n");
356 ret
= gpio_request_array(gpio_array
, ARRAY_SIZE(gpio_array
));
361 ret
= gpio_request_one(st
->pdata
->gpio_reset
, GPIOF_OUT_INIT_LOW
,
364 st
->have_reset
= true;
366 ret
= gpio_request_one(st
->pdata
->gpio_range
, GPIOF_DIR_OUT
|
367 ((st
->range
== 10000) ? GPIOF_INIT_HIGH
:
368 GPIOF_INIT_LOW
), "AD7606_RANGE");
370 st
->have_range
= true;
372 ret
= gpio_request_one(st
->pdata
->gpio_stby
, GPIOF_OUT_INIT_HIGH
,
375 st
->have_stby
= true;
377 if (gpio_is_valid(st
->pdata
->gpio_frstdata
)) {
378 ret
= gpio_request_one(st
->pdata
->gpio_frstdata
, GPIOF_IN
,
381 st
->have_frstdata
= true;
387 static void ad7606_free_gpios(struct ad7606_state
*st
)
390 gpio_free(st
->pdata
->gpio_range
);
393 gpio_free(st
->pdata
->gpio_stby
);
396 gpio_free(st
->pdata
->gpio_os0
);
397 gpio_free(st
->pdata
->gpio_os1
);
398 gpio_free(st
->pdata
->gpio_os2
);
402 gpio_free(st
->pdata
->gpio_reset
);
404 if (st
->have_frstdata
)
405 gpio_free(st
->pdata
->gpio_frstdata
);
407 gpio_free(st
->pdata
->gpio_convst
);
413 static irqreturn_t
ad7606_interrupt(int irq
, void *dev_id
)
415 struct iio_dev
*indio_dev
= dev_id
;
416 struct ad7606_state
*st
= iio_priv(indio_dev
);
418 if (iio_ring_enabled(indio_dev
)) {
419 if (!work_pending(&st
->poll_work
))
420 schedule_work(&st
->poll_work
);
423 wake_up_interruptible(&st
->wq_data_avail
);
429 static const struct iio_info ad7606_info
= {
430 .driver_module
= THIS_MODULE
,
431 .read_raw
= &ad7606_read_raw
,
432 .attrs
= &ad7606_attribute_group
,
435 struct iio_dev
*ad7606_probe(struct device
*dev
, int irq
,
436 void __iomem
*base_address
,
438 const struct ad7606_bus_ops
*bops
)
440 struct ad7606_platform_data
*pdata
= dev
->platform_data
;
441 struct ad7606_state
*st
;
442 int ret
, regdone
= 0;
443 struct iio_dev
*indio_dev
= iio_allocate_device(sizeof(*st
));
445 if (indio_dev
== NULL
) {
450 st
= iio_priv(indio_dev
);
456 st
->base_address
= base_address
;
457 st
->range
= pdata
->default_range
== 10000 ? 10000 : 5000;
459 ret
= ad7606_oversampling_get_index(pdata
->default_os
);
461 dev_warn(dev
, "oversampling %d is not supported\n",
463 st
->oversampling
= 0;
465 st
->oversampling
= pdata
->default_os
;
468 st
->reg
= regulator_get(dev
, "vcc");
469 if (!IS_ERR(st
->reg
)) {
470 ret
= regulator_enable(st
->reg
);
476 st
->chip_info
= &ad7606_chip_info_tbl
[id
];
478 indio_dev
->dev
.parent
= dev
;
479 indio_dev
->info
= &ad7606_info
;
480 indio_dev
->modes
= INDIO_DIRECT_MODE
;
481 indio_dev
->name
= st
->chip_info
->name
;
482 indio_dev
->channels
= st
->chip_info
->channels
;
483 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
485 init_waitqueue_head(&st
->wq_data_avail
);
487 ret
= ad7606_request_gpios(st
);
489 goto error_disable_reg
;
491 ret
= ad7606_reset(st
);
493 dev_warn(st
->dev
, "failed to RESET: no RESET GPIO specified\n");
495 ret
= request_irq(st
->irq
, ad7606_interrupt
,
496 IRQF_TRIGGER_FALLING
, st
->chip_info
->name
, indio_dev
);
498 goto error_free_gpios
;
500 ret
= ad7606_register_ring_funcs_and_init(indio_dev
);
504 ret
= iio_device_register(indio_dev
);
509 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
511 indio_dev
->num_channels
);
513 goto error_cleanup_ring
;
518 ad7606_ring_cleanup(indio_dev
);
521 free_irq(st
->irq
, indio_dev
);
524 ad7606_free_gpios(st
);
527 if (!IS_ERR(st
->reg
))
528 regulator_disable(st
->reg
);
530 if (!IS_ERR(st
->reg
))
531 regulator_put(st
->reg
);
533 iio_device_unregister(indio_dev
);
535 iio_free_device(indio_dev
);
540 int ad7606_remove(struct iio_dev
*indio_dev
)
542 struct ad7606_state
*st
= iio_priv(indio_dev
);
544 iio_ring_buffer_unregister(indio_dev
->ring
);
545 ad7606_ring_cleanup(indio_dev
);
547 free_irq(st
->irq
, indio_dev
);
548 if (!IS_ERR(st
->reg
)) {
549 regulator_disable(st
->reg
);
550 regulator_put(st
->reg
);
553 ad7606_free_gpios(st
);
554 iio_device_unregister(indio_dev
);
559 void ad7606_suspend(struct iio_dev
*indio_dev
)
561 struct ad7606_state
*st
= iio_priv(indio_dev
);
565 gpio_set_value(st
->pdata
->gpio_range
, 1);
566 gpio_set_value(st
->pdata
->gpio_stby
, 0);
570 void ad7606_resume(struct iio_dev
*indio_dev
)
572 struct ad7606_state
*st
= iio_priv(indio_dev
);
576 gpio_set_value(st
->pdata
->gpio_range
,
579 gpio_set_value(st
->pdata
->gpio_stby
, 1);
584 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
585 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
586 MODULE_LICENSE("GPL v2");