1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2021 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/iio/iio.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/spi/spi.h>
19 #include <asm/unaligned.h>
21 #define AD7293_R1B BIT(16)
22 #define AD7293_R2B BIT(17)
23 #define AD7293_PAGE_ADDR_MSK GENMASK(15, 8)
24 #define AD7293_PAGE(x) FIELD_PREP(AD7293_PAGE_ADDR_MSK, x)
26 /* AD7293 Register Map Common */
27 #define AD7293_REG_NO_OP (AD7293_R1B | AD7293_PAGE(0x0) | 0x0)
28 #define AD7293_REG_PAGE_SELECT (AD7293_R1B | AD7293_PAGE(0x0) | 0x1)
29 #define AD7293_REG_CONV_CMD (AD7293_R2B | AD7293_PAGE(0x0) | 0x2)
30 #define AD7293_REG_RESULT (AD7293_R1B | AD7293_PAGE(0x0) | 0x3)
31 #define AD7293_REG_DAC_EN (AD7293_R1B | AD7293_PAGE(0x0) | 0x4)
32 #define AD7293_REG_DEVICE_ID (AD7293_R2B | AD7293_PAGE(0x0) | 0xC)
33 #define AD7293_REG_SOFT_RESET (AD7293_R2B | AD7293_PAGE(0x0) | 0xF)
35 /* AD7293 Register Map Page 0x0 */
36 #define AD7293_REG_VIN0 (AD7293_R2B | AD7293_PAGE(0x0) | 0x10)
37 #define AD7293_REG_VIN1 (AD7293_R2B | AD7293_PAGE(0x0) | 0x11)
38 #define AD7293_REG_VIN2 (AD7293_R2B | AD7293_PAGE(0x0) | 0x12)
39 #define AD7293_REG_VIN3 (AD7293_R2B | AD7293_PAGE(0x0) | 0x13)
40 #define AD7293_REG_TSENSE_INT (AD7293_R2B | AD7293_PAGE(0x0) | 0x20)
41 #define AD7293_REG_TSENSE_D0 (AD7293_R2B | AD7293_PAGE(0x0) | 0x21)
42 #define AD7293_REG_TSENSE_D1 (AD7293_R2B | AD7293_PAGE(0x0) | 0x22)
43 #define AD7293_REG_ISENSE_0 (AD7293_R2B | AD7293_PAGE(0x0) | 0x28)
44 #define AD7293_REG_ISENSE_1 (AD7293_R2B | AD7293_PAGE(0x0) | 0x29)
45 #define AD7293_REG_ISENSE_2 (AD7293_R2B | AD7293_PAGE(0x0) | 0x2A)
46 #define AD7293_REG_ISENSE_3 (AD7293_R2B | AD7293_PAGE(0x0) | 0x2B)
47 #define AD7293_REG_UNI_VOUT0 (AD7293_R2B | AD7293_PAGE(0x0) | 0x30)
48 #define AD7293_REG_UNI_VOUT1 (AD7293_R2B | AD7293_PAGE(0x0) | 0x31)
49 #define AD7293_REG_UNI_VOUT2 (AD7293_R2B | AD7293_PAGE(0x0) | 0x32)
50 #define AD7293_REG_UNI_VOUT3 (AD7293_R2B | AD7293_PAGE(0x0) | 0x33)
51 #define AD7293_REG_BI_VOUT0 (AD7293_R2B | AD7293_PAGE(0x0) | 0x34)
52 #define AD7293_REG_BI_VOUT1 (AD7293_R2B | AD7293_PAGE(0x0) | 0x35)
53 #define AD7293_REG_BI_VOUT2 (AD7293_R2B | AD7293_PAGE(0x0) | 0x36)
54 #define AD7293_REG_BI_VOUT3 (AD7293_R2B | AD7293_PAGE(0x0) | 0x37)
56 /* AD7293 Register Map Page 0x2 */
57 #define AD7293_REG_DIGITAL_OUT_EN (AD7293_R2B | AD7293_PAGE(0x2) | 0x11)
58 #define AD7293_REG_DIGITAL_INOUT_FUNC (AD7293_R2B | AD7293_PAGE(0x2) | 0x12)
59 #define AD7293_REG_DIGITAL_FUNC_POL (AD7293_R2B | AD7293_PAGE(0x2) | 0x13)
60 #define AD7293_REG_GENERAL (AD7293_R2B | AD7293_PAGE(0x2) | 0x14)
61 #define AD7293_REG_VINX_RANGE0 (AD7293_R2B | AD7293_PAGE(0x2) | 0x15)
62 #define AD7293_REG_VINX_RANGE1 (AD7293_R2B | AD7293_PAGE(0x2) | 0x16)
63 #define AD7293_REG_VINX_DIFF_SE (AD7293_R2B | AD7293_PAGE(0x2) | 0x17)
64 #define AD7293_REG_VINX_FILTER (AD7293_R2B | AD7293_PAGE(0x2) | 0x18)
65 #define AD7293_REG_BG_EN (AD7293_R2B | AD7293_PAGE(0x2) | 0x19)
66 #define AD7293_REG_CONV_DELAY (AD7293_R2B | AD7293_PAGE(0x2) | 0x1A)
67 #define AD7293_REG_TSENSE_BG_EN (AD7293_R2B | AD7293_PAGE(0x2) | 0x1B)
68 #define AD7293_REG_ISENSE_BG_EN (AD7293_R2B | AD7293_PAGE(0x2) | 0x1C)
69 #define AD7293_REG_ISENSE_GAIN (AD7293_R2B | AD7293_PAGE(0x2) | 0x1D)
70 #define AD7293_REG_DAC_SNOOZE_O (AD7293_R2B | AD7293_PAGE(0x2) | 0x1F)
71 #define AD7293_REG_DAC_SNOOZE_1 (AD7293_R2B | AD7293_PAGE(0x2) | 0x20)
72 #define AD7293_REG_RSX_MON_BG_EN (AD7293_R2B | AD7293_PAGE(0x2) | 0x23)
73 #define AD7293_REG_INTEGR_CL (AD7293_R2B | AD7293_PAGE(0x2) | 0x28)
74 #define AD7293_REG_PA_ON_CTRL (AD7293_R2B | AD7293_PAGE(0x2) | 0x29)
75 #define AD7293_REG_RAMP_TIME_0 (AD7293_R2B | AD7293_PAGE(0x2) | 0x2A)
76 #define AD7293_REG_RAMP_TIME_1 (AD7293_R2B | AD7293_PAGE(0x2) | 0x2B)
77 #define AD7293_REG_RAMP_TIME_2 (AD7293_R2B | AD7293_PAGE(0x2) | 0x2C)
78 #define AD7293_REG_RAMP_TIME_3 (AD7293_R2B | AD7293_PAGE(0x2) | 0x2D)
79 #define AD7293_REG_CL_FR_IT (AD7293_R2B | AD7293_PAGE(0x2) | 0x2E)
80 #define AD7293_REG_INTX_AVSS_AVDD (AD7293_R2B | AD7293_PAGE(0x2) | 0x2F)
82 /* AD7293 Register Map Page 0x3 */
83 #define AD7293_REG_VINX_SEQ (AD7293_R2B | AD7293_PAGE(0x3) | 0x10)
84 #define AD7293_REG_ISENSEX_TSENSEX_SEQ (AD7293_R2B | AD7293_PAGE(0x3) | 0x11)
85 #define AD7293_REG_RSX_MON_BI_VOUTX_SEQ (AD7293_R2B | AD7293_PAGE(0x3) | 0x12)
87 /* AD7293 Register Map Page 0xE */
88 #define AD7293_REG_VIN0_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x10)
89 #define AD7293_REG_VIN1_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x11)
90 #define AD7293_REG_VIN2_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x12)
91 #define AD7293_REG_VIN3_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x13)
92 #define AD7293_REG_TSENSE_INT_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x20)
93 #define AD7293_REG_TSENSE_D0_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x21)
94 #define AD7293_REG_TSENSE_D1_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x22)
95 #define AD7293_REG_ISENSE0_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x28)
96 #define AD7293_REG_ISENSE1_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x29)
97 #define AD7293_REG_ISENSE2_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x2A)
98 #define AD7293_REG_ISENSE3_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x2B)
99 #define AD7293_REG_UNI_VOUT0_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x30)
100 #define AD7293_REG_UNI_VOUT1_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x31)
101 #define AD7293_REG_UNI_VOUT2_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x32)
102 #define AD7293_REG_UNI_VOUT3_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x33)
103 #define AD7293_REG_BI_VOUT0_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x34)
104 #define AD7293_REG_BI_VOUT1_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x35)
105 #define AD7293_REG_BI_VOUT2_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x36)
106 #define AD7293_REG_BI_VOUT3_OFFSET (AD7293_R1B | AD7293_PAGE(0xE) | 0x37)
108 /* AD7293 Miscellaneous Definitions */
109 #define AD7293_READ BIT(7)
110 #define AD7293_TRANSF_LEN_MSK GENMASK(17, 16)
112 #define AD7293_REG_ADDR_MSK GENMASK(7, 0)
113 #define AD7293_REG_VOUT_OFFSET_MSK GENMASK(5, 4)
114 #define AD7293_REG_DATA_RAW_MSK GENMASK(15, 4)
115 #define AD7293_REG_VINX_RANGE_GET_CH_MSK(x, ch) (((x) >> (ch)) & 0x1)
116 #define AD7293_REG_VINX_RANGE_SET_CH_MSK(x, ch) (((x) & 0x1) << (ch))
117 #define AD7293_CHIP_ID 0x18
119 enum ad7293_ch_type {
126 enum ad7293_max_offset {
127 AD7293_TSENSE_MIN_OFFSET_CH = 4,
128 AD7293_ISENSE_MIN_OFFSET_CH = 7,
129 AD7293_VOUT_MIN_OFFSET_CH = 11,
130 AD7293_VOUT_MAX_OFFSET_CH = 18,
133 static const int dac_offset_table[] = {0, 1, 2};
135 static const int isense_gain_table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
137 static const int adc_range_table[] = {0, 1, 2, 3};
139 struct ad7293_state {
140 struct spi_device *spi;
141 /* Protect against concurrent accesses to the device, page selection and data content */
143 struct gpio_desc *gpio_reset;
144 struct regulator *reg_avdd;
145 struct regulator *reg_vdrive;
147 u8 data[3] __aligned(IIO_DMA_MINALIGN);
150 static int ad7293_page_select(struct ad7293_state *st, unsigned int reg)
154 if (st->page_select != FIELD_GET(AD7293_PAGE_ADDR_MSK, reg)) {
155 st->data[0] = FIELD_GET(AD7293_REG_ADDR_MSK, AD7293_REG_PAGE_SELECT);
156 st->data[1] = FIELD_GET(AD7293_PAGE_ADDR_MSK, reg);
158 ret = spi_write(st->spi, &st->data[0], 2);
162 st->page_select = FIELD_GET(AD7293_PAGE_ADDR_MSK, reg);
168 static int __ad7293_spi_read(struct ad7293_state *st, unsigned int reg,
173 struct spi_transfer t = {0};
175 length = FIELD_GET(AD7293_TRANSF_LEN_MSK, reg);
177 ret = ad7293_page_select(st, reg);
181 st->data[0] = AD7293_READ | FIELD_GET(AD7293_REG_ADDR_MSK, reg);
185 t.tx_buf = &st->data[0];
186 t.rx_buf = &st->data[0];
189 ret = spi_sync_transfer(st->spi, &t, 1);
196 *val = get_unaligned_be16(&st->data[1]);
201 static int ad7293_spi_read(struct ad7293_state *st, unsigned int reg,
206 mutex_lock(&st->lock);
207 ret = __ad7293_spi_read(st, reg, val);
208 mutex_unlock(&st->lock);
213 static int __ad7293_spi_write(struct ad7293_state *st, unsigned int reg,
219 length = FIELD_GET(AD7293_TRANSF_LEN_MSK, reg);
221 ret = ad7293_page_select(st, reg);
225 st->data[0] = FIELD_GET(AD7293_REG_ADDR_MSK, reg);
230 put_unaligned_be16(val, &st->data[1]);
232 return spi_write(st->spi, &st->data[0], length + 1);
235 static int ad7293_spi_write(struct ad7293_state *st, unsigned int reg,
240 mutex_lock(&st->lock);
241 ret = __ad7293_spi_write(st, reg, val);
242 mutex_unlock(&st->lock);
247 static int __ad7293_spi_update_bits(struct ad7293_state *st, unsigned int reg,
253 ret = __ad7293_spi_read(st, reg, &data);
257 temp = (data & ~mask) | (val & mask);
259 return __ad7293_spi_write(st, reg, temp);
262 static int ad7293_spi_update_bits(struct ad7293_state *st, unsigned int reg,
267 mutex_lock(&st->lock);
268 ret = __ad7293_spi_update_bits(st, reg, mask, val);
269 mutex_unlock(&st->lock);
274 static int ad7293_adc_get_scale(struct ad7293_state *st, unsigned int ch,
280 mutex_lock(&st->lock);
282 ret = __ad7293_spi_read(st, AD7293_REG_VINX_RANGE1, &data);
286 *range = AD7293_REG_VINX_RANGE_GET_CH_MSK(data, ch);
288 ret = __ad7293_spi_read(st, AD7293_REG_VINX_RANGE0, &data);
292 *range |= AD7293_REG_VINX_RANGE_GET_CH_MSK(data, ch) << 1;
295 mutex_unlock(&st->lock);
300 static int ad7293_adc_set_scale(struct ad7293_state *st, unsigned int ch,
304 unsigned int ch_msk = BIT(ch);
306 mutex_lock(&st->lock);
307 ret = __ad7293_spi_update_bits(st, AD7293_REG_VINX_RANGE1, ch_msk,
308 AD7293_REG_VINX_RANGE_SET_CH_MSK(range, ch));
312 ret = __ad7293_spi_update_bits(st, AD7293_REG_VINX_RANGE0, ch_msk,
313 AD7293_REG_VINX_RANGE_SET_CH_MSK((range >> 1), ch));
316 mutex_unlock(&st->lock);
321 static int ad7293_get_offset(struct ad7293_state *st, unsigned int ch,
324 if (ch < AD7293_TSENSE_MIN_OFFSET_CH)
325 return ad7293_spi_read(st, AD7293_REG_VIN0_OFFSET + ch, offset);
326 else if (ch < AD7293_ISENSE_MIN_OFFSET_CH)
327 return ad7293_spi_read(st, AD7293_REG_TSENSE_INT_OFFSET + (ch - 4), offset);
328 else if (ch < AD7293_VOUT_MIN_OFFSET_CH)
329 return ad7293_spi_read(st, AD7293_REG_ISENSE0_OFFSET + (ch - 7), offset);
330 else if (ch <= AD7293_VOUT_MAX_OFFSET_CH)
331 return ad7293_spi_read(st, AD7293_REG_UNI_VOUT0_OFFSET + (ch - 11), offset);
336 static int ad7293_set_offset(struct ad7293_state *st, unsigned int ch,
339 if (ch < AD7293_TSENSE_MIN_OFFSET_CH)
340 return ad7293_spi_write(st, AD7293_REG_VIN0_OFFSET + ch,
342 else if (ch < AD7293_ISENSE_MIN_OFFSET_CH)
343 return ad7293_spi_write(st,
344 AD7293_REG_TSENSE_INT_OFFSET +
345 (ch - AD7293_TSENSE_MIN_OFFSET_CH),
347 else if (ch < AD7293_VOUT_MIN_OFFSET_CH)
348 return ad7293_spi_write(st,
349 AD7293_REG_ISENSE0_OFFSET +
350 (ch - AD7293_ISENSE_MIN_OFFSET_CH),
352 else if (ch <= AD7293_VOUT_MAX_OFFSET_CH)
353 return ad7293_spi_update_bits(st,
354 AD7293_REG_UNI_VOUT0_OFFSET +
355 (ch - AD7293_VOUT_MIN_OFFSET_CH),
356 AD7293_REG_VOUT_OFFSET_MSK,
357 FIELD_PREP(AD7293_REG_VOUT_OFFSET_MSK, offset));
362 static int ad7293_isense_set_scale(struct ad7293_state *st, unsigned int ch,
365 unsigned int ch_msk = (0xf << (4 * ch));
367 return ad7293_spi_update_bits(st, AD7293_REG_ISENSE_GAIN, ch_msk,
371 static int ad7293_isense_get_scale(struct ad7293_state *st, unsigned int ch,
376 ret = ad7293_spi_read(st, AD7293_REG_ISENSE_GAIN, gain);
380 *gain = (*gain >> (4 * ch)) & 0xf;
385 static int ad7293_dac_write_raw(struct ad7293_state *st, unsigned int ch,
390 mutex_lock(&st->lock);
392 ret = __ad7293_spi_update_bits(st, AD7293_REG_DAC_EN, BIT(ch), BIT(ch));
396 ret = __ad7293_spi_write(st, AD7293_REG_UNI_VOUT0 + ch,
397 FIELD_PREP(AD7293_REG_DATA_RAW_MSK, raw));
400 mutex_unlock(&st->lock);
405 static int ad7293_ch_read_raw(struct ad7293_state *st, enum ad7293_ch_type type,
406 unsigned int ch, u16 *raw)
409 unsigned int reg_wr, reg_rd, data_wr;
412 case AD7293_ADC_VINX:
413 reg_wr = AD7293_REG_VINX_SEQ;
414 reg_rd = AD7293_REG_VIN0 + ch;
418 case AD7293_ADC_TSENSE:
419 reg_wr = AD7293_REG_ISENSEX_TSENSEX_SEQ;
420 reg_rd = AD7293_REG_TSENSE_INT + ch;
424 case AD7293_ADC_ISENSE:
425 reg_wr = AD7293_REG_ISENSEX_TSENSEX_SEQ;
426 reg_rd = AD7293_REG_ISENSE_0 + ch;
427 data_wr = BIT(ch) << 8;
431 reg_rd = AD7293_REG_UNI_VOUT0 + ch;
438 mutex_lock(&st->lock);
440 if (type != AD7293_DAC) {
441 if (type == AD7293_ADC_TSENSE) {
442 ret = __ad7293_spi_write(st, AD7293_REG_TSENSE_BG_EN,
447 usleep_range(9000, 9900);
448 } else if (type == AD7293_ADC_ISENSE) {
449 ret = __ad7293_spi_write(st, AD7293_REG_ISENSE_BG_EN,
454 usleep_range(2000, 7000);
457 ret = __ad7293_spi_write(st, reg_wr, data_wr);
461 ret = __ad7293_spi_write(st, AD7293_REG_CONV_CMD, 0x82);
466 ret = __ad7293_spi_read(st, reg_rd, raw);
468 *raw = FIELD_GET(AD7293_REG_DATA_RAW_MSK, *raw);
471 mutex_unlock(&st->lock);
476 static int ad7293_read_raw(struct iio_dev *indio_dev,
477 struct iio_chan_spec const *chan,
478 int *val, int *val2, long info)
480 struct ad7293_state *st = iio_priv(indio_dev);
485 case IIO_CHAN_INFO_RAW:
486 switch (chan->type) {
489 ret = ad7293_ch_read_raw(st, AD7293_DAC,
490 chan->channel, &data);
492 ret = ad7293_ch_read_raw(st, AD7293_ADC_VINX,
493 chan->channel, &data);
497 ret = ad7293_ch_read_raw(st, AD7293_ADC_ISENSE,
498 chan->channel, &data);
502 ret = ad7293_ch_read_raw(st, AD7293_ADC_TSENSE,
503 chan->channel, &data);
516 case IIO_CHAN_INFO_OFFSET:
517 switch (chan->type) {
520 ret = ad7293_get_offset(st,
521 chan->channel + AD7293_VOUT_MIN_OFFSET_CH,
524 data = FIELD_GET(AD7293_REG_VOUT_OFFSET_MSK, data);
526 ret = ad7293_get_offset(st, chan->channel, &data);
531 ret = ad7293_get_offset(st,
532 chan->channel + AD7293_ISENSE_MIN_OFFSET_CH,
537 ret = ad7293_get_offset(st,
538 chan->channel + AD7293_TSENSE_MIN_OFFSET_CH,
551 case IIO_CHAN_INFO_SCALE:
552 switch (chan->type) {
554 ret = ad7293_adc_get_scale(st, chan->channel, &data);
562 ret = ad7293_isense_get_scale(st, chan->channel, &data);
573 return IIO_VAL_FRACTIONAL;
582 static int ad7293_write_raw(struct iio_dev *indio_dev,
583 struct iio_chan_spec const *chan,
584 int val, int val2, long info)
586 struct ad7293_state *st = iio_priv(indio_dev);
589 case IIO_CHAN_INFO_RAW:
590 switch (chan->type) {
595 return ad7293_dac_write_raw(st, chan->channel, val);
599 case IIO_CHAN_INFO_OFFSET:
600 switch (chan->type) {
603 return ad7293_set_offset(st,
605 AD7293_VOUT_MIN_OFFSET_CH,
608 return ad7293_set_offset(st, chan->channel, val);
610 return ad7293_set_offset(st,
612 AD7293_ISENSE_MIN_OFFSET_CH,
615 return ad7293_set_offset(st,
617 AD7293_TSENSE_MIN_OFFSET_CH,
622 case IIO_CHAN_INFO_SCALE:
623 switch (chan->type) {
625 return ad7293_adc_set_scale(st, chan->channel, val);
627 return ad7293_isense_set_scale(st, chan->channel, val);
636 static int ad7293_reg_access(struct iio_dev *indio_dev,
638 unsigned int write_val,
639 unsigned int *read_val)
641 struct ad7293_state *st = iio_priv(indio_dev);
646 ret = ad7293_spi_read(st, reg, &temp);
649 ret = ad7293_spi_write(st, reg, (u16)write_val);
655 static int ad7293_read_avail(struct iio_dev *indio_dev,
656 struct iio_chan_spec const *chan,
657 const int **vals, int *type, int *length,
661 case IIO_CHAN_INFO_OFFSET:
662 *vals = dac_offset_table;
664 *length = ARRAY_SIZE(dac_offset_table);
666 return IIO_AVAIL_LIST;
667 case IIO_CHAN_INFO_SCALE:
670 switch (chan->type) {
672 *vals = adc_range_table;
673 *length = ARRAY_SIZE(adc_range_table);
674 return IIO_AVAIL_LIST;
676 *vals = isense_gain_table;
677 *length = ARRAY_SIZE(isense_gain_table);
678 return IIO_AVAIL_LIST;
687 #define AD7293_CHAN_ADC(_channel) { \
688 .type = IIO_VOLTAGE, \
691 .channel = _channel, \
692 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
693 BIT(IIO_CHAN_INFO_SCALE) | \
694 BIT(IIO_CHAN_INFO_OFFSET), \
695 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) \
698 #define AD7293_CHAN_DAC(_channel) { \
699 .type = IIO_VOLTAGE, \
702 .channel = _channel, \
703 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
704 BIT(IIO_CHAN_INFO_OFFSET), \
705 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_OFFSET) \
708 #define AD7293_CHAN_ISENSE(_channel) { \
709 .type = IIO_CURRENT, \
712 .channel = _channel, \
713 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
714 BIT(IIO_CHAN_INFO_OFFSET) | \
715 BIT(IIO_CHAN_INFO_SCALE), \
716 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) \
719 #define AD7293_CHAN_TEMP(_channel) { \
723 .channel = _channel, \
724 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
725 BIT(IIO_CHAN_INFO_OFFSET), \
726 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
729 static const struct iio_chan_spec ad7293_channels[] = {
734 AD7293_CHAN_ISENSE(0),
735 AD7293_CHAN_ISENSE(1),
736 AD7293_CHAN_ISENSE(2),
737 AD7293_CHAN_ISENSE(3),
751 static int ad7293_soft_reset(struct ad7293_state *st)
755 ret = __ad7293_spi_write(st, AD7293_REG_SOFT_RESET, 0x7293);
759 return __ad7293_spi_write(st, AD7293_REG_SOFT_RESET, 0x0000);
762 static int ad7293_reset(struct ad7293_state *st)
764 if (st->gpio_reset) {
765 gpiod_set_value(st->gpio_reset, 0);
766 usleep_range(100, 1000);
767 gpiod_set_value(st->gpio_reset, 1);
768 usleep_range(100, 1000);
773 /* Perform a software reset */
774 return ad7293_soft_reset(st);
777 static int ad7293_properties_parse(struct ad7293_state *st)
779 struct spi_device *spi = st->spi;
781 st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset",
783 if (IS_ERR(st->gpio_reset))
784 return dev_err_probe(&spi->dev, PTR_ERR(st->gpio_reset),
785 "failed to get the reset GPIO\n");
787 st->reg_avdd = devm_regulator_get(&spi->dev, "avdd");
788 if (IS_ERR(st->reg_avdd))
789 return dev_err_probe(&spi->dev, PTR_ERR(st->reg_avdd),
790 "failed to get the AVDD voltage\n");
792 st->reg_vdrive = devm_regulator_get(&spi->dev, "vdrive");
793 if (IS_ERR(st->reg_vdrive))
794 return dev_err_probe(&spi->dev, PTR_ERR(st->reg_vdrive),
795 "failed to get the VDRIVE voltage\n");
800 static void ad7293_reg_disable(void *data)
802 regulator_disable(data);
805 static int ad7293_init(struct ad7293_state *st)
809 struct spi_device *spi = st->spi;
811 ret = ad7293_properties_parse(st);
815 ret = ad7293_reset(st);
819 ret = regulator_enable(st->reg_avdd);
822 "Failed to enable specified AVDD Voltage!\n");
826 ret = devm_add_action_or_reset(&spi->dev, ad7293_reg_disable,
831 ret = regulator_enable(st->reg_vdrive);
834 "Failed to enable specified VDRIVE Voltage!\n");
838 ret = devm_add_action_or_reset(&spi->dev, ad7293_reg_disable,
843 ret = regulator_get_voltage(st->reg_avdd);
845 dev_err(&spi->dev, "Failed to read avdd regulator: %d\n", ret);
849 if (ret > 5500000 || ret < 4500000)
852 ret = regulator_get_voltage(st->reg_vdrive);
855 "Failed to read vdrive regulator: %d\n", ret);
858 if (ret > 5500000 || ret < 1700000)
862 ret = __ad7293_spi_read(st, AD7293_REG_DEVICE_ID, &chip_id);
866 if (chip_id != AD7293_CHIP_ID) {
867 dev_err(&spi->dev, "Invalid Chip ID.\n");
874 static const struct iio_info ad7293_info = {
875 .read_raw = ad7293_read_raw,
876 .write_raw = ad7293_write_raw,
877 .read_avail = &ad7293_read_avail,
878 .debugfs_reg_access = &ad7293_reg_access,
881 static int ad7293_probe(struct spi_device *spi)
883 struct iio_dev *indio_dev;
884 struct ad7293_state *st;
887 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
891 st = iio_priv(indio_dev);
893 indio_dev->info = &ad7293_info;
894 indio_dev->name = "ad7293";
895 indio_dev->channels = ad7293_channels;
896 indio_dev->num_channels = ARRAY_SIZE(ad7293_channels);
901 mutex_init(&st->lock);
903 ret = ad7293_init(st);
907 return devm_iio_device_register(&spi->dev, indio_dev);
910 static const struct spi_device_id ad7293_id[] = {
914 MODULE_DEVICE_TABLE(spi, ad7293_id);
916 static const struct of_device_id ad7293_of_match[] = {
917 { .compatible = "adi,ad7293" },
920 MODULE_DEVICE_TABLE(of, ad7293_of_match);
922 static struct spi_driver ad7293_driver = {
925 .of_match_table = ad7293_of_match,
927 .probe = ad7293_probe,
928 .id_table = ad7293_id,
930 module_spi_driver(ad7293_driver);
933 MODULE_DESCRIPTION("Analog Devices AD7293");
934 MODULE_LICENSE("GPL v2");