1 // SPDX-License-Identifier: GPL-2.0+
3 * AD7770, AD7771, AD7779 ADC
5 * Copyright 2023-2024 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/bitmap.h>
10 #include <linux/clk.h>
11 #include <linux/crc8.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/math.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/spi/spi.h>
22 #include <linux/string.h>
23 #include <linux/types.h>
24 #include <linux/unaligned.h>
25 #include <linux/units.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include <linux/iio/trigger_consumer.h>
34 #define AD7779_SPI_READ_CMD BIT(7)
36 #define AD7779_DISABLE_SD BIT(7)
38 #define AD7779_REG_CH_DISABLE 0x08
39 #define AD7779_REG_CH_SYNC_OFFSET(ch) (0x09 + (ch))
40 #define AD7779_REG_CH_CONFIG(ch) (0x00 + (ch))
41 #define AD7779_REG_GENERAL_USER_CONFIG_1 0x11
42 #define AD7779_REG_GENERAL_USER_CONFIG_2 0x12
43 #define AD7779_REG_GENERAL_USER_CONFIG_3 0x13
44 #define AD7779_REG_DOUT_FORMAT 0x14
45 #define AD7779_REG_ADC_MUX_CONFIG 0x15
46 #define AD7779_REG_GPIO_CONFIG 0x17
47 #define AD7779_REG_BUFFER_CONFIG_1 0x19
48 #define AD7779_REG_GLOBAL_MUX_CONFIG 0x16
49 #define AD7779_REG_BUFFER_CONFIG_2 0x1A
50 #define AD7779_REG_GPIO_DATA 0x18
51 #define AD7779_REG_CH_OFFSET_UPPER_BYTE(ch) (0x1C + (ch) * 6)
52 #define AD7779_REG_CH_OFFSET_LOWER_BYTE(ch) (0x1E + (ch) * 6)
53 #define AD7779_REG_CH_GAIN_UPPER_BYTE(ch) (0x1F + (ch) * 6)
54 #define AD7779_REG_CH_OFFSET_MID_BYTE(ch) (0x1D + (ch) * 6)
55 #define AD7779_REG_CH_GAIN_MID_BYTE(ch) (0x20 + (ch) * 6)
56 #define AD7779_REG_CH_ERR_REG(ch) (0x4C + (ch))
57 #define AD7779_REG_CH0_1_SAT_ERR 0x54
58 #define AD7779_REG_CH_GAIN_LOWER_BYTE(ch) (0x21 + (ch) * 6)
59 #define AD7779_REG_CH2_3_SAT_ERR 0x55
60 #define AD7779_REG_CH4_5_SAT_ERR 0x56
61 #define AD7779_REG_CH6_7_SAT_ERR 0x57
62 #define AD7779_REG_CHX_ERR_REG_EN 0x58
63 #define AD7779_REG_GEN_ERR_REG_1 0x59
64 #define AD7779_REG_GEN_ERR_REG_1_EN 0x5A
65 #define AD7779_REG_GEN_ERR_REG_2 0x5B
66 #define AD7779_REG_GEN_ERR_REG_2_EN 0x5C
67 #define AD7779_REG_STATUS_REG_1 0x5D
68 #define AD7779_REG_STATUS_REG_2 0x5E
69 #define AD7779_REG_STATUS_REG_3 0x5F
70 #define AD7779_REG_SRC_N_MSB 0x60
71 #define AD7779_REG_SRC_N_LSB 0x61
72 #define AD7779_REG_SRC_IF_MSB 0x62
73 #define AD7779_REG_SRC_IF_LSB 0x63
74 #define AD7779_REG_SRC_UPDATE 0x64
76 #define AD7779_FILTER_MSK BIT(6)
77 #define AD7779_MOD_POWERMODE_MSK BIT(6)
78 #define AD7779_MOD_PDB_REFOUT_MSK BIT(4)
79 #define AD7779_MOD_SPI_EN_MSK BIT(4)
80 #define AD7779_USRMOD_INIT_MSK GENMASK(6, 4)
82 /* AD7779_REG_DOUT_FORMAT */
83 #define AD7779_DOUT_FORMAT_MSK GENMASK(7, 6)
84 #define AD7779_DOUT_HEADER_FORMAT BIT(5)
85 #define AD7779_DCLK_CLK_DIV_MSK GENMASK(3, 1)
87 #define AD7779_REFMUX_CTRL_MSK GENMASK(7, 6)
88 #define AD7779_SPI_CRC_EN_MSK BIT(0)
90 #define AD7779_MAXCLK_LOWPOWER (4096 * HZ_PER_KHZ)
91 #define AD7779_NUM_CHANNELS 8
92 #define AD7779_RESET_BUF_SIZE 8
93 #define AD7779_CHAN_DATA_SIZE 4
95 #define AD7779_LOWPOWER_DIV 512
96 #define AD7779_HIGHPOWER_DIV 2048
98 #define AD7779_SINC3_MAXFREQ (16 * HZ_PER_KHZ)
99 #define AD7779_SINC5_MAXFREQ (128 * HZ_PER_KHZ)
101 #define AD7779_DEFAULT_SAMPLING_FREQ (8 * HZ_PER_KHZ)
102 #define AD7779_DEFAULT_SAMPLING_2LINE (4 * HZ_PER_KHZ)
103 #define AD7779_DEFAULT_SAMPLING_1LINE (2 * HZ_PER_KHZ)
105 #define AD7779_SPIMODE_MAX_SAMP_FREQ (16 * HZ_PER_KHZ)
107 #define GAIN_REL 0x555555
108 #define AD7779_FREQ_MSB_MSK GENMASK(15, 8)
109 #define AD7779_FREQ_LSB_MSK GENMASK(7, 0)
110 #define AD7779_UPPER GENMASK(23, 16)
111 #define AD7779_MID GENMASK(15, 8)
112 #define AD7779_LOWER GENMASK(7, 0)
114 #define AD7779_REG_MSK GENMASK(6, 0)
116 #define AD7779_CRC8_POLY 0x07
117 DECLARE_CRC8_TABLE(ad7779_crc8_table);
124 enum ad7779_variant {
130 enum ad7779_power_mode {
135 struct ad7779_chip_info {
137 struct iio_chan_spec const *channels;
140 struct ad7779_state {
141 struct spi_device *spi;
142 const struct ad7779_chip_info *chip_info;
144 struct iio_trigger *trig;
145 struct completion completion;
146 unsigned int sampling_freq;
147 enum ad7779_filter filter_enabled;
149 * DMA (thus cache coherency maintenance) requires the
150 * transfer buffers to live in their own cache lines.
154 aligned_s64 timestamp;
155 } data __aligned(IIO_DMA_MINALIGN);
162 static const char * const ad7779_filter_type[] = {
163 [AD7779_SINC3] = "sinc3",
164 [AD7779_SINC5] = "sinc5",
167 static const char * const ad7779_power_supplies[] = {
168 "avdd1", "avdd2", "avdd4",
171 static int ad7779_spi_read(struct ad7779_state *st, u8 reg, u8 *rbuf)
176 struct spi_transfer t = {
177 .tx_buf = st->reg_tx_buf,
178 .rx_buf = st->reg_rx_buf,
181 st->reg_tx_buf[0] = AD7779_SPI_READ_CMD | FIELD_GET(AD7779_REG_MSK, reg);
182 st->reg_tx_buf[1] = 0;
184 if (reg == AD7779_REG_GEN_ERR_REG_1_EN) {
188 st->reg_tx_buf[2] = crc8(ad7779_crc8_table, st->reg_tx_buf,
192 ret = spi_sync_transfer(st->spi, &t, 1);
196 crc_buf[0] = AD7779_SPI_READ_CMD | FIELD_GET(AD7779_REG_MSK, reg);
197 crc_buf[1] = st->reg_rx_buf[1];
198 exp_crc = crc8(ad7779_crc8_table, crc_buf, ARRAY_SIZE(crc_buf), 0);
199 if (reg != AD7779_REG_GEN_ERR_REG_1_EN && exp_crc != st->reg_rx_buf[2]) {
200 dev_err(&st->spi->dev, "Bad CRC %x, expected %x",
201 st->reg_rx_buf[2], exp_crc);
204 *rbuf = st->reg_rx_buf[1];
209 static int ad7779_spi_write(struct ad7779_state *st, u8 reg, u8 val)
213 st->reg_tx_buf[0] = FIELD_GET(AD7779_REG_MSK, reg);
214 st->reg_tx_buf[1] = val;
215 if (reg == AD7779_REG_GEN_ERR_REG_1_EN)
218 st->reg_tx_buf[2] = crc8(ad7779_crc8_table, st->reg_tx_buf,
221 return spi_write(st->spi, st->reg_tx_buf, length);
224 static int ad7779_spi_write_mask(struct ad7779_state *st, u8 reg, u8 mask,
230 ret = ad7779_spi_read(st, reg, &data);
234 regval = (data & ~mask) | (val & mask);
239 return ad7779_spi_write(st, reg, regval);
242 static int ad7779_reg_access(struct iio_dev *indio_dev,
244 unsigned int writeval,
245 unsigned int *readval)
247 struct ad7779_state *st = iio_priv(indio_dev);
252 ret = ad7779_spi_read(st, reg, &rval);
257 return ad7779_spi_write(st, reg, writeval);
260 static int ad7779_set_sampling_frequency(struct ad7779_state *st,
261 unsigned int sampling_freq)
267 unsigned int decimal;
268 unsigned int freq_khz;
270 if (st->filter_enabled == AD7779_SINC3 &&
271 sampling_freq > AD7779_SINC3_MAXFREQ)
274 if (st->filter_enabled == AD7779_SINC5 &&
275 sampling_freq > AD7779_SINC5_MAXFREQ)
278 if (sampling_freq > AD7779_SPIMODE_MAX_SAMP_FREQ)
281 div = AD7779_HIGHPOWER_DIV;
283 freq_khz = sampling_freq / HZ_PER_KHZ;
284 dec = div / freq_khz;
285 frac = div % freq_khz;
287 ret = ad7779_spi_write(st, AD7779_REG_SRC_N_MSB,
288 FIELD_GET(AD7779_FREQ_MSB_MSK, dec));
291 ret = ad7779_spi_write(st, AD7779_REG_SRC_N_LSB,
292 FIELD_GET(AD7779_FREQ_LSB_MSK, dec));
298 * In order to obtain the first three decimals of the decimation
299 * the initial number is multiplied with 10^3 prior to the
300 * division, then the original division result is subtracted and
301 * the number is divided by 10^3.
303 decimal = ((mult_frac(div, KILO, freq_khz) - dec * KILO) << 16)
305 ret = ad7779_spi_write(st, AD7779_REG_SRC_N_MSB,
306 FIELD_GET(AD7779_FREQ_MSB_MSK, decimal));
309 ret = ad7779_spi_write(st, AD7779_REG_SRC_N_LSB,
310 FIELD_GET(AD7779_FREQ_LSB_MSK, decimal));
314 ret = ad7779_spi_write(st, AD7779_REG_SRC_N_MSB,
315 FIELD_GET(AD7779_FREQ_MSB_MSK, 0x0));
318 ret = ad7779_spi_write(st, AD7779_REG_SRC_N_LSB,
319 FIELD_GET(AD7779_FREQ_LSB_MSK, 0x0));
323 ret = ad7779_spi_write(st, AD7779_REG_SRC_UPDATE, BIT(0));
327 /* SRC update settling time */
330 ret = ad7779_spi_write(st, AD7779_REG_SRC_UPDATE, 0x0);
334 /* SRC update settling time */
337 st->sampling_freq = sampling_freq;
342 static int ad7779_get_filter(struct iio_dev *indio_dev,
343 struct iio_chan_spec const *chan)
345 struct ad7779_state *st = iio_priv(indio_dev);
349 ret = ad7779_spi_read(st, AD7779_REG_GENERAL_USER_CONFIG_2, &temp);
353 return FIELD_GET(AD7779_FILTER_MSK, temp);
356 static int ad7779_set_filter(struct iio_dev *indio_dev,
357 struct iio_chan_spec const *chan,
360 struct ad7779_state *st = iio_priv(indio_dev);
363 ret = ad7779_spi_write_mask(st,
364 AD7779_REG_GENERAL_USER_CONFIG_2,
366 FIELD_PREP(AD7779_FILTER_MSK, mode));
370 ret = ad7779_set_sampling_frequency(st, st->sampling_freq);
374 st->filter_enabled = mode;
379 static int ad7779_get_calibscale(struct ad7779_state *st, int channel)
384 ret = ad7779_spi_read(st, AD7779_REG_CH_GAIN_LOWER_BYTE(channel),
389 ret = ad7779_spi_read(st, AD7779_REG_CH_GAIN_MID_BYTE(channel),
394 ret = ad7779_spi_read(st, AD7779_REG_CH_GAIN_UPPER_BYTE(channel),
399 return get_unaligned_be24(calibscale);
402 static int ad7779_set_calibscale(struct ad7779_state *st, int channel, int val)
409 * The gain value is relative to 0x555555, which represents a gain of 1
411 gain = DIV_ROUND_CLOSEST_ULL((u64)val * 5592405LL, MEGA);
412 put_unaligned_be24(gain, gain_bytes);
413 ret = ad7779_spi_write(st, AD7779_REG_CH_GAIN_UPPER_BYTE(channel),
418 ret = ad7779_spi_write(st, AD7779_REG_CH_GAIN_MID_BYTE(channel),
423 return ad7779_spi_write(st, AD7779_REG_CH_GAIN_LOWER_BYTE(channel),
427 static int ad7779_get_calibbias(struct ad7779_state *st, int channel)
432 ret = ad7779_spi_read(st, AD7779_REG_CH_OFFSET_LOWER_BYTE(channel),
437 ret = ad7779_spi_read(st, AD7779_REG_CH_OFFSET_MID_BYTE(channel),
442 ret = ad7779_spi_read(st, AD7779_REG_CH_OFFSET_UPPER_BYTE(channel),
447 return get_unaligned_be24(calibbias);
450 static int ad7779_set_calibbias(struct ad7779_state *st, int channel, int val)
455 put_unaligned_be24(val, calibbias);
456 ret = ad7779_spi_write(st, AD7779_REG_CH_OFFSET_UPPER_BYTE(channel),
461 ret = ad7779_spi_write(st, AD7779_REG_CH_OFFSET_MID_BYTE(channel),
466 return ad7779_spi_write(st, AD7779_REG_CH_OFFSET_LOWER_BYTE(channel),
470 static int ad7779_read_raw(struct iio_dev *indio_dev,
471 struct iio_chan_spec const *chan, int *val,
472 int *val2, long mask)
474 struct ad7779_state *st = iio_priv(indio_dev);
477 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
479 case IIO_CHAN_INFO_CALIBSCALE:
480 ret = ad7779_get_calibscale(st, chan->channel);
485 return IIO_VAL_FRACTIONAL;
486 case IIO_CHAN_INFO_CALIBBIAS:
487 ret = ad7779_get_calibbias(st, chan->channel);
492 case IIO_CHAN_INFO_SAMP_FREQ:
493 *val = st->sampling_freq;
504 static int ad7779_write_raw(struct iio_dev *indio_dev,
505 struct iio_chan_spec const *chan, int val, int val2,
508 struct ad7779_state *st = iio_priv(indio_dev);
510 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
512 case IIO_CHAN_INFO_CALIBSCALE:
513 return ad7779_set_calibscale(st, chan->channel, val2);
514 case IIO_CHAN_INFO_CALIBBIAS:
515 return ad7779_set_calibbias(st, chan->channel, val);
516 case IIO_CHAN_INFO_SAMP_FREQ:
517 return ad7779_set_sampling_frequency(st, val);
525 static int ad7779_buffer_preenable(struct iio_dev *indio_dev)
528 struct ad7779_state *st = iio_priv(indio_dev);
530 ret = ad7779_spi_write_mask(st,
531 AD7779_REG_GENERAL_USER_CONFIG_3,
532 AD7779_MOD_SPI_EN_MSK,
533 FIELD_PREP(AD7779_MOD_SPI_EN_MSK, 1));
538 * DRDY output cannot be disabled at device level therefore we mask
539 * the irq at host end.
541 enable_irq(st->spi->irq);
546 static int ad7779_buffer_postdisable(struct iio_dev *indio_dev)
548 struct ad7779_state *st = iio_priv(indio_dev);
550 disable_irq(st->spi->irq);
552 return ad7779_spi_write(st, AD7779_REG_GENERAL_USER_CONFIG_3,
556 static irqreturn_t ad7779_trigger_handler(int irq, void *p)
558 struct iio_poll_func *pf = p;
559 struct iio_dev *indio_dev = pf->indio_dev;
560 struct ad7779_state *st = iio_priv(indio_dev);
562 struct spi_transfer t = {
563 .rx_buf = st->data.chans,
564 .tx_buf = st->spidata_tx,
565 .len = AD7779_NUM_CHANNELS * AD7779_CHAN_DATA_SIZE,
568 st->spidata_tx[0] = AD7779_SPI_READ_CMD;
569 ret = spi_sync_transfer(st->spi, &t, 1);
571 dev_err(&st->spi->dev, "SPI transfer error in IRQ handler");
575 iio_push_to_buffers_with_timestamp(indio_dev, &st->data, pf->timestamp);
578 iio_trigger_notify_done(indio_dev->trig);
582 static int ad7779_reset(struct iio_dev *indio_dev, struct gpio_desc *reset_gpio)
584 struct ad7779_state *st = iio_priv(indio_dev);
586 struct spi_transfer t = {
587 .tx_buf = st->reset_buf,
592 gpiod_set_value(reset_gpio, 1);
593 /* Delay for reset to occur is 225 microseconds */
597 memset(st->reset_buf, 0xff, sizeof(st->reset_buf));
598 ret = spi_sync_transfer(st->spi, &t, 1);
603 /* Delay for reset to occur is 225 microseconds */
609 static const struct iio_info ad7779_info = {
610 .read_raw = ad7779_read_raw,
611 .write_raw = ad7779_write_raw,
612 .debugfs_reg_access = &ad7779_reg_access,
615 static const struct iio_enum ad7779_filter_enum = {
616 .items = ad7779_filter_type,
617 .num_items = ARRAY_SIZE(ad7779_filter_type),
618 .get = ad7779_get_filter,
619 .set = ad7779_set_filter,
622 static const struct iio_chan_spec_ext_info ad7779_ext_filter[] = {
623 IIO_ENUM("filter_type", IIO_SHARED_BY_ALL, &ad7779_filter_enum),
624 IIO_ENUM_AVAILABLE("filter_type", IIO_SHARED_BY_ALL,
625 &ad7779_filter_enum),
629 #define AD777x_CHAN_S(index, _ext_info) \
631 .type = IIO_VOLTAGE, \
632 .info_mask_separate = BIT(IIO_CHAN_INFO_CALIBSCALE) | \
633 BIT(IIO_CHAN_INFO_CALIBBIAS), \
634 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
635 .address = (index), \
637 .channel = (index), \
638 .scan_index = (index), \
639 .ext_info = (_ext_info), \
644 .endianness = IIO_BE, \
648 #define AD777x_CHAN_NO_FILTER_S(index) \
649 AD777x_CHAN_S(index, NULL)
651 #define AD777x_CHAN_FILTER_S(index) \
652 AD777x_CHAN_S(index, ad7779_ext_filter)
653 static const struct iio_chan_spec ad7779_channels[] = {
654 AD777x_CHAN_NO_FILTER_S(0),
655 AD777x_CHAN_NO_FILTER_S(1),
656 AD777x_CHAN_NO_FILTER_S(2),
657 AD777x_CHAN_NO_FILTER_S(3),
658 AD777x_CHAN_NO_FILTER_S(4),
659 AD777x_CHAN_NO_FILTER_S(5),
660 AD777x_CHAN_NO_FILTER_S(6),
661 AD777x_CHAN_NO_FILTER_S(7),
662 IIO_CHAN_SOFT_TIMESTAMP(8),
665 static const struct iio_chan_spec ad7779_channels_filter[] = {
666 AD777x_CHAN_FILTER_S(0),
667 AD777x_CHAN_FILTER_S(1),
668 AD777x_CHAN_FILTER_S(2),
669 AD777x_CHAN_FILTER_S(3),
670 AD777x_CHAN_FILTER_S(4),
671 AD777x_CHAN_FILTER_S(5),
672 AD777x_CHAN_FILTER_S(6),
673 AD777x_CHAN_FILTER_S(7),
674 IIO_CHAN_SOFT_TIMESTAMP(8),
677 static const struct iio_buffer_setup_ops ad7779_buffer_setup_ops = {
678 .preenable = ad7779_buffer_preenable,
679 .postdisable = ad7779_buffer_postdisable,
682 static const struct iio_trigger_ops ad7779_trigger_ops = {
683 .validate_device = iio_trigger_validate_own_device,
686 static int ad7779_conf(struct ad7779_state *st, struct gpio_desc *start_gpio)
690 ret = ad7779_spi_write_mask(st, AD7779_REG_GEN_ERR_REG_1_EN,
691 AD7779_SPI_CRC_EN_MSK,
692 FIELD_PREP(AD7779_SPI_CRC_EN_MSK, 1));
696 ret = ad7779_spi_write_mask(st, AD7779_REG_GENERAL_USER_CONFIG_1,
697 AD7779_USRMOD_INIT_MSK,
698 FIELD_PREP(AD7779_USRMOD_INIT_MSK, 5));
702 ret = ad7779_spi_write_mask(st, AD7779_REG_DOUT_FORMAT,
703 AD7779_DCLK_CLK_DIV_MSK,
704 FIELD_PREP(AD7779_DCLK_CLK_DIV_MSK, 1));
708 ret = ad7779_spi_write_mask(st, AD7779_REG_ADC_MUX_CONFIG,
709 AD7779_REFMUX_CTRL_MSK,
710 FIELD_PREP(AD7779_REFMUX_CTRL_MSK, 1));
714 ret = ad7779_set_sampling_frequency(st, AD7779_DEFAULT_SAMPLING_FREQ);
718 gpiod_set_value(start_gpio, 0);
719 /* Start setup time */
721 gpiod_set_value(start_gpio, 1);
722 /* Start setup time */
724 gpiod_set_value(start_gpio, 0);
725 /* Start setup time */
731 static int ad7779_probe(struct spi_device *spi)
733 struct iio_dev *indio_dev;
734 struct ad7779_state *st;
735 struct gpio_desc *reset_gpio, *start_gpio;
736 struct device *dev = &spi->dev;
740 return dev_err_probe(dev, ret, "DRDY irq not present\n");
742 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
746 st = iio_priv(indio_dev);
748 ret = devm_regulator_bulk_get_enable(dev,
749 ARRAY_SIZE(ad7779_power_supplies),
750 ad7779_power_supplies);
752 return dev_err_probe(dev, ret,
753 "failed to get and enable supplies\n");
755 st->mclk = devm_clk_get_enabled(dev, "mclk");
756 if (IS_ERR(st->mclk))
757 return PTR_ERR(st->mclk);
759 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
760 if (IS_ERR(reset_gpio))
761 return PTR_ERR(reset_gpio);
763 start_gpio = devm_gpiod_get(dev, "start", GPIOD_OUT_HIGH);
764 if (IS_ERR(start_gpio))
765 return PTR_ERR(start_gpio);
767 crc8_populate_msb(ad7779_crc8_table, AD7779_CRC8_POLY);
770 st->chip_info = spi_get_device_match_data(spi);
774 ret = ad7779_reset(indio_dev, reset_gpio);
778 ret = ad7779_conf(st, start_gpio);
782 indio_dev->name = st->chip_info->name;
783 indio_dev->info = &ad7779_info;
784 indio_dev->modes = INDIO_DIRECT_MODE;
785 indio_dev->channels = st->chip_info->channels;
786 indio_dev->num_channels = ARRAY_SIZE(ad7779_channels);
788 st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
789 iio_device_id(indio_dev));
793 st->trig->ops = &ad7779_trigger_ops;
795 iio_trigger_set_drvdata(st->trig, st);
797 ret = devm_request_irq(dev, spi->irq, iio_trigger_generic_data_rdy_poll,
798 IRQF_ONESHOT | IRQF_NO_AUTOEN, indio_dev->name,
801 return dev_err_probe(dev, ret, "request IRQ %d failed\n",
804 ret = devm_iio_trigger_register(dev, st->trig);
808 indio_dev->trig = iio_trigger_get(st->trig);
810 init_completion(&st->completion);
812 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
813 &iio_pollfunc_store_time,
814 &ad7779_trigger_handler,
815 &ad7779_buffer_setup_ops);
819 ret = ad7779_spi_write_mask(st, AD7779_REG_DOUT_FORMAT,
820 AD7779_DCLK_CLK_DIV_MSK,
821 FIELD_PREP(AD7779_DCLK_CLK_DIV_MSK, 7));
825 return devm_iio_device_register(dev, indio_dev);
828 static int ad7779_suspend(struct device *dev)
830 struct iio_dev *indio_dev = dev_get_drvdata(dev);
831 struct ad7779_state *st = iio_priv(indio_dev);
833 return ad7779_spi_write_mask(st, AD7779_REG_GENERAL_USER_CONFIG_1,
834 AD7779_MOD_POWERMODE_MSK,
835 FIELD_PREP(AD7779_MOD_POWERMODE_MSK,
839 static int ad7779_resume(struct device *dev)
841 struct iio_dev *indio_dev = dev_get_drvdata(dev);
842 struct ad7779_state *st = iio_priv(indio_dev);
844 return ad7779_spi_write_mask(st, AD7779_REG_GENERAL_USER_CONFIG_1,
845 AD7779_MOD_POWERMODE_MSK,
846 FIELD_PREP(AD7779_MOD_POWERMODE_MSK,
850 static DEFINE_SIMPLE_DEV_PM_OPS(ad7779_pm_ops, ad7779_suspend, ad7779_resume);
852 static const struct ad7779_chip_info ad7770_chip_info = {
854 .channels = ad7779_channels,
857 static const struct ad7779_chip_info ad7771_chip_info = {
859 .channels = ad7779_channels_filter,
862 static const struct ad7779_chip_info ad7779_chip_info = {
864 .channels = ad7779_channels,
867 static const struct spi_device_id ad7779_id[] = {
870 .driver_data = (kernel_ulong_t)&ad7770_chip_info,
874 .driver_data = (kernel_ulong_t)&ad7771_chip_info,
878 .driver_data = (kernel_ulong_t)&ad7779_chip_info,
882 MODULE_DEVICE_TABLE(spi, ad7779_id);
884 static const struct of_device_id ad7779_of_table[] = {
886 .compatible = "adi,ad7770",
887 .data = &ad7770_chip_info,
890 .compatible = "adi,ad7771",
891 .data = &ad7771_chip_info,
894 .compatible = "adi,ad7779",
895 .data = &ad7779_chip_info,
899 MODULE_DEVICE_TABLE(of, ad7779_of_table);
901 static struct spi_driver ad7779_driver = {
904 .pm = pm_sleep_ptr(&ad7779_pm_ops),
905 .of_match_table = ad7779_of_table,
907 .probe = ad7779_probe,
908 .id_table = ad7779_id,
910 module_spi_driver(ad7779_driver);
913 MODULE_DESCRIPTION("Analog Devices AD7779 ADC");
914 MODULE_LICENSE("GPL");