10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
17 #include <linux/sched.h>
31 #define AD7192_REG_COMM 0
32 #define AD7192_REG_STAT 0
33 #define AD7192_REG_MODE 1
34 #define AD7192_REG_CONF 2
35 #define AD7192_REG_DATA 3
36 #define AD7192_REG_ID 4
37 #define AD7192_REG_GPOCON 5
38 #define AD7192_REG_OFFSET 6
40 #define AD7192_REG_FULLSALE 7
44 #define AD7192_COMM_WEN (1 << 7)
45 #define AD7192_COMM_WRITE (0 << 6)
46 #define AD7192_COMM_READ (1 << 6)
47 #define AD7192_COMM_ADDR(x) (((x) & 0x7) << 3)
48 #define AD7192_COMM_CREAD (1 << 2)
51 #define AD7192_STAT_RDY (1 << 7)
52 #define AD7192_STAT_ERR (1 << 6)
53 #define AD7192_STAT_NOREF (1 << 5)
54 #define AD7192_STAT_PARITY (1 << 4)
55 #define AD7192_STAT_CH3 (1 << 2)
56 #define AD7192_STAT_CH2 (1 << 1)
57 #define AD7192_STAT_CH1 (1 << 0)
60 #define AD7192_MODE_SEL(x) (((x) & 0x7) << 21)
61 #define AD7192_MODE_SEL_MASK (0x7 << 21)
62 #define AD7192_MODE_DAT_STA (1 << 20)
63 #define AD7192_MODE_CLKSRC(x) (((x) & 0x3) << 18)
64 #define AD7192_MODE_SINC3 (1 << 15)
65 #define AD7192_MODE_ACX (1 << 14)
66 #define AD7192_MODE_ENPAR (1 << 13)
67 #define AD7192_MODE_CLKDIV (1 << 12)
68 #define AD7192_MODE_SCYCLE (1 << 11)
69 #define AD7192_MODE_REJ60 (1 << 10)
70 #define AD7192_MODE_RATE(x) ((x) & 0x3FF)
73 #define AD7192_MODE_CONT 0
74 #define AD7192_MODE_SINGLE 1
75 #define AD7192_MODE_IDLE 2
76 #define AD7192_MODE_PWRDN 3
77 #define AD7192_MODE_CAL_INT_ZERO 4
78 #define AD7192_MODE_CAL_INT_FULL 5
79 #define AD7192_MODE_CAL_SYS_ZERO 6
80 #define AD7192_MODE_CAL_SYS_FULL 7
83 #define AD7192_CLK_EXT_MCLK1_2 0
85 #define AD7192_CLK_EXT_MCLK2 1
86 #define AD7192_CLK_INT 2
88 #define AD7192_CLK_INT_CO 3
94 #define AD7192_CONF_CHOP (1 << 23)
95 #define AD7192_CONF_REFSEL (1 << 20)
96 #define AD7192_CONF_CHAN(x) (((1 << (x)) & 0xFF) << 8)
97 #define AD7192_CONF_CHAN_MASK (0xFF << 8)
98 #define AD7192_CONF_BURN (1 << 7)
99 #define AD7192_CONF_REFDET (1 << 6)
100 #define AD7192_CONF_BUF (1 << 4)
101 #define AD7192_CONF_UNIPOLAR (1 << 3)
102 #define AD7192_CONF_GAIN(x) ((x) & 0x7)
104 #define AD7192_CH_AIN1P_AIN2M 0
105 #define AD7192_CH_AIN3P_AIN4M 1
106 #define AD7192_CH_TEMP 2
107 #define AD7192_CH_AIN2P_AIN2M 3
108 #define AD7192_CH_AIN1 4
109 #define AD7192_CH_AIN2 5
110 #define AD7192_CH_AIN3 6
111 #define AD7192_CH_AIN4 7
114 #define ID_AD7190 0x4
115 #define ID_AD7192 0x0
116 #define ID_AD7195 0x6
117 #define AD7192_ID_MASK 0x0F
120 #define AD7192_GPOCON_BPDSW (1 << 6)
121 #define AD7192_GPOCON_GP32EN (1 << 5)
122 #define AD7192_GPOCON_GP10EN (1 << 4)
123 #define AD7192_GPOCON_P3DAT (1 << 3)
124 #define AD7192_GPOCON_P2DAT (1 << 2)
125 #define AD7192_GPOCON_P1DAT (1 << 1)
126 #define AD7192_GPOCON_P0DAT (1 << 0)
128 #define AD7192_INT_FREQ_MHz 4915200
179 .set_channel = ad7192_set_channel,
180 .set_mode = ad7192_set_mode,
181 .has_registers =
true,
206 struct iio_dev *indio_dev = spi_get_drvdata(st->
sd.spi);
207 unsigned long long scale_uv;
213 ret = spi_write(st->
sd.spi, &ones, 6);
226 dev_warn(&st->
sd.spi->dev,
"device ID query failed (0x%X)\n",
id);
287 ret = ad7192_calibrate_all(st);
294 >> (indio_dev->
channels[0].scan_type.realbits -
304 dev_err(&st->
sd.spi->dev,
"setup failed\n");
312 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
324 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
336 if (iio_buffer_enabled(indio_dev)) {
342 if (div < 1 || div > 1023) {
354 return ret ? ret : len;
358 ad7192_read_frequency,
359 ad7192_write_frequency);
361 static ssize_t ad7192_show_scale_available(
struct device *dev,
364 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
372 len +=
sprintf(buf + len,
"\n");
378 in_voltage-voltage_scale_available,
382 ad7192_show_scale_available,
NULL, 0);
388 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
398 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
409 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
420 if (iio_buffer_enabled(indio_dev)) {
448 return ret ? ret : len;
452 ad7192_show_bridge_switch, ad7192_set,
456 ad7192_show_ac_excitation, ad7192_set,
459 static struct attribute *ad7192_attributes[] = {
460 &iio_dev_attr_sampling_frequency.dev_attr.attr,
461 &iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,
462 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
463 &iio_dev_attr_bridge_switch_en.dev_attr.attr,
464 &iio_dev_attr_ac_excitation_en.dev_attr.attr,
469 .attrs = ad7192_attributes,
472 static struct attribute *ad7195_attributes[] = {
473 &iio_dev_attr_sampling_frequency.dev_attr.attr,
474 &iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,
475 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
476 &iio_dev_attr_bridge_switch_en.dev_attr.attr,
481 .attrs = ad7195_attributes,
484 static unsigned int ad7192_get_temp_scale(
bool unipolar)
486 return unipolar ? 2815 * 2 : 2815;
489 static int ad7192_read_raw(
struct iio_dev *indio_dev,
502 switch (chan->
type) {
511 *val2 = 1000000000 / ad7192_get_temp_scale(unipolar);
518 *val = -(1 << (chan->
scan_type.realbits - 1));
523 *val -= 273 * ad7192_get_temp_scale(unipolar);
530 static int ad7192_write_raw(
struct iio_dev *indio_dev,
541 if (iio_buffer_enabled(indio_dev)) {
559 ad7192_calibrate_all(st);
572 static int ad7192_write_raw_get_fmt(
struct iio_dev *indio_dev,
579 static const struct iio_info ad7192_info = {
580 .read_raw = &ad7192_read_raw,
581 .write_raw = &ad7192_write_raw,
582 .write_raw_get_fmt = &ad7192_write_raw_get_fmt,
583 .attrs = &ad7192_attribute_group,
588 static const struct iio_info ad7195_info = {
589 .read_raw = &ad7192_read_raw,
590 .write_raw = &ad7192_write_raw,
591 .write_raw_get_fmt = &ad7192_write_raw_get_fmt,
592 .attrs = &ad7195_attribute_group,
614 int ret , voltage_uv = 0;
627 if (indio_dev ==
NULL)
630 st = iio_priv(indio_dev);
633 if (!IS_ERR(st->
reg)) {
646 dev_warn(&spi->
dev,
"reference voltage undefined\n");
648 spi_set_drvdata(spi, indio_dev);
650 indio_dev->
dev.parent = &spi->
dev;
653 indio_dev->
channels = ad7192_channels;
656 indio_dev->
info = &ad7195_info;
658 indio_dev->
info = &ad7192_info;
660 ad_sd_init(&st->
sd, indio_dev, spi, &ad7192_sigma_delta_info);
664 goto error_disable_reg;
666 ret = ad7192_setup(st, pdata);
668 goto error_remove_trigger;
672 goto error_remove_trigger;
675 error_remove_trigger:
678 if (!IS_ERR(st->
reg))
681 if (!IS_ERR(st->
reg))
691 struct iio_dev *indio_dev = spi_get_drvdata(spi);
697 if (!IS_ERR(st->
reg)) {
718 .probe = ad7192_probe,
720 .id_table = ad7192_id,