Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / staging / iio / adc / ad7793.c
bloba831b92cd08250707cdaf1ff0783f19f00cf87ff
1 /*
2 * AD7792/AD7793 SPI ADC driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
7 */
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>
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "../buffer_generic.h"
23 #include "../ring_sw.h"
24 #include "../trigger.h"
25 #include "../trigger_consumer.h"
27 #include "ad7793.h"
29 /* NOTE:
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];
41 struct ad7793_state {
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;
48 bool done;
49 bool irq_dis;
50 u16 int_vref_mv;
51 u16 mode;
52 u16 conf;
53 u32 scale_avail[8][2];
54 /* Note this uses fact that 8 the mask always fits in a long */
55 unsigned long available_scan_masks[7];
57 * DMA (thus cache coherency maintenance) requires the
58 * transfer buffers to live in their own cache lines.
60 u8 data[4] ____cacheline_aligned;
63 enum ad7793_supported_device_ids {
64 ID_AD7792,
65 ID_AD7793,
68 static int __ad7793_write_reg(struct ad7793_state *st, bool locked,
69 bool cs_change, unsigned char reg,
70 unsigned size, unsigned val)
72 u8 *data = st->data;
73 struct spi_transfer t = {
74 .tx_buf = data,
75 .len = size + 1,
76 .cs_change = cs_change,
78 struct spi_message m;
80 data[0] = AD7793_COMM_WRITE | AD7793_COMM_ADDR(reg);
82 switch (size) {
83 case 3:
84 data[1] = val >> 16;
85 data[2] = val >> 8;
86 data[3] = val;
87 break;
88 case 2:
89 data[1] = val >> 8;
90 data[2] = val;
91 break;
92 case 1:
93 data[1] = val;
94 break;
95 default:
96 return -EINVAL;
99 spi_message_init(&m);
100 spi_message_add_tail(&t, &m);
102 if (locked)
103 return spi_sync_locked(st->spi, &m);
104 else
105 return spi_sync(st->spi, &m);
108 static int ad7793_write_reg(struct ad7793_state *st,
109 unsigned reg, unsigned size, unsigned val)
111 return __ad7793_write_reg(st, false, false, reg, size, val);
114 static int __ad7793_read_reg(struct ad7793_state *st, bool locked,
115 bool cs_change, unsigned char reg,
116 int *val, unsigned size)
118 u8 *data = st->data;
119 int ret;
120 struct spi_transfer t[] = {
122 .tx_buf = data,
123 .len = 1,
124 }, {
125 .rx_buf = data,
126 .len = size,
127 .cs_change = cs_change,
130 struct spi_message m;
132 data[0] = AD7793_COMM_READ | AD7793_COMM_ADDR(reg);
134 spi_message_init(&m);
135 spi_message_add_tail(&t[0], &m);
136 spi_message_add_tail(&t[1], &m);
138 if (locked)
139 ret = spi_sync_locked(st->spi, &m);
140 else
141 ret = spi_sync(st->spi, &m);
143 if (ret < 0)
144 return ret;
146 switch (size) {
147 case 3:
148 *val = data[0] << 16 | data[1] << 8 | data[2];
149 break;
150 case 2:
151 *val = data[0] << 8 | data[1];
152 break;
153 case 1:
154 *val = data[0];
155 break;
156 default:
157 return -EINVAL;
160 return 0;
163 static int ad7793_read_reg(struct ad7793_state *st,
164 unsigned reg, int *val, unsigned size)
166 return __ad7793_read_reg(st, 0, 0, reg, val, size);
169 static int ad7793_read(struct ad7793_state *st, unsigned ch,
170 unsigned len, int *val)
172 int ret;
173 st->conf = (st->conf & ~AD7793_CONF_CHAN(-1)) | AD7793_CONF_CHAN(ch);
174 st->mode = (st->mode & ~AD7793_MODE_SEL(-1)) |
175 AD7793_MODE_SEL(AD7793_MODE_SINGLE);
177 ad7793_write_reg(st, AD7793_REG_CONF, sizeof(st->conf), st->conf);
179 spi_bus_lock(st->spi->master);
180 st->done = false;
182 ret = __ad7793_write_reg(st, 1, 1, AD7793_REG_MODE,
183 sizeof(st->mode), st->mode);
184 if (ret < 0)
185 goto out;
187 st->irq_dis = false;
188 enable_irq(st->spi->irq);
189 wait_event_interruptible(st->wq_data_avail, st->done);
191 ret = __ad7793_read_reg(st, 1, 0, AD7793_REG_DATA, val, len);
192 out:
193 spi_bus_unlock(st->spi->master);
195 return ret;
198 static int ad7793_calibrate(struct ad7793_state *st, unsigned mode, unsigned ch)
200 int ret;
202 st->conf = (st->conf & ~AD7793_CONF_CHAN(-1)) | AD7793_CONF_CHAN(ch);
203 st->mode = (st->mode & ~AD7793_MODE_SEL(-1)) | AD7793_MODE_SEL(mode);
205 ad7793_write_reg(st, AD7793_REG_CONF, sizeof(st->conf), st->conf);
207 spi_bus_lock(st->spi->master);
208 st->done = false;
210 ret = __ad7793_write_reg(st, 1, 1, AD7793_REG_MODE,
211 sizeof(st->mode), st->mode);
212 if (ret < 0)
213 goto out;
215 st->irq_dis = false;
216 enable_irq(st->spi->irq);
217 wait_event_interruptible(st->wq_data_avail, st->done);
219 st->mode = (st->mode & ~AD7793_MODE_SEL(-1)) |
220 AD7793_MODE_SEL(AD7793_MODE_IDLE);
222 ret = __ad7793_write_reg(st, 1, 0, AD7793_REG_MODE,
223 sizeof(st->mode), st->mode);
224 out:
225 spi_bus_unlock(st->spi->master);
227 return ret;
230 static const u8 ad7793_calib_arr[6][2] = {
231 {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN1P_AIN1M},
232 {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN1P_AIN1M},
233 {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN2P_AIN2M},
234 {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN2P_AIN2M},
235 {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN3P_AIN3M},
236 {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN3P_AIN3M}
239 static int ad7793_calibrate_all(struct ad7793_state *st)
241 int i, ret;
243 for (i = 0; i < ARRAY_SIZE(ad7793_calib_arr); i++) {
244 ret = ad7793_calibrate(st, ad7793_calib_arr[i][0],
245 ad7793_calib_arr[i][1]);
246 if (ret)
247 goto out;
250 return 0;
251 out:
252 dev_err(&st->spi->dev, "Calibration failed\n");
253 return ret;
256 static int ad7793_setup(struct ad7793_state *st)
258 int i, ret = -1;
259 unsigned long long scale_uv;
260 u32 id;
262 /* reset the serial interface */
263 ret = spi_write(st->spi, (u8 *)&ret, sizeof(ret));
264 if (ret < 0)
265 goto out;
266 msleep(1); /* Wait for at least 500us */
268 /* write/read test for device presence */
269 ret = ad7793_read_reg(st, AD7793_REG_ID, &id, 1);
270 if (ret)
271 goto out;
273 id &= AD7793_ID_MASK;
275 if (!((id == AD7792_ID) || (id == AD7793_ID))) {
276 dev_err(&st->spi->dev, "device ID query failed\n");
277 goto out;
280 st->mode = (st->pdata->mode & ~AD7793_MODE_SEL(-1)) |
281 AD7793_MODE_SEL(AD7793_MODE_IDLE);
282 st->conf = st->pdata->conf & ~AD7793_CONF_CHAN(-1);
284 ret = ad7793_write_reg(st, AD7793_REG_MODE, sizeof(st->mode), st->mode);
285 if (ret)
286 goto out;
288 ret = ad7793_write_reg(st, AD7793_REG_CONF, sizeof(st->conf), st->conf);
289 if (ret)
290 goto out;
292 ret = ad7793_write_reg(st, AD7793_REG_IO,
293 sizeof(st->pdata->io), st->pdata->io);
294 if (ret)
295 goto out;
297 ret = ad7793_calibrate_all(st);
298 if (ret)
299 goto out;
301 /* Populate available ADC input ranges */
302 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
303 scale_uv = ((u64)st->int_vref_mv * 100000000)
304 >> (st->chip_info->channel[0].scan_type.realbits -
305 (!!(st->conf & AD7793_CONF_UNIPOLAR) ? 0 : 1));
306 scale_uv >>= i;
308 st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
309 st->scale_avail[i][0] = scale_uv;
312 return 0;
313 out:
314 dev_err(&st->spi->dev, "setup failed\n");
315 return ret;
318 static int ad7793_scan_from_ring(struct ad7793_state *st, unsigned ch, int *val)
320 struct iio_buffer *ring = iio_priv_to_dev(st)->buffer;
321 int ret;
322 s64 dat64[2];
323 u32 *dat32 = (u32 *)dat64;
325 if (!(test_bit(ch, ring->scan_mask)))
326 return -EBUSY;
328 ret = ring->access->read_last(ring, (u8 *) &dat64);
329 if (ret)
330 return ret;
332 *val = *dat32;
334 return 0;
337 static int ad7793_ring_preenable(struct iio_dev *indio_dev)
339 struct ad7793_state *st = iio_priv(indio_dev);
340 struct iio_buffer *ring = indio_dev->buffer;
341 size_t d_size;
342 unsigned channel;
344 if (!ring->scan_count)
345 return -EINVAL;
347 channel = find_first_bit(ring->scan_mask,
348 indio_dev->masklength);
350 d_size = ring->scan_count *
351 indio_dev->channels[0].scan_type.storagebits / 8;
353 if (ring->scan_timestamp) {
354 d_size += sizeof(s64);
356 if (d_size % sizeof(s64))
357 d_size += sizeof(s64) - (d_size % sizeof(s64));
360 if (indio_dev->buffer->access->set_bytes_per_datum)
361 indio_dev->buffer->access->
362 set_bytes_per_datum(indio_dev->buffer, d_size);
364 st->mode = (st->mode & ~AD7793_MODE_SEL(-1)) |
365 AD7793_MODE_SEL(AD7793_MODE_CONT);
366 st->conf = (st->conf & ~AD7793_CONF_CHAN(-1)) |
367 AD7793_CONF_CHAN(indio_dev->channels[channel].address);
369 ad7793_write_reg(st, AD7793_REG_CONF, sizeof(st->conf), st->conf);
371 spi_bus_lock(st->spi->master);
372 __ad7793_write_reg(st, 1, 1, AD7793_REG_MODE,
373 sizeof(st->mode), st->mode);
375 st->irq_dis = false;
376 enable_irq(st->spi->irq);
378 return 0;
381 static int ad7793_ring_postdisable(struct iio_dev *indio_dev)
383 struct ad7793_state *st = iio_priv(indio_dev);
385 st->mode = (st->mode & ~AD7793_MODE_SEL(-1)) |
386 AD7793_MODE_SEL(AD7793_MODE_IDLE);
388 st->done = false;
389 wait_event_interruptible(st->wq_data_avail, st->done);
391 if (!st->irq_dis)
392 disable_irq_nosync(st->spi->irq);
394 __ad7793_write_reg(st, 1, 0, AD7793_REG_MODE,
395 sizeof(st->mode), st->mode);
397 return spi_bus_unlock(st->spi->master);
401 * ad7793_trigger_handler() bh of trigger launched polling to ring buffer
404 static irqreturn_t ad7793_trigger_handler(int irq, void *p)
406 struct iio_poll_func *pf = p;
407 struct iio_dev *indio_dev = pf->indio_dev;
408 struct iio_buffer *ring = indio_dev->buffer;
409 struct ad7793_state *st = iio_priv(indio_dev);
410 s64 dat64[2];
411 s32 *dat32 = (s32 *)dat64;
413 if (ring->scan_count)
414 __ad7793_read_reg(st, 1, 1, AD7793_REG_DATA,
415 dat32,
416 indio_dev->channels[0].scan_type.realbits/8);
418 /* Guaranteed to be aligned with 8 byte boundary */
419 if (ring->scan_timestamp)
420 dat64[1] = pf->timestamp;
422 ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
424 iio_trigger_notify_done(indio_dev->trig);
425 st->irq_dis = false;
426 enable_irq(st->spi->irq);
428 return IRQ_HANDLED;
431 static const struct iio_buffer_setup_ops ad7793_ring_setup_ops = {
432 .preenable = &ad7793_ring_preenable,
433 .postenable = &iio_triggered_buffer_postenable,
434 .predisable = &iio_triggered_buffer_predisable,
435 .postdisable = &ad7793_ring_postdisable,
438 static int ad7793_register_ring_funcs_and_init(struct iio_dev *indio_dev)
440 int ret;
442 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
443 if (!indio_dev->buffer) {
444 ret = -ENOMEM;
445 goto error_ret;
447 /* Effectively select the ring buffer implementation */
448 indio_dev->buffer->access = &ring_sw_access_funcs;
449 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
450 &ad7793_trigger_handler,
451 IRQF_ONESHOT,
452 indio_dev,
453 "ad7793_consumer%d",
454 indio_dev->id);
455 if (indio_dev->pollfunc == NULL) {
456 ret = -ENOMEM;
457 goto error_deallocate_sw_rb;
460 /* Ring buffer functions - here trigger setup related */
461 indio_dev->buffer->setup_ops = &ad7793_ring_setup_ops;
463 /* Flag that polled ring buffering is possible */
464 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
465 return 0;
467 error_deallocate_sw_rb:
468 iio_sw_rb_free(indio_dev->buffer);
469 error_ret:
470 return ret;
473 static void ad7793_ring_cleanup(struct iio_dev *indio_dev)
475 iio_dealloc_pollfunc(indio_dev->pollfunc);
476 iio_sw_rb_free(indio_dev->buffer);
480 * ad7793_data_rdy_trig_poll() the event handler for the data rdy trig
482 static irqreturn_t ad7793_data_rdy_trig_poll(int irq, void *private)
484 struct ad7793_state *st = iio_priv(private);
486 st->done = true;
487 wake_up_interruptible(&st->wq_data_avail);
488 disable_irq_nosync(irq);
489 st->irq_dis = true;
490 iio_trigger_poll(st->trig, iio_get_time_ns());
492 return IRQ_HANDLED;
495 static int ad7793_probe_trigger(struct iio_dev *indio_dev)
497 struct ad7793_state *st = iio_priv(indio_dev);
498 int ret;
500 st->trig = iio_allocate_trigger("%s-dev%d",
501 spi_get_device_id(st->spi)->name,
502 indio_dev->id);
503 if (st->trig == NULL) {
504 ret = -ENOMEM;
505 goto error_ret;
508 ret = request_irq(st->spi->irq,
509 ad7793_data_rdy_trig_poll,
510 IRQF_TRIGGER_LOW,
511 spi_get_device_id(st->spi)->name,
512 indio_dev);
513 if (ret)
514 goto error_free_trig;
516 disable_irq_nosync(st->spi->irq);
517 st->irq_dis = true;
518 st->trig->dev.parent = &st->spi->dev;
519 st->trig->owner = THIS_MODULE;
520 st->trig->private_data = indio_dev;
522 ret = iio_trigger_register(st->trig);
524 /* select default trigger */
525 indio_dev->trig = st->trig;
526 if (ret)
527 goto error_free_irq;
529 return 0;
531 error_free_irq:
532 free_irq(st->spi->irq, indio_dev);
533 error_free_trig:
534 iio_free_trigger(st->trig);
535 error_ret:
536 return ret;
539 static void ad7793_remove_trigger(struct iio_dev *indio_dev)
541 struct ad7793_state *st = iio_priv(indio_dev);
543 iio_trigger_unregister(st->trig);
544 free_irq(st->spi->irq, indio_dev);
545 iio_free_trigger(st->trig);
548 static const u16 sample_freq_avail[16] = {0, 470, 242, 123, 62, 50, 39, 33, 19,
549 17, 16, 12, 10, 8, 6, 4};
551 static ssize_t ad7793_read_frequency(struct device *dev,
552 struct device_attribute *attr,
553 char *buf)
555 struct iio_dev *indio_dev = dev_get_drvdata(dev);
556 struct ad7793_state *st = iio_priv(indio_dev);
558 return sprintf(buf, "%d\n",
559 sample_freq_avail[AD7793_MODE_RATE(st->mode)]);
562 static ssize_t ad7793_write_frequency(struct device *dev,
563 struct device_attribute *attr,
564 const char *buf,
565 size_t len)
567 struct iio_dev *indio_dev = dev_get_drvdata(dev);
568 struct ad7793_state *st = iio_priv(indio_dev);
569 long lval;
570 int i, ret;
572 mutex_lock(&indio_dev->mlock);
573 if (iio_buffer_enabled(indio_dev)) {
574 mutex_unlock(&indio_dev->mlock);
575 return -EBUSY;
577 mutex_unlock(&indio_dev->mlock);
579 ret = strict_strtol(buf, 10, &lval);
580 if (ret)
581 return ret;
583 ret = -EINVAL;
585 for (i = 0; i < ARRAY_SIZE(sample_freq_avail); i++)
586 if (lval == sample_freq_avail[i]) {
587 mutex_lock(&indio_dev->mlock);
588 st->mode &= ~AD7793_MODE_RATE(-1);
589 st->mode |= AD7793_MODE_RATE(i);
590 ad7793_write_reg(st, AD7793_REG_MODE,
591 sizeof(st->mode), st->mode);
592 mutex_unlock(&indio_dev->mlock);
593 ret = 0;
596 return ret ? ret : len;
599 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
600 ad7793_read_frequency,
601 ad7793_write_frequency);
603 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
604 "470 242 123 62 50 39 33 19 17 16 12 10 8 6 4");
606 static ssize_t ad7793_show_scale_available(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 struct iio_dev *indio_dev = dev_get_drvdata(dev);
610 struct ad7793_state *st = iio_priv(indio_dev);
611 int i, len = 0;
613 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
614 len += sprintf(buf + len, "%d.%09u ", st->scale_avail[i][0],
615 st->scale_avail[i][1]);
617 len += sprintf(buf + len, "\n");
619 return len;
622 static IIO_DEVICE_ATTR_NAMED(in_m_in_scale_available, in-in_scale_available,
623 S_IRUGO, ad7793_show_scale_available, NULL, 0);
625 static struct attribute *ad7793_attributes[] = {
626 &iio_dev_attr_sampling_frequency.dev_attr.attr,
627 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
628 &iio_dev_attr_in_m_in_scale_available.dev_attr.attr,
629 NULL
632 static const struct attribute_group ad7793_attribute_group = {
633 .attrs = ad7793_attributes,
636 static int ad7793_read_raw(struct iio_dev *indio_dev,
637 struct iio_chan_spec const *chan,
638 int *val,
639 int *val2,
640 long m)
642 struct ad7793_state *st = iio_priv(indio_dev);
643 int ret, smpl = 0;
644 unsigned long long scale_uv;
645 bool unipolar = !!(st->conf & AD7793_CONF_UNIPOLAR);
647 switch (m) {
648 case 0:
649 mutex_lock(&indio_dev->mlock);
650 if (iio_buffer_enabled(indio_dev))
651 ret = ad7793_scan_from_ring(st,
652 chan->scan_index, &smpl);
653 else
654 ret = ad7793_read(st, chan->address,
655 chan->scan_type.realbits / 8, &smpl);
656 mutex_unlock(&indio_dev->mlock);
658 if (ret < 0)
659 return ret;
661 *val = (smpl >> chan->scan_type.shift) &
662 ((1 << (chan->scan_type.realbits)) - 1);
664 if (!unipolar)
665 *val -= (1 << (chan->scan_type.realbits - 1));
667 return IIO_VAL_INT;
669 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
670 *val = st->scale_avail[(st->conf >> 8) & 0x7][0];
671 *val2 = st->scale_avail[(st->conf >> 8) & 0x7][1];
673 return IIO_VAL_INT_PLUS_NANO;
675 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
676 switch (chan->type) {
677 case IIO_VOLTAGE:
678 /* 1170mV / 2^23 * 6 */
679 scale_uv = (1170ULL * 100000000ULL * 6ULL)
680 >> (chan->scan_type.realbits -
681 (unipolar ? 0 : 1));
682 break;
683 case IIO_TEMP:
684 /* Always uses unity gain and internal ref */
685 scale_uv = (2500ULL * 100000000ULL)
686 >> (chan->scan_type.realbits -
687 (unipolar ? 0 : 1));
688 break;
689 default:
690 return -EINVAL;
693 *val2 = do_div(scale_uv, 100000000) * 10;
694 *val = scale_uv;
696 return IIO_VAL_INT_PLUS_NANO;
698 return -EINVAL;
701 static int ad7793_write_raw(struct iio_dev *indio_dev,
702 struct iio_chan_spec const *chan,
703 int val,
704 int val2,
705 long mask)
707 struct ad7793_state *st = iio_priv(indio_dev);
708 int ret, i;
709 unsigned int tmp;
711 mutex_lock(&indio_dev->mlock);
712 if (iio_buffer_enabled(indio_dev)) {
713 mutex_unlock(&indio_dev->mlock);
714 return -EBUSY;
717 switch (mask) {
718 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
719 ret = -EINVAL;
720 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
721 if (val2 == st->scale_avail[i][1]) {
722 tmp = st->conf;
723 st->conf &= ~AD7793_CONF_GAIN(-1);
724 st->conf |= AD7793_CONF_GAIN(i);
726 if (tmp != st->conf) {
727 ad7793_write_reg(st, AD7793_REG_CONF,
728 sizeof(st->conf),
729 st->conf);
730 ad7793_calibrate_all(st);
732 ret = 0;
735 default:
736 ret = -EINVAL;
739 mutex_unlock(&indio_dev->mlock);
740 return ret;
743 static int ad7793_validate_trigger(struct iio_dev *indio_dev,
744 struct iio_trigger *trig)
746 if (indio_dev->trig != trig)
747 return -EINVAL;
749 return 0;
752 static int ad7793_write_raw_get_fmt(struct iio_dev *indio_dev,
753 struct iio_chan_spec const *chan,
754 long mask)
756 return IIO_VAL_INT_PLUS_NANO;
759 static const struct iio_info ad7793_info = {
760 .read_raw = &ad7793_read_raw,
761 .write_raw = &ad7793_write_raw,
762 .write_raw_get_fmt = &ad7793_write_raw_get_fmt,
763 .attrs = &ad7793_attribute_group,
764 .validate_trigger = ad7793_validate_trigger,
765 .driver_module = THIS_MODULE,
768 static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
769 [ID_AD7793] = {
770 .channel[0] = {
771 .type = IIO_VOLTAGE,
772 .differential = 1,
773 .indexed = 1,
774 .channel = 0,
775 .channel2 = 0,
776 .address = AD7793_CH_AIN1P_AIN1M,
777 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
778 .scan_index = 0,
779 .scan_type = IIO_ST('s', 24, 32, 0)
781 .channel[1] = {
782 .type = IIO_VOLTAGE,
783 .differential = 1,
784 .indexed = 1,
785 .channel = 1,
786 .channel2 = 1,
787 .address = AD7793_CH_AIN2P_AIN2M,
788 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
789 .scan_index = 1,
790 .scan_type = IIO_ST('s', 24, 32, 0)
792 .channel[2] = {
793 .type = IIO_VOLTAGE,
794 .differential = 1,
795 .indexed = 1,
796 .channel = 2,
797 .channel2 = 2,
798 .address = AD7793_CH_AIN3P_AIN3M,
799 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
800 .scan_index = 2,
801 .scan_type = IIO_ST('s', 24, 32, 0)
803 .channel[3] = {
804 .type = IIO_VOLTAGE,
805 .differential = 1,
806 .extend_name = "shorted",
807 .indexed = 1,
808 .channel = 2,
809 .channel2 = 2,
810 .address = AD7793_CH_AIN1M_AIN1M,
811 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
812 .scan_index = 2,
813 .scan_type = IIO_ST('s', 24, 32, 0)
815 .channel[4] = {
816 .type = IIO_TEMP,
817 .indexed = 1,
818 .channel = 0,
819 .address = AD7793_CH_TEMP,
820 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
821 .scan_index = 4,
822 .scan_type = IIO_ST('s', 24, 32, 0),
824 .channel[5] = {
825 .type = IIO_VOLTAGE,
826 .extend_name = "supply",
827 .indexed = 1,
828 .channel = 4,
829 .address = AD7793_CH_AVDD_MONITOR,
830 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
831 .scan_index = 5,
832 .scan_type = IIO_ST('s', 24, 32, 0),
834 .channel[6] = IIO_CHAN_SOFT_TIMESTAMP(6),
836 [ID_AD7792] = {
837 .channel[0] = {
838 .type = IIO_VOLTAGE,
839 .differential = 1,
840 .indexed = 1,
841 .channel = 0,
842 .channel2 = 0,
843 .address = AD7793_CH_AIN1P_AIN1M,
844 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
845 .scan_index = 0,
846 .scan_type = IIO_ST('s', 16, 32, 0)
848 .channel[1] = {
849 .type = IIO_VOLTAGE,
850 .differential = 1,
851 .indexed = 1,
852 .channel = 1,
853 .channel2 = 1,
854 .address = AD7793_CH_AIN2P_AIN2M,
855 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
856 .scan_index = 1,
857 .scan_type = IIO_ST('s', 16, 32, 0)
859 .channel[2] = {
860 .type = IIO_VOLTAGE,
861 .differential = 1,
862 .indexed = 1,
863 .channel = 2,
864 .channel2 = 2,
865 .address = AD7793_CH_AIN3P_AIN3M,
866 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
867 .scan_index = 2,
868 .scan_type = IIO_ST('s', 16, 32, 0)
870 .channel[3] = {
871 .type = IIO_VOLTAGE,
872 .differential = 1,
873 .extend_name = "shorted",
874 .indexed = 1,
875 .channel = 2,
876 .channel2 = 2,
877 .address = AD7793_CH_AIN1M_AIN1M,
878 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
879 .scan_index = 2,
880 .scan_type = IIO_ST('s', 16, 32, 0)
882 .channel[4] = {
883 .type = IIO_TEMP,
884 .indexed = 1,
885 .channel = 0,
886 .address = AD7793_CH_TEMP,
887 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
888 .scan_index = 4,
889 .scan_type = IIO_ST('s', 16, 32, 0),
891 .channel[5] = {
892 .type = IIO_VOLTAGE,
893 .extend_name = "supply",
894 .indexed = 1,
895 .channel = 4,
896 .address = AD7793_CH_AVDD_MONITOR,
897 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
898 .scan_index = 5,
899 .scan_type = IIO_ST('s', 16, 32, 0),
901 .channel[6] = IIO_CHAN_SOFT_TIMESTAMP(6),
905 static int __devinit ad7793_probe(struct spi_device *spi)
907 struct ad7793_platform_data *pdata = spi->dev.platform_data;
908 struct ad7793_state *st;
909 struct iio_dev *indio_dev;
910 int ret, i, voltage_uv = 0;
912 if (!pdata) {
913 dev_err(&spi->dev, "no platform data?\n");
914 return -ENODEV;
917 if (!spi->irq) {
918 dev_err(&spi->dev, "no IRQ?\n");
919 return -ENODEV;
922 indio_dev = iio_allocate_device(sizeof(*st));
923 if (indio_dev == NULL)
924 return -ENOMEM;
926 st = iio_priv(indio_dev);
928 st->reg = regulator_get(&spi->dev, "vcc");
929 if (!IS_ERR(st->reg)) {
930 ret = regulator_enable(st->reg);
931 if (ret)
932 goto error_put_reg;
934 voltage_uv = regulator_get_voltage(st->reg);
937 st->chip_info =
938 &ad7793_chip_info_tbl[spi_get_device_id(spi)->driver_data];
940 st->pdata = pdata;
942 if (pdata && pdata->vref_mv)
943 st->int_vref_mv = pdata->vref_mv;
944 else if (voltage_uv)
945 st->int_vref_mv = voltage_uv / 1000;
946 else
947 st->int_vref_mv = 2500; /* Build-in ref */
949 spi_set_drvdata(spi, indio_dev);
950 st->spi = spi;
952 indio_dev->dev.parent = &spi->dev;
953 indio_dev->name = spi_get_device_id(spi)->name;
954 indio_dev->modes = INDIO_DIRECT_MODE;
955 indio_dev->channels = st->chip_info->channel;
956 indio_dev->available_scan_masks = st->available_scan_masks;
957 indio_dev->num_channels = 7;
958 indio_dev->info = &ad7793_info;
960 for (i = 0; i < indio_dev->num_channels; i++) {
961 set_bit(i, &st->available_scan_masks[i]);
962 set_bit(indio_dev->
963 channels[indio_dev->num_channels - 1].scan_index,
964 &st->available_scan_masks[i]);
967 init_waitqueue_head(&st->wq_data_avail);
969 ret = ad7793_register_ring_funcs_and_init(indio_dev);
970 if (ret)
971 goto error_disable_reg;
973 ret = ad7793_probe_trigger(indio_dev);
974 if (ret)
975 goto error_unreg_ring;
977 ret = iio_buffer_register(indio_dev,
978 indio_dev->channels,
979 indio_dev->num_channels);
980 if (ret)
981 goto error_remove_trigger;
983 ret = ad7793_setup(st);
984 if (ret)
985 goto error_uninitialize_ring;
987 ret = iio_device_register(indio_dev);
988 if (ret)
989 goto error_uninitialize_ring;
991 return 0;
993 error_uninitialize_ring:
994 iio_buffer_unregister(indio_dev);
995 error_remove_trigger:
996 ad7793_remove_trigger(indio_dev);
997 error_unreg_ring:
998 ad7793_ring_cleanup(indio_dev);
999 error_disable_reg:
1000 if (!IS_ERR(st->reg))
1001 regulator_disable(st->reg);
1002 error_put_reg:
1003 if (!IS_ERR(st->reg))
1004 regulator_put(st->reg);
1006 iio_free_device(indio_dev);
1008 return ret;
1011 static int ad7793_remove(struct spi_device *spi)
1013 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1014 struct ad7793_state *st = iio_priv(indio_dev);
1016 iio_device_unregister(indio_dev);
1017 iio_buffer_unregister(indio_dev);
1018 ad7793_remove_trigger(indio_dev);
1019 ad7793_ring_cleanup(indio_dev);
1021 if (!IS_ERR(st->reg)) {
1022 regulator_disable(st->reg);
1023 regulator_put(st->reg);
1026 iio_free_device(indio_dev);
1028 return 0;
1031 static const struct spi_device_id ad7793_id[] = {
1032 {"ad7792", ID_AD7792},
1033 {"ad7793", ID_AD7793},
1037 static struct spi_driver ad7793_driver = {
1038 .driver = {
1039 .name = "ad7793",
1040 .bus = &spi_bus_type,
1041 .owner = THIS_MODULE,
1043 .probe = ad7793_probe,
1044 .remove = __devexit_p(ad7793_remove),
1045 .id_table = ad7793_id,
1048 static int __init ad7793_init(void)
1050 return spi_register_driver(&ad7793_driver);
1052 module_init(ad7793_init);
1054 static void __exit ad7793_exit(void)
1056 spi_unregister_driver(&ad7793_driver);
1058 module_exit(ad7793_exit);
1060 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
1061 MODULE_DESCRIPTION("Analog Devices AD7792/3 ADC");
1062 MODULE_LICENSE("GPL v2");