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_scan_from_ring(struct ad7793_state *st, unsigned ch, int *val)
321 struct iio_buffer *ring = iio_priv_to_dev(st)->buffer;
324 u32 *dat32 = (u32 *)dat64;
326 if (!(test_bit(ch, ring->scan_mask)))
329 ret = ring->access->read_last(ring, (u8 *) &dat64);
338 static int ad7793_ring_preenable(struct iio_dev *indio_dev)
340 struct ad7793_state *st = iio_priv(indio_dev);
341 struct iio_buffer *ring = indio_dev->buffer;
345 if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
348 channel = find_first_bit(indio_dev->active_scan_mask,
349 indio_dev->masklength);
351 d_size = bitmap_weight(indio_dev->active_scan_mask,
352 indio_dev->masklength) *
353 indio_dev->channels[0].scan_type.storagebits / 8;
355 if (ring->scan_timestamp) {
356 d_size += sizeof(s64);
358 if (d_size % sizeof(s64))
359 d_size += sizeof(s64) - (d_size % sizeof(s64));
362 if (indio_dev->buffer->access->set_bytes_per_datum)
363 indio_dev->buffer->access->
364 set_bytes_per_datum(indio_dev->buffer, d_size);
366 st->mode = (st->mode & ~AD7793_MODE_SEL(-1)) |
367 AD7793_MODE_SEL(AD7793_MODE_CONT);
368 st->conf = (st->conf & ~AD7793_CONF_CHAN(-1)) |
369 AD7793_CONF_CHAN(indio_dev->channels[channel].address);
371 ad7793_write_reg(st, AD7793_REG_CONF, sizeof(st->conf), st->conf);
373 spi_bus_lock(st->spi->master);
374 __ad7793_write_reg(st, 1, 1, AD7793_REG_MODE,
375 sizeof(st->mode), st->mode);
378 enable_irq(st->spi->irq);
383 static int ad7793_ring_postdisable(struct iio_dev *indio_dev)
385 struct ad7793_state *st = iio_priv(indio_dev);
387 st->mode = (st->mode & ~AD7793_MODE_SEL(-1)) |
388 AD7793_MODE_SEL(AD7793_MODE_IDLE);
391 wait_event_interruptible(st->wq_data_avail, st->done);
394 disable_irq_nosync(st->spi->irq);
396 __ad7793_write_reg(st, 1, 0, AD7793_REG_MODE,
397 sizeof(st->mode), st->mode);
399 return spi_bus_unlock(st->spi->master);
403 * ad7793_trigger_handler() bh of trigger launched polling to ring buffer
406 static irqreturn_t ad7793_trigger_handler(int irq, void *p)
408 struct iio_poll_func *pf = p;
409 struct iio_dev *indio_dev = pf->indio_dev;
410 struct iio_buffer *ring = indio_dev->buffer;
411 struct ad7793_state *st = iio_priv(indio_dev);
413 s32 *dat32 = (s32 *)dat64;
415 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
416 __ad7793_read_reg(st, 1, 1, AD7793_REG_DATA,
418 indio_dev->channels[0].scan_type.realbits/8);
420 /* Guaranteed to be aligned with 8 byte boundary */
421 if (ring->scan_timestamp)
422 dat64[1] = pf->timestamp;
424 ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
426 iio_trigger_notify_done(indio_dev->trig);
428 enable_irq(st->spi->irq);
433 static const struct iio_buffer_setup_ops ad7793_ring_setup_ops = {
434 .preenable = &ad7793_ring_preenable,
435 .postenable = &iio_triggered_buffer_postenable,
436 .predisable = &iio_triggered_buffer_predisable,
437 .postdisable = &ad7793_ring_postdisable,
440 static int ad7793_register_ring_funcs_and_init(struct iio_dev *indio_dev)
444 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
445 if (!indio_dev->buffer) {
449 /* Effectively select the ring buffer implementation */
450 indio_dev->buffer->access = &ring_sw_access_funcs;
451 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
452 &ad7793_trigger_handler,
457 if (indio_dev->pollfunc == NULL) {
459 goto error_deallocate_sw_rb;
462 /* Ring buffer functions - here trigger setup related */
463 indio_dev->setup_ops = &ad7793_ring_setup_ops;
465 /* Flag that polled ring buffering is possible */
466 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
469 error_deallocate_sw_rb:
470 iio_sw_rb_free(indio_dev->buffer);
475 static void ad7793_ring_cleanup(struct iio_dev *indio_dev)
477 iio_dealloc_pollfunc(indio_dev->pollfunc);
478 iio_sw_rb_free(indio_dev->buffer);
482 * ad7793_data_rdy_trig_poll() the event handler for the data rdy trig
484 static irqreturn_t ad7793_data_rdy_trig_poll(int irq, void *private)
486 struct ad7793_state *st = iio_priv(private);
489 wake_up_interruptible(&st->wq_data_avail);
490 disable_irq_nosync(irq);
492 iio_trigger_poll(st->trig, iio_get_time_ns());
497 static int ad7793_probe_trigger(struct iio_dev *indio_dev)
499 struct ad7793_state *st = iio_priv(indio_dev);
502 st->trig = iio_allocate_trigger("%s-dev%d",
503 spi_get_device_id(st->spi)->name,
505 if (st->trig == NULL) {
510 ret = request_irq(st->spi->irq,
511 ad7793_data_rdy_trig_poll,
513 spi_get_device_id(st->spi)->name,
516 goto error_free_trig;
518 disable_irq_nosync(st->spi->irq);
520 st->trig->dev.parent = &st->spi->dev;
521 st->trig->owner = THIS_MODULE;
522 st->trig->private_data = indio_dev;
524 ret = iio_trigger_register(st->trig);
526 /* select default trigger */
527 indio_dev->trig = st->trig;
534 free_irq(st->spi->irq, indio_dev);
536 iio_free_trigger(st->trig);
541 static void ad7793_remove_trigger(struct iio_dev *indio_dev)
543 struct ad7793_state *st = iio_priv(indio_dev);
545 iio_trigger_unregister(st->trig);
546 free_irq(st->spi->irq, indio_dev);
547 iio_free_trigger(st->trig);
550 static const u16 sample_freq_avail[16] = {0, 470, 242, 123, 62, 50, 39, 33, 19,
551 17, 16, 12, 10, 8, 6, 4};
553 static ssize_t ad7793_read_frequency(struct device *dev,
554 struct device_attribute *attr,
557 struct iio_dev *indio_dev = dev_get_drvdata(dev);
558 struct ad7793_state *st = iio_priv(indio_dev);
560 return sprintf(buf, "%d\n",
561 sample_freq_avail[AD7793_MODE_RATE(st->mode)]);
564 static ssize_t ad7793_write_frequency(struct device *dev,
565 struct device_attribute *attr,
569 struct iio_dev *indio_dev = dev_get_drvdata(dev);
570 struct ad7793_state *st = iio_priv(indio_dev);
574 mutex_lock(&indio_dev->mlock);
575 if (iio_buffer_enabled(indio_dev)) {
576 mutex_unlock(&indio_dev->mlock);
579 mutex_unlock(&indio_dev->mlock);
581 ret = strict_strtol(buf, 10, &lval);
587 for (i = 0; i < ARRAY_SIZE(sample_freq_avail); i++)
588 if (lval == sample_freq_avail[i]) {
589 mutex_lock(&indio_dev->mlock);
590 st->mode &= ~AD7793_MODE_RATE(-1);
591 st->mode |= AD7793_MODE_RATE(i);
592 ad7793_write_reg(st, AD7793_REG_MODE,
593 sizeof(st->mode), st->mode);
594 mutex_unlock(&indio_dev->mlock);
598 return ret ? ret : len;
601 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
602 ad7793_read_frequency,
603 ad7793_write_frequency);
605 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
606 "470 242 123 62 50 39 33 19 17 16 12 10 8 6 4");
608 static ssize_t ad7793_show_scale_available(struct device *dev,
609 struct device_attribute *attr, char *buf)
611 struct iio_dev *indio_dev = dev_get_drvdata(dev);
612 struct ad7793_state *st = iio_priv(indio_dev);
615 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
616 len += sprintf(buf + len, "%d.%09u ", st->scale_avail[i][0],
617 st->scale_avail[i][1]);
619 len += sprintf(buf + len, "\n");
624 static IIO_DEVICE_ATTR_NAMED(in_m_in_scale_available, in-in_scale_available,
625 S_IRUGO, ad7793_show_scale_available, NULL, 0);
627 static struct attribute *ad7793_attributes[] = {
628 &iio_dev_attr_sampling_frequency.dev_attr.attr,
629 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
630 &iio_dev_attr_in_m_in_scale_available.dev_attr.attr,
634 static const struct attribute_group ad7793_attribute_group = {
635 .attrs = ad7793_attributes,
638 static int ad7793_read_raw(struct iio_dev *indio_dev,
639 struct iio_chan_spec const *chan,
644 struct ad7793_state *st = iio_priv(indio_dev);
646 unsigned long long scale_uv;
647 bool unipolar = !!(st->conf & AD7793_CONF_UNIPOLAR);
651 mutex_lock(&indio_dev->mlock);
652 if (iio_buffer_enabled(indio_dev))
653 ret = ad7793_scan_from_ring(st,
654 chan->scan_index, &smpl);
656 ret = ad7793_read(st, chan->address,
657 chan->scan_type.realbits / 8, &smpl);
658 mutex_unlock(&indio_dev->mlock);
663 *val = (smpl >> chan->scan_type.shift) &
664 ((1 << (chan->scan_type.realbits)) - 1);
667 *val -= (1 << (chan->scan_type.realbits - 1));
671 case IIO_CHAN_INFO_SCALE:
672 switch (chan->type) {
674 if (chan->differential) {
676 scale_avail[(st->conf >> 8) & 0x7][0];
678 scale_avail[(st->conf >> 8) & 0x7][1];
679 return IIO_VAL_INT_PLUS_NANO;
681 /* 1170mV / 2^23 * 6 */
682 scale_uv = (1170ULL * 100000000ULL * 6ULL)
683 >> (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_buffer_enabled(indio_dev)) {
717 mutex_unlock(&indio_dev->mlock);
722 case IIO_CHAN_INFO_SCALE:
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[] = {
780 .address = AD7793_CH_AIN1P_AIN1M,
781 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
783 .scan_type = IIO_ST('s', 24, 32, 0)
791 .address = AD7793_CH_AIN2P_AIN2M,
792 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
794 .scan_type = IIO_ST('s', 24, 32, 0)
802 .address = AD7793_CH_AIN3P_AIN3M,
803 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
805 .scan_type = IIO_ST('s', 24, 32, 0)
810 .extend_name = "shorted",
814 .address = AD7793_CH_AIN1M_AIN1M,
815 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
817 .scan_type = IIO_ST('s', 24, 32, 0)
823 .address = AD7793_CH_TEMP,
824 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
826 .scan_type = IIO_ST('s', 24, 32, 0),
830 .extend_name = "supply",
833 .address = AD7793_CH_AVDD_MONITOR,
834 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
836 .scan_type = IIO_ST('s', 24, 32, 0),
838 .channel[6] = IIO_CHAN_SOFT_TIMESTAMP(6),
847 .address = AD7793_CH_AIN1P_AIN1M,
848 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
850 .scan_type = IIO_ST('s', 16, 32, 0)
858 .address = AD7793_CH_AIN2P_AIN2M,
859 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
861 .scan_type = IIO_ST('s', 16, 32, 0)
869 .address = AD7793_CH_AIN3P_AIN3M,
870 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
872 .scan_type = IIO_ST('s', 16, 32, 0)
877 .extend_name = "shorted",
881 .address = AD7793_CH_AIN1M_AIN1M,
882 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
884 .scan_type = IIO_ST('s', 16, 32, 0)
890 .address = AD7793_CH_TEMP,
891 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
893 .scan_type = IIO_ST('s', 16, 32, 0),
897 .extend_name = "supply",
900 .address = AD7793_CH_AVDD_MONITOR,
901 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
903 .scan_type = IIO_ST('s', 16, 32, 0),
905 .channel[6] = IIO_CHAN_SOFT_TIMESTAMP(6),
909 static int __devinit ad7793_probe(struct spi_device *spi)
911 struct ad7793_platform_data *pdata = spi->dev.platform_data;
912 struct ad7793_state *st;
913 struct iio_dev *indio_dev;
914 int ret, i, voltage_uv = 0;
917 dev_err(&spi->dev, "no platform data?\n");
922 dev_err(&spi->dev, "no IRQ?\n");
926 indio_dev = iio_allocate_device(sizeof(*st));
927 if (indio_dev == NULL)
930 st = iio_priv(indio_dev);
932 st->reg = regulator_get(&spi->dev, "vcc");
933 if (!IS_ERR(st->reg)) {
934 ret = regulator_enable(st->reg);
938 voltage_uv = regulator_get_voltage(st->reg);
942 &ad7793_chip_info_tbl[spi_get_device_id(spi)->driver_data];
946 if (pdata && pdata->vref_mv)
947 st->int_vref_mv = pdata->vref_mv;
949 st->int_vref_mv = voltage_uv / 1000;
951 st->int_vref_mv = 2500; /* Build-in ref */
953 spi_set_drvdata(spi, indio_dev);
956 indio_dev->dev.parent = &spi->dev;
957 indio_dev->name = spi_get_device_id(spi)->name;
958 indio_dev->modes = INDIO_DIRECT_MODE;
959 indio_dev->channels = st->chip_info->channel;
960 indio_dev->available_scan_masks = st->available_scan_masks;
961 indio_dev->num_channels = 7;
962 indio_dev->info = &ad7793_info;
964 for (i = 0; i < indio_dev->num_channels; i++) {
965 set_bit(i, &st->available_scan_masks[i]);
967 channels[indio_dev->num_channels - 1].scan_index,
968 &st->available_scan_masks[i]);
971 init_waitqueue_head(&st->wq_data_avail);
973 ret = ad7793_register_ring_funcs_and_init(indio_dev);
975 goto error_disable_reg;
977 ret = ad7793_probe_trigger(indio_dev);
979 goto error_unreg_ring;
981 ret = iio_buffer_register(indio_dev,
983 indio_dev->num_channels);
985 goto error_remove_trigger;
987 ret = ad7793_setup(st);
989 goto error_uninitialize_ring;
991 ret = iio_device_register(indio_dev);
993 goto error_uninitialize_ring;
997 error_uninitialize_ring:
998 iio_buffer_unregister(indio_dev);
999 error_remove_trigger:
1000 ad7793_remove_trigger(indio_dev);
1002 ad7793_ring_cleanup(indio_dev);
1004 if (!IS_ERR(st->reg))
1005 regulator_disable(st->reg);
1007 if (!IS_ERR(st->reg))
1008 regulator_put(st->reg);
1010 iio_free_device(indio_dev);
1015 static int ad7793_remove(struct spi_device *spi)
1017 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1018 struct ad7793_state *st = iio_priv(indio_dev);
1020 iio_device_unregister(indio_dev);
1021 iio_buffer_unregister(indio_dev);
1022 ad7793_remove_trigger(indio_dev);
1023 ad7793_ring_cleanup(indio_dev);
1025 if (!IS_ERR(st->reg)) {
1026 regulator_disable(st->reg);
1027 regulator_put(st->reg);
1030 iio_free_device(indio_dev);
1035 static const struct spi_device_id ad7793_id[] = {
1036 {"ad7792", ID_AD7792},
1037 {"ad7793", ID_AD7793},
1040 MODULE_DEVICE_TABLE(spi, ad7793_id);
1042 static struct spi_driver ad7793_driver = {
1045 .owner = THIS_MODULE,
1047 .probe = ad7793_probe,
1048 .remove = __devexit_p(ad7793_remove),
1049 .id_table = ad7793_id,
1052 static int __init ad7793_init(void)
1054 return spi_register_driver(&ad7793_driver);
1056 module_init(ad7793_init);
1058 static void __exit ad7793_exit(void)
1060 spi_unregister_driver(&ad7793_driver);
1062 module_exit(ad7793_exit);
1064 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
1065 MODULE_DESCRIPTION("Analog Devices AD7792/3 ADC");
1066 MODULE_LICENSE("GPL v2");