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>
19 #include <linux/module.h>
23 #include "../buffer.h"
24 #include "../ring_sw.h"
25 #include "../trigger.h"
26 #include "../trigger_consumer.h"
31 * The AD7792/AD7793 features a dual use data out ready DOUT/RDY output.
32 * In order to avoid contentions on the SPI bus, it's therefore necessary
33 * to use spi bus locking.
35 * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
38 struct ad7793_chip_info
{
39 struct iio_chan_spec channel
[7];
43 struct spi_device
*spi
;
44 struct iio_trigger
*trig
;
45 const struct ad7793_chip_info
*chip_info
;
46 struct regulator
*reg
;
47 struct ad7793_platform_data
*pdata
;
48 wait_queue_head_t wq_data_avail
;
54 u32 scale_avail
[8][2];
55 /* Note this uses fact that 8 the mask always fits in a long */
56 unsigned long available_scan_masks
[7];
58 * DMA (thus cache coherency maintenance) requires the
59 * transfer buffers to live in their own cache lines.
61 u8 data
[4] ____cacheline_aligned
;
64 enum ad7793_supported_device_ids
{
69 static int __ad7793_write_reg(struct ad7793_state
*st
, bool locked
,
70 bool cs_change
, unsigned char reg
,
71 unsigned size
, unsigned val
)
74 struct spi_transfer t
= {
77 .cs_change
= cs_change
,
81 data
[0] = AD7793_COMM_WRITE
| AD7793_COMM_ADDR(reg
);
100 spi_message_init(&m
);
101 spi_message_add_tail(&t
, &m
);
104 return spi_sync_locked(st
->spi
, &m
);
106 return spi_sync(st
->spi
, &m
);
109 static int ad7793_write_reg(struct ad7793_state
*st
,
110 unsigned reg
, unsigned size
, unsigned val
)
112 return __ad7793_write_reg(st
, false, false, reg
, size
, val
);
115 static int __ad7793_read_reg(struct ad7793_state
*st
, bool locked
,
116 bool cs_change
, unsigned char reg
,
117 int *val
, unsigned size
)
121 struct spi_transfer t
[] = {
128 .cs_change
= cs_change
,
131 struct spi_message m
;
133 data
[0] = AD7793_COMM_READ
| AD7793_COMM_ADDR(reg
);
135 spi_message_init(&m
);
136 spi_message_add_tail(&t
[0], &m
);
137 spi_message_add_tail(&t
[1], &m
);
140 ret
= spi_sync_locked(st
->spi
, &m
);
142 ret
= spi_sync(st
->spi
, &m
);
149 *val
= data
[0] << 16 | data
[1] << 8 | data
[2];
152 *val
= data
[0] << 8 | data
[1];
164 static int ad7793_read_reg(struct ad7793_state
*st
,
165 unsigned reg
, int *val
, unsigned size
)
167 return __ad7793_read_reg(st
, 0, 0, reg
, val
, size
);
170 static int ad7793_read(struct ad7793_state
*st
, unsigned ch
,
171 unsigned len
, int *val
)
174 st
->conf
= (st
->conf
& ~AD7793_CONF_CHAN(-1)) | AD7793_CONF_CHAN(ch
);
175 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) |
176 AD7793_MODE_SEL(AD7793_MODE_SINGLE
);
178 ad7793_write_reg(st
, AD7793_REG_CONF
, sizeof(st
->conf
), st
->conf
);
180 spi_bus_lock(st
->spi
->master
);
183 ret
= __ad7793_write_reg(st
, 1, 1, AD7793_REG_MODE
,
184 sizeof(st
->mode
), st
->mode
);
189 enable_irq(st
->spi
->irq
);
190 wait_event_interruptible(st
->wq_data_avail
, st
->done
);
192 ret
= __ad7793_read_reg(st
, 1, 0, AD7793_REG_DATA
, val
, len
);
194 spi_bus_unlock(st
->spi
->master
);
199 static int ad7793_calibrate(struct ad7793_state
*st
, unsigned mode
, unsigned ch
)
203 st
->conf
= (st
->conf
& ~AD7793_CONF_CHAN(-1)) | AD7793_CONF_CHAN(ch
);
204 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) | AD7793_MODE_SEL(mode
);
206 ad7793_write_reg(st
, AD7793_REG_CONF
, sizeof(st
->conf
), st
->conf
);
208 spi_bus_lock(st
->spi
->master
);
211 ret
= __ad7793_write_reg(st
, 1, 1, AD7793_REG_MODE
,
212 sizeof(st
->mode
), st
->mode
);
217 enable_irq(st
->spi
->irq
);
218 wait_event_interruptible(st
->wq_data_avail
, st
->done
);
220 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) |
221 AD7793_MODE_SEL(AD7793_MODE_IDLE
);
223 ret
= __ad7793_write_reg(st
, 1, 0, AD7793_REG_MODE
,
224 sizeof(st
->mode
), st
->mode
);
226 spi_bus_unlock(st
->spi
->master
);
231 static const u8 ad7793_calib_arr
[6][2] = {
232 {AD7793_MODE_CAL_INT_ZERO
, AD7793_CH_AIN1P_AIN1M
},
233 {AD7793_MODE_CAL_INT_FULL
, AD7793_CH_AIN1P_AIN1M
},
234 {AD7793_MODE_CAL_INT_ZERO
, AD7793_CH_AIN2P_AIN2M
},
235 {AD7793_MODE_CAL_INT_FULL
, AD7793_CH_AIN2P_AIN2M
},
236 {AD7793_MODE_CAL_INT_ZERO
, AD7793_CH_AIN3P_AIN3M
},
237 {AD7793_MODE_CAL_INT_FULL
, AD7793_CH_AIN3P_AIN3M
}
240 static int ad7793_calibrate_all(struct ad7793_state
*st
)
244 for (i
= 0; i
< ARRAY_SIZE(ad7793_calib_arr
); i
++) {
245 ret
= ad7793_calibrate(st
, ad7793_calib_arr
[i
][0],
246 ad7793_calib_arr
[i
][1]);
253 dev_err(&st
->spi
->dev
, "Calibration failed\n");
257 static int ad7793_setup(struct ad7793_state
*st
)
260 unsigned long long scale_uv
;
263 /* reset the serial interface */
264 ret
= spi_write(st
->spi
, (u8
*)&ret
, sizeof(ret
));
267 msleep(1); /* Wait for at least 500us */
269 /* write/read test for device presence */
270 ret
= ad7793_read_reg(st
, AD7793_REG_ID
, &id
, 1);
274 id
&= AD7793_ID_MASK
;
276 if (!((id
== AD7792_ID
) || (id
== AD7793_ID
))) {
277 dev_err(&st
->spi
->dev
, "device ID query failed\n");
281 st
->mode
= (st
->pdata
->mode
& ~AD7793_MODE_SEL(-1)) |
282 AD7793_MODE_SEL(AD7793_MODE_IDLE
);
283 st
->conf
= st
->pdata
->conf
& ~AD7793_CONF_CHAN(-1);
285 ret
= ad7793_write_reg(st
, AD7793_REG_MODE
, sizeof(st
->mode
), st
->mode
);
289 ret
= ad7793_write_reg(st
, AD7793_REG_CONF
, sizeof(st
->conf
), st
->conf
);
293 ret
= ad7793_write_reg(st
, AD7793_REG_IO
,
294 sizeof(st
->pdata
->io
), st
->pdata
->io
);
298 ret
= ad7793_calibrate_all(st
);
302 /* Populate available ADC input ranges */
303 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++) {
304 scale_uv
= ((u64
)st
->int_vref_mv
* 100000000)
305 >> (st
->chip_info
->channel
[0].scan_type
.realbits
-
306 (!!(st
->conf
& AD7793_CONF_UNIPOLAR
) ? 0 : 1));
309 st
->scale_avail
[i
][1] = do_div(scale_uv
, 100000000) * 10;
310 st
->scale_avail
[i
][0] = scale_uv
;
315 dev_err(&st
->spi
->dev
, "setup failed\n");
319 static int ad7793_ring_preenable(struct iio_dev
*indio_dev
)
321 struct ad7793_state
*st
= iio_priv(indio_dev
);
322 struct iio_buffer
*ring
= indio_dev
->buffer
;
326 if (bitmap_empty(indio_dev
->active_scan_mask
, indio_dev
->masklength
))
329 channel
= find_first_bit(indio_dev
->active_scan_mask
,
330 indio_dev
->masklength
);
332 d_size
= bitmap_weight(indio_dev
->active_scan_mask
,
333 indio_dev
->masklength
) *
334 indio_dev
->channels
[0].scan_type
.storagebits
/ 8;
336 if (ring
->scan_timestamp
) {
337 d_size
+= sizeof(s64
);
339 if (d_size
% sizeof(s64
))
340 d_size
+= sizeof(s64
) - (d_size
% sizeof(s64
));
343 if (indio_dev
->buffer
->access
->set_bytes_per_datum
)
344 indio_dev
->buffer
->access
->
345 set_bytes_per_datum(indio_dev
->buffer
, d_size
);
347 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) |
348 AD7793_MODE_SEL(AD7793_MODE_CONT
);
349 st
->conf
= (st
->conf
& ~AD7793_CONF_CHAN(-1)) |
350 AD7793_CONF_CHAN(indio_dev
->channels
[channel
].address
);
352 ad7793_write_reg(st
, AD7793_REG_CONF
, sizeof(st
->conf
), st
->conf
);
354 spi_bus_lock(st
->spi
->master
);
355 __ad7793_write_reg(st
, 1, 1, AD7793_REG_MODE
,
356 sizeof(st
->mode
), st
->mode
);
359 enable_irq(st
->spi
->irq
);
364 static int ad7793_ring_postdisable(struct iio_dev
*indio_dev
)
366 struct ad7793_state
*st
= iio_priv(indio_dev
);
368 st
->mode
= (st
->mode
& ~AD7793_MODE_SEL(-1)) |
369 AD7793_MODE_SEL(AD7793_MODE_IDLE
);
372 wait_event_interruptible(st
->wq_data_avail
, st
->done
);
375 disable_irq_nosync(st
->spi
->irq
);
377 __ad7793_write_reg(st
, 1, 0, AD7793_REG_MODE
,
378 sizeof(st
->mode
), st
->mode
);
380 return spi_bus_unlock(st
->spi
->master
);
384 * ad7793_trigger_handler() bh of trigger launched polling to ring buffer
387 static irqreturn_t
ad7793_trigger_handler(int irq
, void *p
)
389 struct iio_poll_func
*pf
= p
;
390 struct iio_dev
*indio_dev
= pf
->indio_dev
;
391 struct iio_buffer
*ring
= indio_dev
->buffer
;
392 struct ad7793_state
*st
= iio_priv(indio_dev
);
394 s32
*dat32
= (s32
*)dat64
;
396 if (!bitmap_empty(indio_dev
->active_scan_mask
, indio_dev
->masklength
))
397 __ad7793_read_reg(st
, 1, 1, AD7793_REG_DATA
,
399 indio_dev
->channels
[0].scan_type
.realbits
/8);
401 /* Guaranteed to be aligned with 8 byte boundary */
402 if (ring
->scan_timestamp
)
403 dat64
[1] = pf
->timestamp
;
405 ring
->access
->store_to(ring
, (u8
*)dat64
, pf
->timestamp
);
407 iio_trigger_notify_done(indio_dev
->trig
);
409 enable_irq(st
->spi
->irq
);
414 static const struct iio_buffer_setup_ops ad7793_ring_setup_ops
= {
415 .preenable
= &ad7793_ring_preenable
,
416 .postenable
= &iio_triggered_buffer_postenable
,
417 .predisable
= &iio_triggered_buffer_predisable
,
418 .postdisable
= &ad7793_ring_postdisable
,
421 static int ad7793_register_ring_funcs_and_init(struct iio_dev
*indio_dev
)
425 indio_dev
->buffer
= iio_sw_rb_allocate(indio_dev
);
426 if (!indio_dev
->buffer
) {
430 /* Effectively select the ring buffer implementation */
431 indio_dev
->buffer
->access
= &ring_sw_access_funcs
;
432 indio_dev
->pollfunc
= iio_alloc_pollfunc(&iio_pollfunc_store_time
,
433 &ad7793_trigger_handler
,
438 if (indio_dev
->pollfunc
== NULL
) {
440 goto error_deallocate_sw_rb
;
443 /* Ring buffer functions - here trigger setup related */
444 indio_dev
->setup_ops
= &ad7793_ring_setup_ops
;
446 /* Flag that polled ring buffering is possible */
447 indio_dev
->modes
|= INDIO_BUFFER_TRIGGERED
;
450 error_deallocate_sw_rb
:
451 iio_sw_rb_free(indio_dev
->buffer
);
456 static void ad7793_ring_cleanup(struct iio_dev
*indio_dev
)
458 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
459 iio_sw_rb_free(indio_dev
->buffer
);
463 * ad7793_data_rdy_trig_poll() the event handler for the data rdy trig
465 static irqreturn_t
ad7793_data_rdy_trig_poll(int irq
, void *private)
467 struct ad7793_state
*st
= iio_priv(private);
470 wake_up_interruptible(&st
->wq_data_avail
);
471 disable_irq_nosync(irq
);
473 iio_trigger_poll(st
->trig
, iio_get_time_ns());
478 static struct iio_trigger_ops ad7793_trigger_ops
= {
479 .owner
= THIS_MODULE
,
482 static int ad7793_probe_trigger(struct iio_dev
*indio_dev
)
484 struct ad7793_state
*st
= iio_priv(indio_dev
);
487 st
->trig
= iio_allocate_trigger("%s-dev%d",
488 spi_get_device_id(st
->spi
)->name
,
490 if (st
->trig
== NULL
) {
494 st
->trig
->ops
= &ad7793_trigger_ops
;
496 ret
= request_irq(st
->spi
->irq
,
497 ad7793_data_rdy_trig_poll
,
499 spi_get_device_id(st
->spi
)->name
,
502 goto error_free_trig
;
504 disable_irq_nosync(st
->spi
->irq
);
506 st
->trig
->dev
.parent
= &st
->spi
->dev
;
507 st
->trig
->private_data
= indio_dev
;
509 ret
= iio_trigger_register(st
->trig
);
511 /* select default trigger */
512 indio_dev
->trig
= st
->trig
;
519 free_irq(st
->spi
->irq
, indio_dev
);
521 iio_free_trigger(st
->trig
);
526 static void ad7793_remove_trigger(struct iio_dev
*indio_dev
)
528 struct ad7793_state
*st
= iio_priv(indio_dev
);
530 iio_trigger_unregister(st
->trig
);
531 free_irq(st
->spi
->irq
, indio_dev
);
532 iio_free_trigger(st
->trig
);
535 static const u16 sample_freq_avail
[16] = {0, 470, 242, 123, 62, 50, 39, 33, 19,
536 17, 16, 12, 10, 8, 6, 4};
538 static ssize_t
ad7793_read_frequency(struct device
*dev
,
539 struct device_attribute
*attr
,
542 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
543 struct ad7793_state
*st
= iio_priv(indio_dev
);
545 return sprintf(buf
, "%d\n",
546 sample_freq_avail
[AD7793_MODE_RATE(st
->mode
)]);
549 static ssize_t
ad7793_write_frequency(struct device
*dev
,
550 struct device_attribute
*attr
,
554 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
555 struct ad7793_state
*st
= iio_priv(indio_dev
);
559 mutex_lock(&indio_dev
->mlock
);
560 if (iio_buffer_enabled(indio_dev
)) {
561 mutex_unlock(&indio_dev
->mlock
);
564 mutex_unlock(&indio_dev
->mlock
);
566 ret
= strict_strtol(buf
, 10, &lval
);
572 for (i
= 0; i
< ARRAY_SIZE(sample_freq_avail
); i
++)
573 if (lval
== sample_freq_avail
[i
]) {
574 mutex_lock(&indio_dev
->mlock
);
575 st
->mode
&= ~AD7793_MODE_RATE(-1);
576 st
->mode
|= AD7793_MODE_RATE(i
);
577 ad7793_write_reg(st
, AD7793_REG_MODE
,
578 sizeof(st
->mode
), st
->mode
);
579 mutex_unlock(&indio_dev
->mlock
);
583 return ret
? ret
: len
;
586 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
587 ad7793_read_frequency
,
588 ad7793_write_frequency
);
590 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
591 "470 242 123 62 50 39 33 19 17 16 12 10 8 6 4");
593 static ssize_t
ad7793_show_scale_available(struct device
*dev
,
594 struct device_attribute
*attr
, char *buf
)
596 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
597 struct ad7793_state
*st
= iio_priv(indio_dev
);
600 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
601 len
+= sprintf(buf
+ len
, "%d.%09u ", st
->scale_avail
[i
][0],
602 st
->scale_avail
[i
][1]);
604 len
+= sprintf(buf
+ len
, "\n");
609 static IIO_DEVICE_ATTR_NAMED(in_m_in_scale_available
, in
-in_scale_available
,
610 S_IRUGO
, ad7793_show_scale_available
, NULL
, 0);
612 static struct attribute
*ad7793_attributes
[] = {
613 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
614 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
615 &iio_dev_attr_in_m_in_scale_available
.dev_attr
.attr
,
619 static const struct attribute_group ad7793_attribute_group
= {
620 .attrs
= ad7793_attributes
,
623 static int ad7793_read_raw(struct iio_dev
*indio_dev
,
624 struct iio_chan_spec
const *chan
,
629 struct ad7793_state
*st
= iio_priv(indio_dev
);
631 unsigned long long scale_uv
;
632 bool unipolar
= !!(st
->conf
& AD7793_CONF_UNIPOLAR
);
636 mutex_lock(&indio_dev
->mlock
);
637 if (iio_buffer_enabled(indio_dev
))
640 ret
= ad7793_read(st
, chan
->address
,
641 chan
->scan_type
.realbits
/ 8, &smpl
);
642 mutex_unlock(&indio_dev
->mlock
);
647 *val
= (smpl
>> chan
->scan_type
.shift
) &
648 ((1 << (chan
->scan_type
.realbits
)) - 1);
651 *val
-= (1 << (chan
->scan_type
.realbits
- 1));
655 case IIO_CHAN_INFO_SCALE
:
656 switch (chan
->type
) {
658 if (chan
->differential
) {
660 scale_avail
[(st
->conf
>> 8) & 0x7][0];
662 scale_avail
[(st
->conf
>> 8) & 0x7][1];
663 return IIO_VAL_INT_PLUS_NANO
;
665 /* 1170mV / 2^23 * 6 */
666 scale_uv
= (1170ULL * 100000000ULL * 6ULL)
667 >> (chan
->scan_type
.realbits
-
672 /* Always uses unity gain and internal ref */
673 scale_uv
= (2500ULL * 100000000ULL)
674 >> (chan
->scan_type
.realbits
-
681 *val2
= do_div(scale_uv
, 100000000) * 10;
684 return IIO_VAL_INT_PLUS_NANO
;
689 static int ad7793_write_raw(struct iio_dev
*indio_dev
,
690 struct iio_chan_spec
const *chan
,
695 struct ad7793_state
*st
= iio_priv(indio_dev
);
699 mutex_lock(&indio_dev
->mlock
);
700 if (iio_buffer_enabled(indio_dev
)) {
701 mutex_unlock(&indio_dev
->mlock
);
706 case IIO_CHAN_INFO_SCALE
:
708 for (i
= 0; i
< ARRAY_SIZE(st
->scale_avail
); i
++)
709 if (val2
== st
->scale_avail
[i
][1]) {
711 st
->conf
&= ~AD7793_CONF_GAIN(-1);
712 st
->conf
|= AD7793_CONF_GAIN(i
);
714 if (tmp
!= st
->conf
) {
715 ad7793_write_reg(st
, AD7793_REG_CONF
,
718 ad7793_calibrate_all(st
);
727 mutex_unlock(&indio_dev
->mlock
);
731 static int ad7793_validate_trigger(struct iio_dev
*indio_dev
,
732 struct iio_trigger
*trig
)
734 if (indio_dev
->trig
!= trig
)
740 static int ad7793_write_raw_get_fmt(struct iio_dev
*indio_dev
,
741 struct iio_chan_spec
const *chan
,
744 return IIO_VAL_INT_PLUS_NANO
;
747 static const struct iio_info ad7793_info
= {
748 .read_raw
= &ad7793_read_raw
,
749 .write_raw
= &ad7793_write_raw
,
750 .write_raw_get_fmt
= &ad7793_write_raw_get_fmt
,
751 .attrs
= &ad7793_attribute_group
,
752 .validate_trigger
= ad7793_validate_trigger
,
753 .driver_module
= THIS_MODULE
,
756 static const struct ad7793_chip_info ad7793_chip_info_tbl
[] = {
764 .address
= AD7793_CH_AIN1P_AIN1M
,
765 .info_mask
= IIO_CHAN_INFO_SCALE_SHARED_BIT
,
767 .scan_type
= IIO_ST('s', 24, 32, 0)
775 .address
= AD7793_CH_AIN2P_AIN2M
,
776 .info_mask
= IIO_CHAN_INFO_SCALE_SHARED_BIT
,
778 .scan_type
= IIO_ST('s', 24, 32, 0)
786 .address
= AD7793_CH_AIN3P_AIN3M
,
787 .info_mask
= IIO_CHAN_INFO_SCALE_SHARED_BIT
,
789 .scan_type
= IIO_ST('s', 24, 32, 0)
794 .extend_name
= "shorted",
798 .address
= AD7793_CH_AIN1M_AIN1M
,
799 .info_mask
= IIO_CHAN_INFO_SCALE_SHARED_BIT
,
801 .scan_type
= IIO_ST('s', 24, 32, 0)
807 .address
= AD7793_CH_TEMP
,
808 .info_mask
= IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
810 .scan_type
= IIO_ST('s', 24, 32, 0),
814 .extend_name
= "supply",
817 .address
= AD7793_CH_AVDD_MONITOR
,
818 .info_mask
= IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
820 .scan_type
= IIO_ST('s', 24, 32, 0),
822 .channel
[6] = IIO_CHAN_SOFT_TIMESTAMP(6),
831 .address
= AD7793_CH_AIN1P_AIN1M
,
832 .info_mask
= IIO_CHAN_INFO_SCALE_SHARED_BIT
,
834 .scan_type
= IIO_ST('s', 16, 32, 0)
842 .address
= AD7793_CH_AIN2P_AIN2M
,
843 .info_mask
= IIO_CHAN_INFO_SCALE_SHARED_BIT
,
845 .scan_type
= IIO_ST('s', 16, 32, 0)
853 .address
= AD7793_CH_AIN3P_AIN3M
,
854 .info_mask
= IIO_CHAN_INFO_SCALE_SHARED_BIT
,
856 .scan_type
= IIO_ST('s', 16, 32, 0)
861 .extend_name
= "shorted",
865 .address
= AD7793_CH_AIN1M_AIN1M
,
866 .info_mask
= IIO_CHAN_INFO_SCALE_SHARED_BIT
,
868 .scan_type
= IIO_ST('s', 16, 32, 0)
874 .address
= AD7793_CH_TEMP
,
875 .info_mask
= IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
877 .scan_type
= IIO_ST('s', 16, 32, 0),
881 .extend_name
= "supply",
884 .address
= AD7793_CH_AVDD_MONITOR
,
885 .info_mask
= IIO_CHAN_INFO_SCALE_SEPARATE_BIT
,
887 .scan_type
= IIO_ST('s', 16, 32, 0),
889 .channel
[6] = IIO_CHAN_SOFT_TIMESTAMP(6),
893 static int __devinit
ad7793_probe(struct spi_device
*spi
)
895 struct ad7793_platform_data
*pdata
= spi
->dev
.platform_data
;
896 struct ad7793_state
*st
;
897 struct iio_dev
*indio_dev
;
898 int ret
, i
, voltage_uv
= 0;
901 dev_err(&spi
->dev
, "no platform data?\n");
906 dev_err(&spi
->dev
, "no IRQ?\n");
910 indio_dev
= iio_allocate_device(sizeof(*st
));
911 if (indio_dev
== NULL
)
914 st
= iio_priv(indio_dev
);
916 st
->reg
= regulator_get(&spi
->dev
, "vcc");
917 if (!IS_ERR(st
->reg
)) {
918 ret
= regulator_enable(st
->reg
);
922 voltage_uv
= regulator_get_voltage(st
->reg
);
926 &ad7793_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
930 if (pdata
&& pdata
->vref_mv
)
931 st
->int_vref_mv
= pdata
->vref_mv
;
933 st
->int_vref_mv
= voltage_uv
/ 1000;
935 st
->int_vref_mv
= 2500; /* Build-in ref */
937 spi_set_drvdata(spi
, indio_dev
);
940 indio_dev
->dev
.parent
= &spi
->dev
;
941 indio_dev
->name
= spi_get_device_id(spi
)->name
;
942 indio_dev
->modes
= INDIO_DIRECT_MODE
;
943 indio_dev
->channels
= st
->chip_info
->channel
;
944 indio_dev
->available_scan_masks
= st
->available_scan_masks
;
945 indio_dev
->num_channels
= 7;
946 indio_dev
->info
= &ad7793_info
;
948 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
949 set_bit(i
, &st
->available_scan_masks
[i
]);
951 channels
[indio_dev
->num_channels
- 1].scan_index
,
952 &st
->available_scan_masks
[i
]);
955 init_waitqueue_head(&st
->wq_data_avail
);
957 ret
= ad7793_register_ring_funcs_and_init(indio_dev
);
959 goto error_disable_reg
;
961 ret
= ad7793_probe_trigger(indio_dev
);
963 goto error_unreg_ring
;
965 ret
= iio_buffer_register(indio_dev
,
967 indio_dev
->num_channels
);
969 goto error_remove_trigger
;
971 ret
= ad7793_setup(st
);
973 goto error_uninitialize_ring
;
975 ret
= iio_device_register(indio_dev
);
977 goto error_uninitialize_ring
;
981 error_uninitialize_ring
:
982 iio_buffer_unregister(indio_dev
);
983 error_remove_trigger
:
984 ad7793_remove_trigger(indio_dev
);
986 ad7793_ring_cleanup(indio_dev
);
988 if (!IS_ERR(st
->reg
))
989 regulator_disable(st
->reg
);
991 if (!IS_ERR(st
->reg
))
992 regulator_put(st
->reg
);
994 iio_free_device(indio_dev
);
999 static int ad7793_remove(struct spi_device
*spi
)
1001 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
1002 struct ad7793_state
*st
= iio_priv(indio_dev
);
1004 iio_device_unregister(indio_dev
);
1005 iio_buffer_unregister(indio_dev
);
1006 ad7793_remove_trigger(indio_dev
);
1007 ad7793_ring_cleanup(indio_dev
);
1009 if (!IS_ERR(st
->reg
)) {
1010 regulator_disable(st
->reg
);
1011 regulator_put(st
->reg
);
1014 iio_free_device(indio_dev
);
1019 static const struct spi_device_id ad7793_id
[] = {
1020 {"ad7792", ID_AD7792
},
1021 {"ad7793", ID_AD7793
},
1024 MODULE_DEVICE_TABLE(spi
, ad7793_id
);
1026 static struct spi_driver ad7793_driver
= {
1029 .owner
= THIS_MODULE
,
1031 .probe
= ad7793_probe
,
1032 .remove
= __devexit_p(ad7793_remove
),
1033 .id_table
= ad7793_id
,
1035 module_spi_driver(ad7793_driver
);
1037 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
1038 MODULE_DESCRIPTION("Analog Devices AD7792/3 ADC");
1039 MODULE_LICENSE("GPL v2");