2 * AD7190 AD7192 AD7195 SPI ADC driver
4 * Copyright 2011-2012 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>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
30 #define AD7192_REG_COMM 0 /* Communications Register (WO, 8-bit) */
31 #define AD7192_REG_STAT 0 /* Status Register (RO, 8-bit) */
32 #define AD7192_REG_MODE 1 /* Mode Register (RW, 24-bit */
33 #define AD7192_REG_CONF 2 /* Configuration Register (RW, 24-bit) */
34 #define AD7192_REG_DATA 3 /* Data Register (RO, 24/32-bit) */
35 #define AD7192_REG_ID 4 /* ID Register (RO, 8-bit) */
36 #define AD7192_REG_GPOCON 5 /* GPOCON Register (RO, 8-bit) */
37 #define AD7192_REG_OFFSET 6 /* Offset Register (RW, 16-bit
38 * (AD7792)/24-bit (AD7192)) */
39 #define AD7192_REG_FULLSALE 7 /* Full-Scale Register
40 * (RW, 16-bit (AD7792)/24-bit (AD7192)) */
42 /* Communications Register Bit Designations (AD7192_REG_COMM) */
43 #define AD7192_COMM_WEN (1 << 7) /* Write Enable */
44 #define AD7192_COMM_WRITE (0 << 6) /* Write Operation */
45 #define AD7192_COMM_READ (1 << 6) /* Read Operation */
46 #define AD7192_COMM_ADDR(x) (((x) & 0x7) << 3) /* Register Address */
47 #define AD7192_COMM_CREAD (1 << 2) /* Continuous Read of Data Register */
49 /* Status Register Bit Designations (AD7192_REG_STAT) */
50 #define AD7192_STAT_RDY (1 << 7) /* Ready */
51 #define AD7192_STAT_ERR (1 << 6) /* Error (Overrange, Underrange) */
52 #define AD7192_STAT_NOREF (1 << 5) /* Error no external reference */
53 #define AD7192_STAT_PARITY (1 << 4) /* Parity */
54 #define AD7192_STAT_CH3 (1 << 2) /* Channel 3 */
55 #define AD7192_STAT_CH2 (1 << 1) /* Channel 2 */
56 #define AD7192_STAT_CH1 (1 << 0) /* Channel 1 */
58 /* Mode Register Bit Designations (AD7192_REG_MODE) */
59 #define AD7192_MODE_SEL(x) (((x) & 0x7) << 21) /* Operation Mode Select */
60 #define AD7192_MODE_DAT_STA (1 << 20) /* Status Register transmission */
61 #define AD7192_MODE_CLKSRC(x) (((x) & 0x3) << 18) /* Clock Source Select */
62 #define AD7192_MODE_SINC3 (1 << 15) /* SINC3 Filter Select */
63 #define AD7192_MODE_ACX (1 << 14) /* AC excitation enable(AD7195 only)*/
64 #define AD7192_MODE_ENPAR (1 << 13) /* Parity Enable */
65 #define AD7192_MODE_CLKDIV (1 << 12) /* Clock divide by 2 (AD7190/2 only)*/
66 #define AD7192_MODE_SCYCLE (1 << 11) /* Single cycle conversion */
67 #define AD7192_MODE_REJ60 (1 << 10) /* 50/60Hz notch filter */
68 #define AD7192_MODE_RATE(x) ((x) & 0x3FF) /* Filter Update Rate Select */
70 /* Mode Register: AD7192_MODE_SEL options */
71 #define AD7192_MODE_CONT 0 /* Continuous Conversion Mode */
72 #define AD7192_MODE_SINGLE 1 /* Single Conversion Mode */
73 #define AD7192_MODE_IDLE 2 /* Idle Mode */
74 #define AD7192_MODE_PWRDN 3 /* Power-Down Mode */
75 #define AD7192_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */
76 #define AD7192_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */
77 #define AD7192_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */
78 #define AD7192_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */
80 /* Mode Register: AD7192_MODE_CLKSRC options */
81 #define AD7192_CLK_EXT_MCLK1_2 0 /* External 4.92 MHz Clock connected
82 * from MCLK1 to MCLK2 */
83 #define AD7192_CLK_EXT_MCLK2 1 /* External Clock applied to MCLK2 */
84 #define AD7192_CLK_INT 2 /* Internal 4.92 MHz Clock not
85 * available at the MCLK2 pin */
86 #define AD7192_CLK_INT_CO 3 /* Internal 4.92 MHz Clock available
90 /* Configuration Register Bit Designations (AD7192_REG_CONF) */
92 #define AD7192_CONF_CHOP (1 << 23) /* CHOP enable */
93 #define AD7192_CONF_REFSEL (1 << 20) /* REFIN1/REFIN2 Reference Select */
94 #define AD7192_CONF_CHAN(x) (((x) & 0xFF) << 8) /* Channel select */
95 #define AD7192_CONF_BURN (1 << 7) /* Burnout current enable */
96 #define AD7192_CONF_REFDET (1 << 6) /* Reference detect enable */
97 #define AD7192_CONF_BUF (1 << 4) /* Buffered Mode Enable */
98 #define AD7192_CONF_UNIPOLAR (1 << 3) /* Unipolar/Bipolar Enable */
99 #define AD7192_CONF_GAIN(x) ((x) & 0x7) /* Gain Select */
101 #define AD7192_CH_AIN1P_AIN2M 0 /* AIN1(+) - AIN2(-) */
102 #define AD7192_CH_AIN3P_AIN4M 1 /* AIN3(+) - AIN4(-) */
103 #define AD7192_CH_TEMP 2 /* Temp Sensor */
104 #define AD7192_CH_AIN2P_AIN2M 3 /* AIN2(+) - AIN2(-) */
105 #define AD7192_CH_AIN1 4 /* AIN1 - AINCOM */
106 #define AD7192_CH_AIN2 5 /* AIN2 - AINCOM */
107 #define AD7192_CH_AIN3 6 /* AIN3 - AINCOM */
108 #define AD7192_CH_AIN4 7 /* AIN4 - AINCOM */
110 /* ID Register Bit Designations (AD7192_REG_ID) */
111 #define ID_AD7190 0x4
112 #define ID_AD7192 0x0
113 #define ID_AD7195 0x6
114 #define AD7192_ID_MASK 0x0F
116 /* GPOCON Register Bit Designations (AD7192_REG_GPOCON) */
117 #define AD7192_GPOCON_BPDSW (1 << 6) /* Bridge power-down switch enable */
118 #define AD7192_GPOCON_GP32EN (1 << 5) /* Digital Output P3 and P2 enable */
119 #define AD7192_GPOCON_GP10EN (1 << 4) /* Digital Output P1 and P0 enable */
120 #define AD7192_GPOCON_P3DAT (1 << 3) /* P3 state */
121 #define AD7192_GPOCON_P2DAT (1 << 2) /* P2 state */
122 #define AD7192_GPOCON_P1DAT (1 << 1) /* P1 state */
123 #define AD7192_GPOCON_P0DAT (1 << 0) /* P0 state */
125 #define AD7192_INT_FREQ_MHz 4915200
128 * The AD7190/2/5 features a dual use data out ready DOUT/RDY output.
129 * In order to avoid contentions on the SPI bus, it's therefore necessary
130 * to use spi bus locking.
132 * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
135 struct ad7192_state {
136 struct spi_device *spi;
137 struct iio_trigger *trig;
138 struct regulator *reg;
139 struct ad7192_platform_data *pdata;
140 wait_queue_head_t wq_data_avail;
148 u32 scale_avail[8][2];
152 * DMA (thus cache coherency maintenance) requires the
153 * transfer buffers to live in their own cache lines.
155 u8 data[4] ____cacheline_aligned;
158 static int __ad7192_write_reg(struct ad7192_state *st, bool locked,
159 bool cs_change, unsigned char reg,
160 unsigned size, unsigned val)
163 struct spi_transfer t = {
166 .cs_change = cs_change,
168 struct spi_message m;
170 data[0] = AD7192_COMM_WRITE | AD7192_COMM_ADDR(reg);
189 spi_message_init(&m);
190 spi_message_add_tail(&t, &m);
193 return spi_sync_locked(st->spi, &m);
195 return spi_sync(st->spi, &m);
198 static int ad7192_write_reg(struct ad7192_state *st,
199 unsigned reg, unsigned size, unsigned val)
201 return __ad7192_write_reg(st, false, false, reg, size, val);
204 static int __ad7192_read_reg(struct ad7192_state *st, bool locked,
205 bool cs_change, unsigned char reg,
206 int *val, unsigned size)
210 struct spi_transfer t[] = {
217 .cs_change = cs_change,
220 struct spi_message m;
222 data[0] = AD7192_COMM_READ | AD7192_COMM_ADDR(reg);
224 spi_message_init(&m);
225 spi_message_add_tail(&t[0], &m);
226 spi_message_add_tail(&t[1], &m);
229 ret = spi_sync_locked(st->spi, &m);
231 ret = spi_sync(st->spi, &m);
238 *val = data[0] << 16 | data[1] << 8 | data[2];
241 *val = data[0] << 8 | data[1];
253 static int ad7192_read_reg(struct ad7192_state *st,
254 unsigned reg, int *val, unsigned size)
256 return __ad7192_read_reg(st, 0, 0, reg, val, size);
259 static int ad7192_read(struct ad7192_state *st, unsigned ch,
260 unsigned len, int *val)
263 st->conf = (st->conf & ~AD7192_CONF_CHAN(-1)) |
264 AD7192_CONF_CHAN(1 << ch);
265 st->mode = (st->mode & ~AD7192_MODE_SEL(-1)) |
266 AD7192_MODE_SEL(AD7192_MODE_SINGLE);
268 ad7192_write_reg(st, AD7192_REG_CONF, 3, st->conf);
270 spi_bus_lock(st->spi->master);
273 ret = __ad7192_write_reg(st, 1, 1, AD7192_REG_MODE, 3, st->mode);
278 enable_irq(st->spi->irq);
279 wait_event_interruptible(st->wq_data_avail, st->done);
281 ret = __ad7192_read_reg(st, 1, 0, AD7192_REG_DATA, val, len);
283 spi_bus_unlock(st->spi->master);
288 static int ad7192_calibrate(struct ad7192_state *st, unsigned mode, unsigned ch)
292 st->conf = (st->conf & ~AD7192_CONF_CHAN(-1)) |
293 AD7192_CONF_CHAN(1 << ch);
294 st->mode = (st->mode & ~AD7192_MODE_SEL(-1)) | AD7192_MODE_SEL(mode);
296 ad7192_write_reg(st, AD7192_REG_CONF, 3, st->conf);
298 spi_bus_lock(st->spi->master);
301 ret = __ad7192_write_reg(st, 1, 1, AD7192_REG_MODE, 3,
302 (st->devid != ID_AD7195) ?
303 st->mode | AD7192_MODE_CLKDIV :
309 enable_irq(st->spi->irq);
310 wait_event_interruptible(st->wq_data_avail, st->done);
312 st->mode = (st->mode & ~AD7192_MODE_SEL(-1)) |
313 AD7192_MODE_SEL(AD7192_MODE_IDLE);
315 ret = __ad7192_write_reg(st, 1, 0, AD7192_REG_MODE, 3, st->mode);
317 spi_bus_unlock(st->spi->master);
322 static const u8 ad7192_calib_arr[8][2] = {
323 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN1},
324 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN1},
325 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN2},
326 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN2},
327 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN3},
328 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN3},
329 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN4},
330 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN4}
333 static int ad7192_calibrate_all(struct ad7192_state *st)
337 for (i = 0; i < ARRAY_SIZE(ad7192_calib_arr); i++) {
338 ret = ad7192_calibrate(st, ad7192_calib_arr[i][0],
339 ad7192_calib_arr[i][1]);
346 dev_err(&st->spi->dev, "Calibration failed\n");
350 static int ad7192_setup(struct ad7192_state *st)
352 struct iio_dev *indio_dev = spi_get_drvdata(st->spi);
353 struct ad7192_platform_data *pdata = st->pdata;
354 unsigned long long scale_uv;
358 /* reset the serial interface */
359 memset(&ones, 0xFF, 6);
360 ret = spi_write(st->spi, &ones, 6);
363 msleep(1); /* Wait for at least 500us */
365 /* write/read test for device presence */
366 ret = ad7192_read_reg(st, AD7192_REG_ID, &id, 1);
370 id &= AD7192_ID_MASK;
373 dev_warn(&st->spi->dev, "device ID query failed (0x%X)\n", id);
375 switch (pdata->clock_source_sel) {
376 case AD7192_CLK_EXT_MCLK1_2:
377 case AD7192_CLK_EXT_MCLK2:
378 st->mclk = AD7192_INT_FREQ_MHz;
381 case AD7192_CLK_INT_CO:
382 if (pdata->ext_clk_Hz)
383 st->mclk = pdata->ext_clk_Hz;
385 st->mclk = AD7192_INT_FREQ_MHz;
392 st->mode = AD7192_MODE_SEL(AD7192_MODE_IDLE) |
393 AD7192_MODE_CLKSRC(pdata->clock_source_sel) |
394 AD7192_MODE_RATE(480);
396 st->conf = AD7192_CONF_GAIN(0);
399 st->mode |= AD7192_MODE_REJ60;
402 st->mode |= AD7192_MODE_SINC3;
404 if (pdata->refin2_en && (st->devid != ID_AD7195))
405 st->conf |= AD7192_CONF_REFSEL;
407 if (pdata->chop_en) {
408 st->conf |= AD7192_CONF_CHOP;
410 st->f_order = 3; /* SINC 3rd order */
412 st->f_order = 4; /* SINC 4th order */
418 st->conf |= AD7192_CONF_BUF;
420 if (pdata->unipolar_en)
421 st->conf |= AD7192_CONF_UNIPOLAR;
423 if (pdata->burnout_curr_en)
424 st->conf |= AD7192_CONF_BURN;
426 ret = ad7192_write_reg(st, AD7192_REG_MODE, 3, st->mode);
430 ret = ad7192_write_reg(st, AD7192_REG_CONF, 3, st->conf);
434 ret = ad7192_calibrate_all(st);
438 /* Populate available ADC input ranges */
439 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
440 scale_uv = ((u64)st->int_vref_mv * 100000000)
441 >> (indio_dev->channels[0].scan_type.realbits -
442 ((st->conf & AD7192_CONF_UNIPOLAR) ? 0 : 1));
445 st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
446 st->scale_avail[i][0] = scale_uv;
451 dev_err(&st->spi->dev, "setup failed\n");
455 static int ad7192_ring_preenable(struct iio_dev *indio_dev)
457 struct ad7192_state *st = iio_priv(indio_dev);
461 if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
464 ret = iio_sw_buffer_preenable(indio_dev);
468 channel = find_first_bit(indio_dev->active_scan_mask,
469 indio_dev->masklength);
471 st->mode = (st->mode & ~AD7192_MODE_SEL(-1)) |
472 AD7192_MODE_SEL(AD7192_MODE_CONT);
473 st->conf = (st->conf & ~AD7192_CONF_CHAN(-1)) |
474 AD7192_CONF_CHAN(1 << indio_dev->channels[channel].address);
476 ad7192_write_reg(st, AD7192_REG_CONF, 3, st->conf);
478 spi_bus_lock(st->spi->master);
479 __ad7192_write_reg(st, 1, 1, AD7192_REG_MODE, 3, st->mode);
482 enable_irq(st->spi->irq);
487 static int ad7192_ring_postdisable(struct iio_dev *indio_dev)
489 struct ad7192_state *st = iio_priv(indio_dev);
491 st->mode = (st->mode & ~AD7192_MODE_SEL(-1)) |
492 AD7192_MODE_SEL(AD7192_MODE_IDLE);
495 wait_event_interruptible(st->wq_data_avail, st->done);
498 disable_irq_nosync(st->spi->irq);
500 __ad7192_write_reg(st, 1, 0, AD7192_REG_MODE, 3, st->mode);
502 return spi_bus_unlock(st->spi->master);
506 * ad7192_trigger_handler() bh of trigger launched polling to ring buffer
508 static irqreturn_t ad7192_trigger_handler(int irq, void *p)
510 struct iio_poll_func *pf = p;
511 struct iio_dev *indio_dev = pf->indio_dev;
512 struct iio_buffer *ring = indio_dev->buffer;
513 struct ad7192_state *st = iio_priv(indio_dev);
515 s32 *dat32 = (s32 *)dat64;
517 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
518 __ad7192_read_reg(st, 1, 1, AD7192_REG_DATA,
520 indio_dev->channels[0].scan_type.realbits/8);
522 /* Guaranteed to be aligned with 8 byte boundary */
523 if (indio_dev->scan_timestamp)
524 dat64[1] = pf->timestamp;
526 ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
528 iio_trigger_notify_done(indio_dev->trig);
530 enable_irq(st->spi->irq);
535 static const struct iio_buffer_setup_ops ad7192_ring_setup_ops = {
536 .preenable = &ad7192_ring_preenable,
537 .postenable = &iio_triggered_buffer_postenable,
538 .predisable = &iio_triggered_buffer_predisable,
539 .postdisable = &ad7192_ring_postdisable,
540 .validate_scan_mask = &iio_validate_scan_mask_onehot,
543 static int ad7192_register_ring_funcs_and_init(struct iio_dev *indio_dev)
545 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
546 &ad7192_trigger_handler, &ad7192_ring_setup_ops);
549 static void ad7192_ring_cleanup(struct iio_dev *indio_dev)
551 iio_triggered_buffer_cleanup(indio_dev);
555 * ad7192_data_rdy_trig_poll() the event handler for the data rdy trig
557 static irqreturn_t ad7192_data_rdy_trig_poll(int irq, void *private)
559 struct ad7192_state *st = iio_priv(private);
562 wake_up_interruptible(&st->wq_data_avail);
563 disable_irq_nosync(irq);
565 iio_trigger_poll(st->trig, iio_get_time_ns());
570 static struct iio_trigger_ops ad7192_trigger_ops = {
571 .owner = THIS_MODULE,
574 static int ad7192_probe_trigger(struct iio_dev *indio_dev)
576 struct ad7192_state *st = iio_priv(indio_dev);
579 st->trig = iio_trigger_alloc("%s-dev%d",
580 spi_get_device_id(st->spi)->name,
582 if (st->trig == NULL) {
586 st->trig->ops = &ad7192_trigger_ops;
587 ret = request_irq(st->spi->irq,
588 ad7192_data_rdy_trig_poll,
590 spi_get_device_id(st->spi)->name,
593 goto error_free_trig;
595 disable_irq_nosync(st->spi->irq);
597 st->trig->dev.parent = &st->spi->dev;
598 st->trig->private_data = indio_dev;
600 ret = iio_trigger_register(st->trig);
602 /* select default trigger */
603 indio_dev->trig = st->trig;
610 free_irq(st->spi->irq, indio_dev);
612 iio_trigger_free(st->trig);
617 static void ad7192_remove_trigger(struct iio_dev *indio_dev)
619 struct ad7192_state *st = iio_priv(indio_dev);
621 iio_trigger_unregister(st->trig);
622 free_irq(st->spi->irq, indio_dev);
623 iio_trigger_free(st->trig);
626 static ssize_t ad7192_read_frequency(struct device *dev,
627 struct device_attribute *attr,
630 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
631 struct ad7192_state *st = iio_priv(indio_dev);
633 return sprintf(buf, "%d\n", st->mclk /
634 (st->f_order * 1024 * AD7192_MODE_RATE(st->mode)));
637 static ssize_t ad7192_write_frequency(struct device *dev,
638 struct device_attribute *attr,
642 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
643 struct ad7192_state *st = iio_priv(indio_dev);
647 ret = strict_strtoul(buf, 10, &lval);
651 mutex_lock(&indio_dev->mlock);
652 if (iio_buffer_enabled(indio_dev)) {
653 mutex_unlock(&indio_dev->mlock);
657 div = st->mclk / (lval * st->f_order * 1024);
658 if (div < 1 || div > 1023) {
663 st->mode &= ~AD7192_MODE_RATE(-1);
664 st->mode |= AD7192_MODE_RATE(div);
665 ad7192_write_reg(st, AD7192_REG_MODE, 3, st->mode);
668 mutex_unlock(&indio_dev->mlock);
670 return ret ? ret : len;
673 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
674 ad7192_read_frequency,
675 ad7192_write_frequency);
678 static ssize_t ad7192_show_scale_available(struct device *dev,
679 struct device_attribute *attr, char *buf)
681 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
682 struct ad7192_state *st = iio_priv(indio_dev);
685 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
686 len += sprintf(buf + len, "%d.%09u ", st->scale_avail[i][0],
687 st->scale_avail[i][1]);
689 len += sprintf(buf + len, "\n");
694 static IIO_DEVICE_ATTR_NAMED(in_v_m_v_scale_available,
695 in_voltage-voltage_scale_available,
696 S_IRUGO, ad7192_show_scale_available, NULL, 0);
698 static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
699 ad7192_show_scale_available, NULL, 0);
701 static ssize_t ad7192_show_ac_excitation(struct device *dev,
702 struct device_attribute *attr,
705 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
706 struct ad7192_state *st = iio_priv(indio_dev);
708 return sprintf(buf, "%d\n", !!(st->mode & AD7192_MODE_ACX));
711 static ssize_t ad7192_show_bridge_switch(struct device *dev,
712 struct device_attribute *attr,
715 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
716 struct ad7192_state *st = iio_priv(indio_dev);
718 return sprintf(buf, "%d\n", !!(st->gpocon & AD7192_GPOCON_BPDSW));
721 static ssize_t ad7192_set(struct device *dev,
722 struct device_attribute *attr,
726 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
727 struct ad7192_state *st = iio_priv(indio_dev);
728 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
732 ret = strtobool(buf, &val);
736 mutex_lock(&indio_dev->mlock);
737 if (iio_buffer_enabled(indio_dev)) {
738 mutex_unlock(&indio_dev->mlock);
742 switch ((u32) this_attr->address) {
743 case AD7192_REG_GPOCON:
745 st->gpocon |= AD7192_GPOCON_BPDSW;
747 st->gpocon &= ~AD7192_GPOCON_BPDSW;
749 ad7192_write_reg(st, AD7192_REG_GPOCON, 1, st->gpocon);
751 case AD7192_REG_MODE:
753 st->mode |= AD7192_MODE_ACX;
755 st->mode &= ~AD7192_MODE_ACX;
757 ad7192_write_reg(st, AD7192_REG_GPOCON, 3, st->mode);
763 mutex_unlock(&indio_dev->mlock);
765 return ret ? ret : len;
768 static IIO_DEVICE_ATTR(bridge_switch_en, S_IRUGO | S_IWUSR,
769 ad7192_show_bridge_switch, ad7192_set,
772 static IIO_DEVICE_ATTR(ac_excitation_en, S_IRUGO | S_IWUSR,
773 ad7192_show_ac_excitation, ad7192_set,
776 static struct attribute *ad7192_attributes[] = {
777 &iio_dev_attr_sampling_frequency.dev_attr.attr,
778 &iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,
779 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
780 &iio_dev_attr_bridge_switch_en.dev_attr.attr,
781 &iio_dev_attr_ac_excitation_en.dev_attr.attr,
785 static const struct attribute_group ad7192_attribute_group = {
786 .attrs = ad7192_attributes,
789 static struct attribute *ad7195_attributes[] = {
790 &iio_dev_attr_sampling_frequency.dev_attr.attr,
791 &iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,
792 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
793 &iio_dev_attr_bridge_switch_en.dev_attr.attr,
797 static const struct attribute_group ad7195_attribute_group = {
798 .attrs = ad7195_attributes,
801 static int ad7192_read_raw(struct iio_dev *indio_dev,
802 struct iio_chan_spec const *chan,
807 struct ad7192_state *st = iio_priv(indio_dev);
809 bool unipolar = !!(st->conf & AD7192_CONF_UNIPOLAR);
812 case IIO_CHAN_INFO_RAW:
813 mutex_lock(&indio_dev->mlock);
814 if (iio_buffer_enabled(indio_dev))
817 ret = ad7192_read(st, chan->address,
818 chan->scan_type.realbits / 8, &smpl);
819 mutex_unlock(&indio_dev->mlock);
824 *val = (smpl >> chan->scan_type.shift) &
825 ((1 << (chan->scan_type.realbits)) - 1);
827 switch (chan->type) {
830 *val -= (1 << (chan->scan_type.realbits - 1));
834 *val /= 2815; /* temp Kelvin */
835 *val -= 273; /* temp Celsius */
842 case IIO_CHAN_INFO_SCALE:
843 switch (chan->type) {
845 mutex_lock(&indio_dev->mlock);
846 *val = st->scale_avail[AD7192_CONF_GAIN(st->conf)][0];
847 *val2 = st->scale_avail[AD7192_CONF_GAIN(st->conf)][1];
848 mutex_unlock(&indio_dev->mlock);
849 return IIO_VAL_INT_PLUS_NANO;
861 static int ad7192_write_raw(struct iio_dev *indio_dev,
862 struct iio_chan_spec const *chan,
867 struct ad7192_state *st = iio_priv(indio_dev);
871 mutex_lock(&indio_dev->mlock);
872 if (iio_buffer_enabled(indio_dev)) {
873 mutex_unlock(&indio_dev->mlock);
878 case IIO_CHAN_INFO_SCALE:
880 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
881 if (val2 == st->scale_avail[i][1]) {
883 st->conf &= ~AD7192_CONF_GAIN(-1);
884 st->conf |= AD7192_CONF_GAIN(i);
886 if (tmp != st->conf) {
887 ad7192_write_reg(st, AD7192_REG_CONF,
889 ad7192_calibrate_all(st);
898 mutex_unlock(&indio_dev->mlock);
903 static int ad7192_validate_trigger(struct iio_dev *indio_dev,
904 struct iio_trigger *trig)
906 if (indio_dev->trig != trig)
912 static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev,
913 struct iio_chan_spec const *chan,
916 return IIO_VAL_INT_PLUS_NANO;
919 static const struct iio_info ad7192_info = {
920 .read_raw = &ad7192_read_raw,
921 .write_raw = &ad7192_write_raw,
922 .write_raw_get_fmt = &ad7192_write_raw_get_fmt,
923 .attrs = &ad7192_attribute_group,
924 .validate_trigger = ad7192_validate_trigger,
925 .driver_module = THIS_MODULE,
928 static const struct iio_info ad7195_info = {
929 .read_raw = &ad7192_read_raw,
930 .write_raw = &ad7192_write_raw,
931 .write_raw_get_fmt = &ad7192_write_raw_get_fmt,
932 .attrs = &ad7195_attribute_group,
933 .validate_trigger = ad7192_validate_trigger,
934 .driver_module = THIS_MODULE,
937 #define AD7192_CHAN_DIFF(_chan, _chan2, _name, _address, _si) \
938 { .type = IIO_VOLTAGE, \
941 .extend_name = _name, \
943 .channel2 = _chan2, \
944 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
945 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
946 .address = _address, \
948 .scan_type = IIO_ST('s', 24, 32, 0)}
950 #define AD7192_CHAN(_chan, _address, _si) \
951 { .type = IIO_VOLTAGE, \
954 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
955 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
956 .address = _address, \
958 .scan_type = IIO_ST('s', 24, 32, 0)}
960 #define AD7192_CHAN_TEMP(_chan, _address, _si) \
961 { .type = IIO_TEMP, \
964 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
965 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
966 .address = _address, \
968 .scan_type = IIO_ST('s', 24, 32, 0)}
970 static struct iio_chan_spec ad7192_channels[] = {
971 AD7192_CHAN_DIFF(1, 2, NULL, AD7192_CH_AIN1P_AIN2M, 0),
972 AD7192_CHAN_DIFF(3, 4, NULL, AD7192_CH_AIN3P_AIN4M, 1),
973 AD7192_CHAN_TEMP(0, AD7192_CH_TEMP, 2),
974 AD7192_CHAN_DIFF(2, 2, "shorted", AD7192_CH_AIN2P_AIN2M, 3),
975 AD7192_CHAN(1, AD7192_CH_AIN1, 4),
976 AD7192_CHAN(2, AD7192_CH_AIN2, 5),
977 AD7192_CHAN(3, AD7192_CH_AIN3, 6),
978 AD7192_CHAN(4, AD7192_CH_AIN4, 7),
979 IIO_CHAN_SOFT_TIMESTAMP(8),
982 static int __devinit ad7192_probe(struct spi_device *spi)
984 struct ad7192_platform_data *pdata = spi->dev.platform_data;
985 struct ad7192_state *st;
986 struct iio_dev *indio_dev;
987 int ret , voltage_uv = 0;
990 dev_err(&spi->dev, "no platform data?\n");
995 dev_err(&spi->dev, "no IRQ?\n");
999 indio_dev = iio_device_alloc(sizeof(*st));
1000 if (indio_dev == NULL)
1003 st = iio_priv(indio_dev);
1005 st->reg = regulator_get(&spi->dev, "vcc");
1006 if (!IS_ERR(st->reg)) {
1007 ret = regulator_enable(st->reg);
1011 voltage_uv = regulator_get_voltage(st->reg);
1016 if (pdata && pdata->vref_mv)
1017 st->int_vref_mv = pdata->vref_mv;
1018 else if (voltage_uv)
1019 st->int_vref_mv = voltage_uv / 1000;
1021 dev_warn(&spi->dev, "reference voltage undefined\n");
1023 spi_set_drvdata(spi, indio_dev);
1025 st->devid = spi_get_device_id(spi)->driver_data;
1026 indio_dev->dev.parent = &spi->dev;
1027 indio_dev->name = spi_get_device_id(spi)->name;
1028 indio_dev->modes = INDIO_DIRECT_MODE;
1029 indio_dev->channels = ad7192_channels;
1030 indio_dev->num_channels = ARRAY_SIZE(ad7192_channels);
1031 if (st->devid == ID_AD7195)
1032 indio_dev->info = &ad7195_info;
1034 indio_dev->info = &ad7192_info;
1036 init_waitqueue_head(&st->wq_data_avail);
1038 ret = ad7192_register_ring_funcs_and_init(indio_dev);
1040 goto error_disable_reg;
1042 ret = ad7192_probe_trigger(indio_dev);
1044 goto error_ring_cleanup;
1046 ret = ad7192_setup(st);
1048 goto error_remove_trigger;
1050 ret = iio_device_register(indio_dev);
1052 goto error_remove_trigger;
1055 error_remove_trigger:
1056 ad7192_remove_trigger(indio_dev);
1058 ad7192_ring_cleanup(indio_dev);
1060 if (!IS_ERR(st->reg))
1061 regulator_disable(st->reg);
1063 if (!IS_ERR(st->reg))
1064 regulator_put(st->reg);
1066 iio_device_free(indio_dev);
1071 static int ad7192_remove(struct spi_device *spi)
1073 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1074 struct ad7192_state *st = iio_priv(indio_dev);
1076 iio_device_unregister(indio_dev);
1077 ad7192_remove_trigger(indio_dev);
1078 ad7192_ring_cleanup(indio_dev);
1080 if (!IS_ERR(st->reg)) {
1081 regulator_disable(st->reg);
1082 regulator_put(st->reg);
1088 static const struct spi_device_id ad7192_id[] = {
1089 {"ad7190", ID_AD7190},
1090 {"ad7192", ID_AD7192},
1091 {"ad7195", ID_AD7195},
1094 MODULE_DEVICE_TABLE(spi, ad7192_id);
1096 static struct spi_driver ad7192_driver = {
1099 .owner = THIS_MODULE,
1101 .probe = ad7192_probe,
1102 .remove = __devexit_p(ad7192_remove),
1103 .id_table = ad7192_id,
1105 module_spi_driver(ad7192_driver);
1107 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
1108 MODULE_DESCRIPTION("Analog Devices AD7190, AD7192, AD7195 ADC");
1109 MODULE_LICENSE("GPL v2");