2 * AD7792/AD7793 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/spi/spi.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/delay.h>
22 #include "../ring_generic.h"
23 #include "../ring_sw.h"
24 #include "../trigger.h"
30 * The AD7792/AD7793 features a dual use data out ready DOUT/RDY output.
31 * In order to avoid contentions on the SPI bus, it's therefore necessary
32 * to use spi bus locking.
34 * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
37 struct ad7793_chip_info
{
38 struct iio_chan_spec channel
[7];
42 struct spi_device
*spi
;
43 struct iio_trigger
*trig
;
44 const struct ad7793_chip_info
*chip_info
;
45 struct regulator
*reg
;
46 struct ad7793_platform_data
*pdata
;
47 wait_queue_head_t wq_data_avail
;
53 u32 scale_avail
[8][2];
54 u32 available_scan_masks
[7];
56 * DMA (thus cache coherency maintenance) requires the
57 * transfer buffers to live in their own cache lines.
59 u8 data
[4] ____cacheline_aligned
;
62 enum ad7793_supported_device_ids
{
67 static int __ad7793_write_reg(struct ad7793_state
*st
, bool locked
,
68 bool cs_change
, unsigned char reg
,
69 unsigned size
, unsigned val
)
72 struct spi_transfer t
= {
75 .cs_change
= cs_change
,
79 data
[0] = AD7793_COMM_WRITE
| AD7793_COMM_ADDR(reg
);
99 spi_message_add_tail(&t
, &m
);
102 return spi_sync_locked(st
->spi
, &m
);
104 return spi_sync(st
->spi
, &m
);
107 static int ad7793_write_reg(struct ad7793_state
*st
,
108 unsigned reg
, unsigned size
, unsigned val
)
110 return __ad7793_write_reg(st
, false, false, reg
, size
, val
);
113 static int __ad7793_read_reg(struct ad7793_state
*st
, bool locked
,
114 bool cs_change
, unsigned char reg
,
115 int *val
, unsigned size
)
119 struct spi_transfer t
[] = {
126 .cs_change
= cs_change
,
129 struct spi_message m
;
131 data
[0] = AD7793_COMM_READ
| AD7793_COMM_ADDR(reg
);
133 spi_message_init(&m
);
134 spi_message_add_tail(&t
[0], &m
);
135 spi_message_add_tail(&t
[1], &m
);
138 ret
= spi_sync_locked(st
->spi
, &m
);
140 ret
= spi_sync(st
->spi
, &m
);
147 *val
= data
[0] << 16 | data
[1] << 8 | data
[2];
150 *val
= data
[0] << 8 | data
[1];
162 static int ad7793_read_reg(struct ad7793_state
*st
,
163 unsigned reg
, int *val
, unsigned size
)
165 return __ad7793_read_reg(st
, 0, 0, reg
, val
, size
);
168 static int ad7793_read(struct ad7793_state
*st
, unsigned ch
,
169 unsigned len
, int *val
)
172 st
->conf
= (st
->conf
& ~AD7793_CONF_CHAN(-1)) | AD7793_CONF_CHAN(ch
);
173 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) |
174 AD7793_MODE_SEL(AD7793_MODE_SINGLE
);
176 ad7793_write_reg(st
, AD7793_REG_CONF
, sizeof(st
->conf
), st
->conf
);
178 spi_bus_lock(st
->spi
->master
);
181 ret
= __ad7793_write_reg(st
, 1, 1, AD7793_REG_MODE
,
182 sizeof(st
->mode
), st
->mode
);
187 enable_irq(st
->spi
->irq
);
188 wait_event_interruptible(st
->wq_data_avail
, st
->done
);
190 ret
= __ad7793_read_reg(st
, 1, 0, AD7793_REG_DATA
, val
, len
);
192 spi_bus_unlock(st
->spi
->master
);
197 static int ad7793_calibrate(struct ad7793_state
*st
, unsigned mode
, unsigned ch
)
201 st
->conf
= (st
->conf
& ~AD7793_CONF_CHAN(-1)) | AD7793_CONF_CHAN(ch
);
202 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) | AD7793_MODE_SEL(mode
);
204 ad7793_write_reg(st
, AD7793_REG_CONF
, sizeof(st
->conf
), st
->conf
);
206 spi_bus_lock(st
->spi
->master
);
209 ret
= __ad7793_write_reg(st
, 1, 1, AD7793_REG_MODE
,
210 sizeof(st
->mode
), st
->mode
);
215 enable_irq(st
->spi
->irq
);
216 wait_event_interruptible(st
->wq_data_avail
, st
->done
);
218 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) |
219 AD7793_MODE_SEL(AD7793_MODE_IDLE
);
221 ret
= __ad7793_write_reg(st
, 1, 0, AD7793_REG_MODE
,
222 sizeof(st
->mode
), st
->mode
);
224 spi_bus_unlock(st
->spi
->master
);
229 static const u8 ad7793_calib_arr
[6][2] = {
230 {AD7793_MODE_CAL_INT_ZERO
, AD7793_CH_AIN1P_AIN1M
},
231 {AD7793_MODE_CAL_INT_FULL
, AD7793_CH_AIN1P_AIN1M
},
232 {AD7793_MODE_CAL_INT_ZERO
, AD7793_CH_AIN2P_AIN2M
},
233 {AD7793_MODE_CAL_INT_FULL
, AD7793_CH_AIN2P_AIN2M
},
234 {AD7793_MODE_CAL_INT_ZERO
, AD7793_CH_AIN3P_AIN3M
},
235 {AD7793_MODE_CAL_INT_FULL
, AD7793_CH_AIN3P_AIN3M
}
238 static int ad7793_calibrate_all(struct ad7793_state
*st
)
242 for (i
= 0; i
< ARRAY_SIZE(ad7793_calib_arr
); i
++) {
243 ret
= ad7793_calibrate(st
, ad7793_calib_arr
[i
][0],
244 ad7793_calib_arr
[i
][1]);
251 dev_err(&st
->spi
->dev
, "Calibration failed\n");
255 static int ad7793_setup(struct ad7793_state
*st
)
258 unsigned long long scale_uv
;
261 /* reset the serial interface */
262 ret
= spi_write(st
->spi
, (u8
*)&ret
, sizeof(ret
));
265 msleep(1); /* Wait for at least 500us */
267 /* write/read test for device presence */
268 ret
= ad7793_read_reg(st
, AD7793_REG_ID
, &id
, 1);
272 id
&= AD7793_ID_MASK
;
274 if (!((id
== AD7792_ID
) || (id
== AD7793_ID
))) {
275 dev_err(&st
->spi
->dev
, "device ID query failed\n");
279 st
->mode
= (st
->pdata
->mode
& ~AD7793_MODE_SEL(-1)) |
280 AD7793_MODE_SEL(AD7793_MODE_IDLE
);
281 st
->conf
= st
->pdata
->conf
& ~AD7793_CONF_CHAN(-1);
283 ret
= ad7793_write_reg(st
, AD7793_REG_MODE
, sizeof(st
->mode
), st
->mode
);
287 ret
= ad7793_write_reg(st
, AD7793_REG_CONF
, sizeof(st
->conf
), st
->conf
);
291 ret
= ad7793_write_reg(st
, AD7793_REG_IO
,
292 sizeof(st
->pdata
->io
), st
->pdata
->io
);
296 ret
= ad7793_calibrate_all(st
);
300 /* Populate available ADC input ranges */
301 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++) {
302 scale_uv
= ((u64
)st
->int_vref_mv
* 100000000)
303 >> (st
->chip_info
->channel
[0].scan_type
.realbits
-
304 (!!(st
->conf
& AD7793_CONF_UNIPOLAR
) ? 0 : 1));
307 st
->scale_avail
[i
][1] = do_div(scale_uv
, 100000000) * 10;
308 st
->scale_avail
[i
][0] = scale_uv
;
313 dev_err(&st
->spi
->dev
, "setup failed\n");
317 static int ad7793_scan_from_ring(struct ad7793_state
*st
, unsigned ch
, int *val
)
319 struct iio_ring_buffer
*ring
= iio_priv_to_dev(st
)->ring
;
322 u32
*dat32
= (u32
*)dat64
;
324 if (!(ring
->scan_mask
& (1 << ch
)))
327 ret
= ring
->access
->read_last(ring
, (u8
*) &dat64
);
336 static int ad7793_ring_preenable(struct iio_dev
*indio_dev
)
338 struct ad7793_state
*st
= iio_priv(indio_dev
);
339 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
343 if (!ring
->scan_count
)
346 channel
= __ffs(ring
->scan_mask
);
348 d_size
= ring
->scan_count
*
349 indio_dev
->channels
[0].scan_type
.storagebits
/ 8;
351 if (ring
->scan_timestamp
) {
352 d_size
+= sizeof(s64
);
354 if (d_size
% sizeof(s64
))
355 d_size
+= sizeof(s64
) - (d_size
% sizeof(s64
));
358 if (indio_dev
->ring
->access
->set_bytes_per_datum
)
359 indio_dev
->ring
->access
->set_bytes_per_datum(indio_dev
->ring
,
362 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) |
363 AD7793_MODE_SEL(AD7793_MODE_CONT
);
364 st
->conf
= (st
->conf
& ~AD7793_CONF_CHAN(-1)) |
365 AD7793_CONF_CHAN(indio_dev
->channels
[channel
].address
);
367 ad7793_write_reg(st
, AD7793_REG_CONF
, sizeof(st
->conf
), st
->conf
);
369 spi_bus_lock(st
->spi
->master
);
370 __ad7793_write_reg(st
, 1, 1, AD7793_REG_MODE
,
371 sizeof(st
->mode
), st
->mode
);
374 enable_irq(st
->spi
->irq
);
379 static int ad7793_ring_postdisable(struct iio_dev
*indio_dev
)
381 struct ad7793_state
*st
= iio_priv(indio_dev
);
383 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) |
384 AD7793_MODE_SEL(AD7793_MODE_IDLE
);
387 wait_event_interruptible(st
->wq_data_avail
, st
->done
);
390 disable_irq_nosync(st
->spi
->irq
);
392 __ad7793_write_reg(st
, 1, 0, AD7793_REG_MODE
,
393 sizeof(st
->mode
), st
->mode
);
395 return spi_bus_unlock(st
->spi
->master
);
399 * ad7793_trigger_handler() bh of trigger launched polling to ring buffer
402 static irqreturn_t
ad7793_trigger_handler(int irq
, void *p
)
404 struct iio_poll_func
*pf
= p
;
405 struct iio_dev
*indio_dev
= pf
->private_data
;
406 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
407 struct ad7793_state
*st
= iio_priv(indio_dev
);
409 s32
*dat32
= (s32
*)dat64
;
411 if (ring
->scan_count
)
412 __ad7793_read_reg(st
, 1, 1, AD7793_REG_DATA
,
414 indio_dev
->channels
[0].scan_type
.realbits
/8);
416 /* Guaranteed to be aligned with 8 byte boundary */
417 if (ring
->scan_timestamp
)
418 dat64
[1] = pf
->timestamp
;
420 ring
->access
->store_to(ring
, (u8
*)dat64
, pf
->timestamp
);
422 iio_trigger_notify_done(indio_dev
->trig
);
424 enable_irq(st
->spi
->irq
);
429 static const struct iio_ring_setup_ops ad7793_ring_setup_ops
= {
430 .preenable
= &ad7793_ring_preenable
,
431 .postenable
= &iio_triggered_ring_postenable
,
432 .predisable
= &iio_triggered_ring_predisable
,
433 .postdisable
= &ad7793_ring_postdisable
,
436 static int ad7793_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
440 indio_dev
->ring
= iio_sw_rb_allocate(indio_dev
);
441 if (!indio_dev
->ring
) {
445 /* Effectively select the ring buffer implementation */
446 indio_dev
->ring
->access
= &ring_sw_access_funcs
;
447 indio_dev
->pollfunc
= iio_alloc_pollfunc(&iio_pollfunc_store_time
,
448 &ad7793_trigger_handler
,
453 if (indio_dev
->pollfunc
== NULL
) {
455 goto error_deallocate_sw_rb
;
458 /* Ring buffer functions - here trigger setup related */
459 indio_dev
->ring
->setup_ops
= &ad7793_ring_setup_ops
;
461 /* Flag that polled ring buffering is possible */
462 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
465 error_deallocate_sw_rb
:
466 iio_sw_rb_free(indio_dev
->ring
);
471 static void ad7793_ring_cleanup(struct iio_dev
*indio_dev
)
473 /* ensure that the trigger has been detached */
474 if (indio_dev
->trig
) {
475 iio_put_trigger(indio_dev
->trig
);
476 iio_trigger_dettach_poll_func(indio_dev
->trig
,
477 indio_dev
->pollfunc
);
479 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
480 iio_sw_rb_free(indio_dev
->ring
);
484 * ad7793_data_rdy_trig_poll() the event handler for the data rdy trig
486 static irqreturn_t
ad7793_data_rdy_trig_poll(int irq
, void *private)
488 struct ad7793_state
*st
= iio_priv(private);
491 wake_up_interruptible(&st
->wq_data_avail
);
492 disable_irq_nosync(irq
);
494 iio_trigger_poll(st
->trig
, iio_get_time_ns());
499 static int ad7793_probe_trigger(struct iio_dev
*indio_dev
)
501 struct ad7793_state
*st
= iio_priv(indio_dev
);
504 st
->trig
= iio_allocate_trigger("%s-dev%d",
505 spi_get_device_id(st
->spi
)->name
,
507 if (st
->trig
== NULL
) {
512 ret
= request_irq(st
->spi
->irq
,
513 ad7793_data_rdy_trig_poll
,
515 spi_get_device_id(st
->spi
)->name
,
518 goto error_free_trig
;
520 disable_irq_nosync(st
->spi
->irq
);
522 st
->trig
->dev
.parent
= &st
->spi
->dev
;
523 st
->trig
->owner
= THIS_MODULE
;
524 st
->trig
->private_data
= indio_dev
;
526 ret
= iio_trigger_register(st
->trig
);
528 /* select default trigger */
529 indio_dev
->trig
= st
->trig
;
536 free_irq(st
->spi
->irq
, indio_dev
);
538 iio_free_trigger(st
->trig
);
543 static void ad7793_remove_trigger(struct iio_dev
*indio_dev
)
545 struct ad7793_state
*st
= iio_priv(indio_dev
);
547 iio_trigger_unregister(st
->trig
);
548 free_irq(st
->spi
->irq
, indio_dev
);
549 iio_free_trigger(st
->trig
);
552 static const u16 sample_freq_avail
[16] = {0, 470, 242, 123, 62, 50, 39, 33, 19,
553 17, 16, 12, 10, 8, 6, 4};
555 static ssize_t
ad7793_read_frequency(struct device
*dev
,
556 struct device_attribute
*attr
,
559 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
560 struct ad7793_state
*st
= iio_priv(indio_dev
);
562 return sprintf(buf
, "%d\n",
563 sample_freq_avail
[AD7793_MODE_RATE(st
->mode
)]);
566 static ssize_t
ad7793_write_frequency(struct device
*dev
,
567 struct device_attribute
*attr
,
571 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
572 struct ad7793_state
*st
= iio_priv(indio_dev
);
576 mutex_lock(&indio_dev
->mlock
);
577 if (iio_ring_enabled(indio_dev
)) {
578 mutex_unlock(&indio_dev
->mlock
);
581 mutex_unlock(&indio_dev
->mlock
);
583 ret
= strict_strtol(buf
, 10, &lval
);
589 for (i
= 0; i
< ARRAY_SIZE(sample_freq_avail
); i
++)
590 if (lval
== sample_freq_avail
[i
]) {
591 mutex_lock(&indio_dev
->mlock
);
592 st
->mode
&= ~AD7793_MODE_RATE(-1);
593 st
->mode
|= AD7793_MODE_RATE(i
);
594 ad7793_write_reg(st
, AD7793_REG_MODE
,
595 sizeof(st
->mode
), st
->mode
);
596 mutex_unlock(&indio_dev
->mlock
);
600 return ret
? ret
: len
;
603 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
604 ad7793_read_frequency
,
605 ad7793_write_frequency
);
607 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
608 "470 242 123 62 50 39 33 19 17 16 12 10 8 6 4");
610 static ssize_t
ad7793_show_scale_available(struct device
*dev
,
611 struct device_attribute
*attr
, char *buf
)
613 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
614 struct ad7793_state
*st
= iio_priv(indio_dev
);
617 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
618 len
+= sprintf(buf
+ len
, "%d.%09u ", st
->scale_avail
[i
][0],
619 st
->scale_avail
[i
][1]);
621 len
+= sprintf(buf
+ len
, "\n");
626 static IIO_DEVICE_ATTR_NAMED(in_m_in_scale_available
, in
-in_scale_available
,
627 S_IRUGO
, ad7793_show_scale_available
, NULL
, 0);
629 static struct attribute
*ad7793_attributes
[] = {
630 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
631 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
632 &iio_dev_attr_in_m_in_scale_available
.dev_attr
.attr
,
636 static const struct attribute_group ad7793_attribute_group
= {
637 .attrs
= ad7793_attributes
,
640 static int ad7793_read_raw(struct iio_dev
*indio_dev
,
641 struct iio_chan_spec
const *chan
,
646 struct ad7793_state
*st
= iio_priv(indio_dev
);
648 unsigned long long scale_uv
;
649 bool unipolar
= !!(st
->conf
& AD7793_CONF_UNIPOLAR
);
653 mutex_lock(&indio_dev
->mlock
);
654 if (iio_ring_enabled(indio_dev
))
655 ret
= ad7793_scan_from_ring(st
,
656 chan
->scan_index
, &smpl
);
658 ret
= ad7793_read(st
, chan
->address
,
659 chan
->scan_type
.realbits
/ 8, &smpl
);
660 mutex_unlock(&indio_dev
->mlock
);
665 *val
= (smpl
>> chan
->scan_type
.shift
) &
666 ((1 << (chan
->scan_type
.realbits
)) - 1);
669 *val
-= (1 << (chan
->scan_type
.realbits
- 1));
673 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
674 *val
= st
->scale_avail
[(st
->conf
>> 8) & 0x7][0];
675 *val2
= st
->scale_avail
[(st
->conf
>> 8) & 0x7][1];
677 return IIO_VAL_INT_PLUS_NANO
;
679 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
680 switch (chan
->type
) {
682 /* 1170mV / 2^23 * 6 */
683 scale_uv
= (1170ULL * 100000000ULL * 6ULL)
684 >> (chan
->scan_type
.realbits
-
688 /* Always uses unity gain and internal ref */
689 scale_uv
= (2500ULL * 100000000ULL)
690 >> (chan
->scan_type
.realbits
-
697 *val2
= do_div(scale_uv
, 100000000) * 10;
700 return IIO_VAL_INT_PLUS_NANO
;
705 static int ad7793_write_raw(struct iio_dev
*indio_dev
,
706 struct iio_chan_spec
const *chan
,
711 struct ad7793_state
*st
= iio_priv(indio_dev
);
715 mutex_lock(&indio_dev
->mlock
);
716 if (iio_ring_enabled(indio_dev
)) {
717 mutex_unlock(&indio_dev
->mlock
);
722 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
724 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
725 if (val2
== st
->scale_avail
[i
][1]) {
727 st
->conf
&= ~AD7793_CONF_GAIN(-1);
728 st
->conf
|= AD7793_CONF_GAIN(i
);
730 if (tmp
!= st
->conf
) {
731 ad7793_write_reg(st
, AD7793_REG_CONF
,
734 ad7793_calibrate_all(st
);
743 mutex_unlock(&indio_dev
->mlock
);
747 static int ad7793_validate_trigger(struct iio_dev
*indio_dev
,
748 struct iio_trigger
*trig
)
750 if (indio_dev
->trig
!= trig
)
756 static int ad7793_write_raw_get_fmt(struct iio_dev
*indio_dev
,
757 struct iio_chan_spec
const *chan
,
760 return IIO_VAL_INT_PLUS_NANO
;
763 static const struct iio_info ad7793_info
= {
764 .read_raw
= &ad7793_read_raw
,
765 .write_raw
= &ad7793_write_raw
,
766 .write_raw_get_fmt
= &ad7793_write_raw_get_fmt
,
767 .attrs
= &ad7793_attribute_group
,
768 .validate_trigger
= ad7793_validate_trigger
,
769 .driver_module
= THIS_MODULE
,
772 static const struct ad7793_chip_info ad7793_chip_info_tbl
[] = {
774 .channel
[0] = IIO_CHAN(IIO_IN_DIFF
, 0, 1, 0, NULL
, 0, 0,
775 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
776 AD7793_CH_AIN1P_AIN1M
,
777 0, IIO_ST('s', 24, 32, 0), 0),
778 .channel
[1] = IIO_CHAN(IIO_IN_DIFF
, 0, 1, 0, NULL
, 1, 1,
779 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
780 AD7793_CH_AIN2P_AIN2M
,
781 1, IIO_ST('s', 24, 32, 0), 0),
782 .channel
[2] = IIO_CHAN(IIO_IN_DIFF
, 0, 1, 0, NULL
, 2, 2,
783 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
784 AD7793_CH_AIN3P_AIN3M
,
785 2, IIO_ST('s', 24, 32, 0), 0),
786 .channel
[3] = IIO_CHAN(IIO_IN_DIFF
, 0, 1, 0, "shorted", 0, 0,
787 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
788 AD7793_CH_AIN1M_AIN1M
,
789 3, IIO_ST('s', 24, 32, 0), 0),
790 .channel
[4] = IIO_CHAN(IIO_TEMP
, 0, 1, 0, NULL
, 0, 0,
791 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
793 4, IIO_ST('s', 24, 32, 0), 0),
794 .channel
[5] = IIO_CHAN(IIO_IN
, 0, 1, 0, "supply", 4, 0,
795 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
796 AD7793_CH_AVDD_MONITOR
,
797 5, IIO_ST('s', 24, 32, 0), 0),
798 .channel
[6] = IIO_CHAN_SOFT_TIMESTAMP(6),
801 .channel
[0] = IIO_CHAN(IIO_IN_DIFF
, 0, 1, 0, NULL
, 0, 0,
802 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
803 AD7793_CH_AIN1P_AIN1M
,
804 0, IIO_ST('s', 16, 32, 0), 0),
805 .channel
[1] = IIO_CHAN(IIO_IN_DIFF
, 0, 1, 0, NULL
, 1, 1,
806 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
807 AD7793_CH_AIN2P_AIN2M
,
808 1, IIO_ST('s', 16, 32, 0), 0),
809 .channel
[2] = IIO_CHAN(IIO_IN_DIFF
, 0, 1, 0, NULL
, 2, 2,
810 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
811 AD7793_CH_AIN3P_AIN3M
,
812 2, IIO_ST('s', 16, 32, 0), 0),
813 .channel
[3] = IIO_CHAN(IIO_IN_DIFF
, 0, 1, 0, "shorted", 0, 0,
814 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
815 AD7793_CH_AIN1M_AIN1M
,
816 3, IIO_ST('s', 16, 32, 0), 0),
817 .channel
[4] = IIO_CHAN(IIO_TEMP
, 0, 1, 0, NULL
, 0, 0,
818 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
820 4, IIO_ST('s', 16, 32, 0), 0),
821 .channel
[5] = IIO_CHAN(IIO_IN
, 0, 1, 0, "supply", 4, 0,
822 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
823 AD7793_CH_AVDD_MONITOR
,
824 5, IIO_ST('s', 16, 32, 0), 0),
825 .channel
[6] = IIO_CHAN_SOFT_TIMESTAMP(6),
829 static int __devinit
ad7793_probe(struct spi_device
*spi
)
831 struct ad7793_platform_data
*pdata
= spi
->dev
.platform_data
;
832 struct ad7793_state
*st
;
833 struct iio_dev
*indio_dev
;
834 int ret
, i
, voltage_uv
= 0, regdone
= 0;
837 dev_err(&spi
->dev
, "no platform data?\n");
842 dev_err(&spi
->dev
, "no IRQ?\n");
846 indio_dev
= iio_allocate_device(sizeof(*st
));
847 if (indio_dev
== NULL
)
850 st
= iio_priv(indio_dev
);
852 st
->reg
= regulator_get(&spi
->dev
, "vcc");
853 if (!IS_ERR(st
->reg
)) {
854 ret
= regulator_enable(st
->reg
);
858 voltage_uv
= regulator_get_voltage(st
->reg
);
862 &ad7793_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
866 if (pdata
&& pdata
->vref_mv
)
867 st
->int_vref_mv
= pdata
->vref_mv
;
869 st
->int_vref_mv
= voltage_uv
/ 1000;
871 st
->int_vref_mv
= 2500; /* Build-in ref */
873 spi_set_drvdata(spi
, indio_dev
);
876 indio_dev
->dev
.parent
= &spi
->dev
;
877 indio_dev
->name
= spi_get_device_id(spi
)->name
;
878 indio_dev
->modes
= INDIO_DIRECT_MODE
;
879 indio_dev
->channels
= st
->chip_info
->channel
;
880 indio_dev
->available_scan_masks
= st
->available_scan_masks
;
881 indio_dev
->num_channels
= 7;
882 indio_dev
->info
= &ad7793_info
;
884 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
885 st
->available_scan_masks
[i
] = (1 << i
) | (1 <<
886 indio_dev
->channels
[indio_dev
->num_channels
- 1].
889 init_waitqueue_head(&st
->wq_data_avail
);
891 ret
= ad7793_register_ring_funcs_and_init(indio_dev
);
893 goto error_disable_reg
;
895 ret
= iio_device_register(indio_dev
);
897 goto error_unreg_ring
;
900 ret
= ad7793_probe_trigger(indio_dev
);
902 goto error_unreg_ring
;
904 ret
= iio_ring_buffer_register_ex(indio_dev
->ring
, 0,
906 indio_dev
->num_channels
);
908 goto error_remove_trigger
;
910 ret
= ad7793_setup(st
);
912 goto error_uninitialize_ring
;
916 error_uninitialize_ring
:
917 iio_ring_buffer_unregister(indio_dev
->ring
);
918 error_remove_trigger
:
919 ad7793_remove_trigger(indio_dev
);
921 ad7793_ring_cleanup(indio_dev
);
923 if (!IS_ERR(st
->reg
))
924 regulator_disable(st
->reg
);
926 if (!IS_ERR(st
->reg
))
927 regulator_put(st
->reg
);
930 iio_device_unregister(indio_dev
);
932 iio_free_device(indio_dev
);
937 static int ad7793_remove(struct spi_device
*spi
)
939 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
940 struct ad7793_state
*st
= iio_priv(indio_dev
);
942 iio_ring_buffer_unregister(indio_dev
->ring
);
943 ad7793_remove_trigger(indio_dev
);
944 ad7793_ring_cleanup(indio_dev
);
946 if (!IS_ERR(st
->reg
)) {
947 regulator_disable(st
->reg
);
948 regulator_put(st
->reg
);
951 iio_device_unregister(indio_dev
);
956 static const struct spi_device_id ad7793_id
[] = {
957 {"ad7792", ID_AD7792
},
958 {"ad7793", ID_AD7793
},
962 static struct spi_driver ad7793_driver
= {
965 .bus
= &spi_bus_type
,
966 .owner
= THIS_MODULE
,
968 .probe
= ad7793_probe
,
969 .remove
= __devexit_p(ad7793_remove
),
970 .id_table
= ad7793_id
,
973 static int __init
ad7793_init(void)
975 return spi_register_driver(&ad7793_driver
);
977 module_init(ad7793_init
);
979 static void __exit
ad7793_exit(void)
981 spi_unregister_driver(&ad7793_driver
);
983 module_exit(ad7793_exit
);
985 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
986 MODULE_DESCRIPTION("Analog Devices AD7792/3 ADC");
987 MODULE_LICENSE("GPL v2");