xref: /linux/drivers/iio/adc/ad4080.c (revision 0d5ec7919f3747193f051036b2301734a4b5e1d6)
1*6b31ba18SAntoniu Miclaus // SPDX-License-Identifier: GPL-2.0-only
2*6b31ba18SAntoniu Miclaus /*
3*6b31ba18SAntoniu Miclaus  * Analog Devices AD4080 SPI ADC driver
4*6b31ba18SAntoniu Miclaus  *
5*6b31ba18SAntoniu Miclaus  * Copyright 2025 Analog Devices Inc.
6*6b31ba18SAntoniu Miclaus  */
7*6b31ba18SAntoniu Miclaus 
8*6b31ba18SAntoniu Miclaus #include <linux/array_size.h>
9*6b31ba18SAntoniu Miclaus #include <linux/bitfield.h>
10*6b31ba18SAntoniu Miclaus #include <linux/bits.h>
11*6b31ba18SAntoniu Miclaus #include <linux/clk.h>
12*6b31ba18SAntoniu Miclaus #include <linux/device.h>
13*6b31ba18SAntoniu Miclaus #include <linux/err.h>
14*6b31ba18SAntoniu Miclaus #include <linux/iio/backend.h>
15*6b31ba18SAntoniu Miclaus #include <linux/iio/iio.h>
16*6b31ba18SAntoniu Miclaus #include <linux/mod_devicetable.h>
17*6b31ba18SAntoniu Miclaus #include <linux/module.h>
18*6b31ba18SAntoniu Miclaus #include <linux/mutex.h>
19*6b31ba18SAntoniu Miclaus #include <linux/regmap.h>
20*6b31ba18SAntoniu Miclaus #include <linux/regulator/consumer.h>
21*6b31ba18SAntoniu Miclaus #include <linux/spi/spi.h>
22*6b31ba18SAntoniu Miclaus #include <linux/types.h>
23*6b31ba18SAntoniu Miclaus #include <linux/unaligned.h>
24*6b31ba18SAntoniu Miclaus #include <linux/units.h>
25*6b31ba18SAntoniu Miclaus 
26*6b31ba18SAntoniu Miclaus /* Register Definition */
27*6b31ba18SAntoniu Miclaus #define AD4080_REG_INTERFACE_CONFIG_A				0x00
28*6b31ba18SAntoniu Miclaus #define AD4080_REG_INTERFACE_CONFIG_B				0x01
29*6b31ba18SAntoniu Miclaus #define AD4080_REG_DEVICE_CONFIG				0x02
30*6b31ba18SAntoniu Miclaus #define AD4080_REG_CHIP_TYPE					0x03
31*6b31ba18SAntoniu Miclaus #define AD4080_REG_PRODUCT_ID_L					0x04
32*6b31ba18SAntoniu Miclaus #define AD4080_REG_PRODUCT_ID_H					0x05
33*6b31ba18SAntoniu Miclaus #define AD4080_REG_CHIP_GRADE					0x06
34*6b31ba18SAntoniu Miclaus #define AD4080_REG_SCRATCH_PAD					0x0A
35*6b31ba18SAntoniu Miclaus #define AD4080_REG_SPI_REVISION					0x0B
36*6b31ba18SAntoniu Miclaus #define AD4080_REG_VENDOR_L					0x0C
37*6b31ba18SAntoniu Miclaus #define AD4080_REG_VENDOR_H					0x0D
38*6b31ba18SAntoniu Miclaus #define AD4080_REG_STREAM_MODE					0x0E
39*6b31ba18SAntoniu Miclaus #define AD4080_REG_TRANSFER_CONFIG				0x0F
40*6b31ba18SAntoniu Miclaus #define AD4080_REG_INTERFACE_CONFIG_C				0x10
41*6b31ba18SAntoniu Miclaus #define AD4080_REG_INTERFACE_STATUS_A				0x11
42*6b31ba18SAntoniu Miclaus #define AD4080_REG_DEVICE_STATUS				0x14
43*6b31ba18SAntoniu Miclaus #define AD4080_REG_ADC_DATA_INTF_CONFIG_A			0x15
44*6b31ba18SAntoniu Miclaus #define AD4080_REG_ADC_DATA_INTF_CONFIG_B			0x16
45*6b31ba18SAntoniu Miclaus #define AD4080_REG_ADC_DATA_INTF_CONFIG_C			0x17
46*6b31ba18SAntoniu Miclaus #define AD4080_REG_PWR_CTRL					0x18
47*6b31ba18SAntoniu Miclaus #define AD4080_REG_GPIO_CONFIG_A				0x19
48*6b31ba18SAntoniu Miclaus #define AD4080_REG_GPIO_CONFIG_B				0x1A
49*6b31ba18SAntoniu Miclaus #define AD4080_REG_GPIO_CONFIG_C				0x1B
50*6b31ba18SAntoniu Miclaus #define AD4080_REG_GENERAL_CONFIG				0x1C
51*6b31ba18SAntoniu Miclaus #define AD4080_REG_FIFO_WATERMARK_LSB				0x1D
52*6b31ba18SAntoniu Miclaus #define AD4080_REG_FIFO_WATERMARK_MSB				0x1E
53*6b31ba18SAntoniu Miclaus #define AD4080_REG_EVENT_HYSTERESIS_LSB				0x1F
54*6b31ba18SAntoniu Miclaus #define AD4080_REG_EVENT_HYSTERESIS_MSB				0x20
55*6b31ba18SAntoniu Miclaus #define AD4080_REG_EVENT_DETECTION_HI_LSB			0x21
56*6b31ba18SAntoniu Miclaus #define AD4080_REG_EVENT_DETECTION_HI_MSB			0x22
57*6b31ba18SAntoniu Miclaus #define AD4080_REG_EVENT_DETECTION_LO_LSB			0x23
58*6b31ba18SAntoniu Miclaus #define AD4080_REG_EVENT_DETECTION_LO_MSB			0x24
59*6b31ba18SAntoniu Miclaus #define AD4080_REG_OFFSET_LSB					0x25
60*6b31ba18SAntoniu Miclaus #define AD4080_REG_OFFSET_MSB					0x26
61*6b31ba18SAntoniu Miclaus #define AD4080_REG_GAIN_LSB					0x27
62*6b31ba18SAntoniu Miclaus #define AD4080_REG_GAIN_MSB					0x28
63*6b31ba18SAntoniu Miclaus #define AD4080_REG_FILTER_CONFIG				0x29
64*6b31ba18SAntoniu Miclaus 
65*6b31ba18SAntoniu Miclaus /* AD4080_REG_INTERFACE_CONFIG_A Bit Definition */
66*6b31ba18SAntoniu Miclaus #define AD4080_INTERFACE_CONFIG_A_SW_RESET			(BIT(7) | BIT(0))
67*6b31ba18SAntoniu Miclaus #define AD4080_INTERFACE_CONFIG_A_ADDR_ASC			BIT(5)
68*6b31ba18SAntoniu Miclaus #define AD4080_INTERFACE_CONFIG_A_SDO_ENABLE			BIT(4)
69*6b31ba18SAntoniu Miclaus 
70*6b31ba18SAntoniu Miclaus /* AD4080_REG_INTERFACE_CONFIG_B Bit Definition */
71*6b31ba18SAntoniu Miclaus #define AD4080_INTERFACE_CONFIG_B_SINGLE_INST			BIT(7)
72*6b31ba18SAntoniu Miclaus #define AD4080_INTERFACE_CONFIG_B_SHORT_INST			BIT(3)
73*6b31ba18SAntoniu Miclaus 
74*6b31ba18SAntoniu Miclaus /* AD4080_REG_DEVICE_CONFIG Bit Definition */
75*6b31ba18SAntoniu Miclaus #define AD4080_DEVICE_CONFIG_OPERATING_MODES_MSK		GENMASK(1, 0)
76*6b31ba18SAntoniu Miclaus 
77*6b31ba18SAntoniu Miclaus /* AD4080_REG_TRANSFER_CONFIG Bit Definition */
78*6b31ba18SAntoniu Miclaus #define AD4080_TRANSFER_CONFIG_KEEP_STREAM_LENGTH_VAL		BIT(2)
79*6b31ba18SAntoniu Miclaus 
80*6b31ba18SAntoniu Miclaus /* AD4080_REG_INTERFACE_CONFIG_C Bit Definition */
81*6b31ba18SAntoniu Miclaus #define AD4080_INTERFACE_CONFIG_C_STRICT_REG_ACCESS		BIT(5)
82*6b31ba18SAntoniu Miclaus 
83*6b31ba18SAntoniu Miclaus /* AD4080_REG_ADC_DATA_INTF_CONFIG_A Bit Definition */
84*6b31ba18SAntoniu Miclaus #define AD4080_ADC_DATA_INTF_CONFIG_A_RESERVED_CONFIG_A		BIT(6)
85*6b31ba18SAntoniu Miclaus #define AD4080_ADC_DATA_INTF_CONFIG_A_INTF_CHK_EN		BIT(4)
86*6b31ba18SAntoniu Miclaus #define AD4080_ADC_DATA_INTF_CONFIG_A_SPI_LVDS_LANES		BIT(2)
87*6b31ba18SAntoniu Miclaus #define AD4080_ADC_DATA_INTF_CONFIG_A_DATA_INTF_MODE		BIT(0)
88*6b31ba18SAntoniu Miclaus 
89*6b31ba18SAntoniu Miclaus /* AD4080_REG_ADC_DATA_INTF_CONFIG_B Bit Definition */
90*6b31ba18SAntoniu Miclaus #define AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_CLK_CNT_MSK	GENMASK(7, 4)
91*6b31ba18SAntoniu Miclaus #define AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_SELF_CLK_MODE	BIT(3)
92*6b31ba18SAntoniu Miclaus #define AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_EN		BIT(0)
93*6b31ba18SAntoniu Miclaus 
94*6b31ba18SAntoniu Miclaus /* AD4080_REG_ADC_DATA_INTF_CONFIG_C Bit Definition */
95*6b31ba18SAntoniu Miclaus #define AD4080_ADC_DATA_INTF_CONFIG_C_LVDS_VOD_MSK		GENMASK(6, 4)
96*6b31ba18SAntoniu Miclaus 
97*6b31ba18SAntoniu Miclaus /* AD4080_REG_PWR_CTRL Bit Definition */
98*6b31ba18SAntoniu Miclaus #define AD4080_PWR_CTRL_ANA_DIG_LDO_PD				BIT(1)
99*6b31ba18SAntoniu Miclaus #define AD4080_PWR_CTRL_INTF_LDO_PD				BIT(0)
100*6b31ba18SAntoniu Miclaus 
101*6b31ba18SAntoniu Miclaus /* AD4080_REG_GPIO_CONFIG_A Bit Definition */
102*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_A_GPO_1_EN				BIT(1)
103*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_A_GPO_0_EN				BIT(0)
104*6b31ba18SAntoniu Miclaus 
105*6b31ba18SAntoniu Miclaus /* AD4080_REG_GPIO_CONFIG_B Bit Definition */
106*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_1_SEL_MSK			GENMASK(7, 4)
107*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_0_SEL_MSK			GENMASK(3, 0)
108*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_SPI_SDO			0
109*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_FIFO_FULL			1
110*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_FIFO_READ_DONE		2
111*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_FILTER_RES_RDY		3
112*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_H_THRESH			4
113*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_L_THRESH			5
114*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_STATUS_ALERT			6
115*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_GPIO_DATA			7
116*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_FILTER_SYNC			8
117*6b31ba18SAntoniu Miclaus #define AD4080_GPIO_CONFIG_B_GPIO_EXTERNAL_EVENT		9
118*6b31ba18SAntoniu Miclaus 
119*6b31ba18SAntoniu Miclaus /* AD4080_REG_FIFO_CONFIG Bit Definition */
120*6b31ba18SAntoniu Miclaus #define AD4080_FIFO_CONFIG_FIFO_MODE_MSK			GENMASK(1, 0)
121*6b31ba18SAntoniu Miclaus 
122*6b31ba18SAntoniu Miclaus /* AD4080_REG_FILTER_CONFIG Bit Definition */
123*6b31ba18SAntoniu Miclaus #define AD4080_FILTER_CONFIG_SINC_DEC_RATE_MSK			GENMASK(6, 3)
124*6b31ba18SAntoniu Miclaus #define AD4080_FILTER_CONFIG_FILTER_SEL_MSK			GENMASK(1, 0)
125*6b31ba18SAntoniu Miclaus 
126*6b31ba18SAntoniu Miclaus /* Miscellaneous Definitions */
127*6b31ba18SAntoniu Miclaus #define AD4080_SPI_READ						BIT(7)
128*6b31ba18SAntoniu Miclaus #define AD4080_CHIP_ID						GENMASK(2, 0)
129*6b31ba18SAntoniu Miclaus 
130*6b31ba18SAntoniu Miclaus #define AD4080_LVDS_CNV_CLK_CNT_MAX				7
131*6b31ba18SAntoniu Miclaus 
132*6b31ba18SAntoniu Miclaus #define AD4080_MAX_SAMP_FREQ					40000000
133*6b31ba18SAntoniu Miclaus #define AD4080_MIN_SAMP_FREQ					1250000
134*6b31ba18SAntoniu Miclaus 
135*6b31ba18SAntoniu Miclaus enum ad4080_filter_type {
136*6b31ba18SAntoniu Miclaus 	FILTER_NONE,
137*6b31ba18SAntoniu Miclaus 	SINC_1,
138*6b31ba18SAntoniu Miclaus 	SINC_5,
139*6b31ba18SAntoniu Miclaus 	SINC_5_COMP
140*6b31ba18SAntoniu Miclaus };
141*6b31ba18SAntoniu Miclaus 
142*6b31ba18SAntoniu Miclaus static const unsigned int ad4080_scale_table[][2] = {
143*6b31ba18SAntoniu Miclaus 	{ 6000, 0 },
144*6b31ba18SAntoniu Miclaus };
145*6b31ba18SAntoniu Miclaus 
146*6b31ba18SAntoniu Miclaus static const char *const ad4080_filter_type_iio_enum[] = {
147*6b31ba18SAntoniu Miclaus 	[FILTER_NONE]      = "none",
148*6b31ba18SAntoniu Miclaus 	[SINC_1]           = "sinc1",
149*6b31ba18SAntoniu Miclaus 	[SINC_5]           = "sinc5",
150*6b31ba18SAntoniu Miclaus 	[SINC_5_COMP]      = "sinc5+pf1",
151*6b31ba18SAntoniu Miclaus };
152*6b31ba18SAntoniu Miclaus 
153*6b31ba18SAntoniu Miclaus static const int ad4080_dec_rate_avail[] = {
154*6b31ba18SAntoniu Miclaus 	2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
155*6b31ba18SAntoniu Miclaus };
156*6b31ba18SAntoniu Miclaus 
157*6b31ba18SAntoniu Miclaus static const int ad4080_dec_rate_none[] = { 1 };
158*6b31ba18SAntoniu Miclaus 
159*6b31ba18SAntoniu Miclaus static const char * const ad4080_power_supplies[] = {
160*6b31ba18SAntoniu Miclaus 	"vdd33", "vdd11", "vddldo", "iovdd", "vrefin",
161*6b31ba18SAntoniu Miclaus };
162*6b31ba18SAntoniu Miclaus 
163*6b31ba18SAntoniu Miclaus struct ad4080_chip_info {
164*6b31ba18SAntoniu Miclaus 	const char *name;
165*6b31ba18SAntoniu Miclaus 	unsigned int product_id;
166*6b31ba18SAntoniu Miclaus 	int num_scales;
167*6b31ba18SAntoniu Miclaus 	const unsigned int (*scale_table)[2];
168*6b31ba18SAntoniu Miclaus 	const struct iio_chan_spec *channels;
169*6b31ba18SAntoniu Miclaus 	unsigned int num_channels;
170*6b31ba18SAntoniu Miclaus };
171*6b31ba18SAntoniu Miclaus 
172*6b31ba18SAntoniu Miclaus struct ad4080_state {
173*6b31ba18SAntoniu Miclaus 	struct regmap			*regmap;
174*6b31ba18SAntoniu Miclaus 	struct iio_backend		*back;
175*6b31ba18SAntoniu Miclaus 	const struct ad4080_chip_info	*info;
176*6b31ba18SAntoniu Miclaus 	/*
177*6b31ba18SAntoniu Miclaus 	 * Synchronize access to members the of driver state, and ensure
178*6b31ba18SAntoniu Miclaus 	 * atomicity of consecutive regmap operations.
179*6b31ba18SAntoniu Miclaus 	 */
180*6b31ba18SAntoniu Miclaus 	struct mutex			lock;
181*6b31ba18SAntoniu Miclaus 	unsigned int			num_lanes;
182*6b31ba18SAntoniu Miclaus 	unsigned int			dec_rate;
183*6b31ba18SAntoniu Miclaus 	unsigned long			clk_rate;
184*6b31ba18SAntoniu Miclaus 	enum ad4080_filter_type		filter_type;
185*6b31ba18SAntoniu Miclaus 	bool				lvds_cnv_en;
186*6b31ba18SAntoniu Miclaus };
187*6b31ba18SAntoniu Miclaus 
188*6b31ba18SAntoniu Miclaus static const struct regmap_config ad4080_regmap_config = {
189*6b31ba18SAntoniu Miclaus 	.reg_bits = 16,
190*6b31ba18SAntoniu Miclaus 	.val_bits = 8,
191*6b31ba18SAntoniu Miclaus 	.read_flag_mask = BIT(7),
192*6b31ba18SAntoniu Miclaus 	.max_register = 0x29,
193*6b31ba18SAntoniu Miclaus };
194*6b31ba18SAntoniu Miclaus 
ad4080_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)195*6b31ba18SAntoniu Miclaus static int ad4080_reg_access(struct iio_dev *indio_dev, unsigned int reg,
196*6b31ba18SAntoniu Miclaus 			     unsigned int writeval, unsigned int *readval)
197*6b31ba18SAntoniu Miclaus {
198*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(indio_dev);
199*6b31ba18SAntoniu Miclaus 
200*6b31ba18SAntoniu Miclaus 	if (readval)
201*6b31ba18SAntoniu Miclaus 		return regmap_read(st->regmap, reg, readval);
202*6b31ba18SAntoniu Miclaus 
203*6b31ba18SAntoniu Miclaus 	return regmap_write(st->regmap, reg, writeval);
204*6b31ba18SAntoniu Miclaus }
205*6b31ba18SAntoniu Miclaus 
ad4080_get_scale(struct ad4080_state * st,int * val,int * val2)206*6b31ba18SAntoniu Miclaus static int ad4080_get_scale(struct ad4080_state *st, int *val, int *val2)
207*6b31ba18SAntoniu Miclaus {
208*6b31ba18SAntoniu Miclaus 	unsigned int tmp;
209*6b31ba18SAntoniu Miclaus 
210*6b31ba18SAntoniu Miclaus 	tmp = (st->info->scale_table[0][0] * 1000000ULL) >>
211*6b31ba18SAntoniu Miclaus 		    st->info->channels[0].scan_type.realbits;
212*6b31ba18SAntoniu Miclaus 	*val = tmp / 1000000;
213*6b31ba18SAntoniu Miclaus 	*val2 = tmp % 1000000;
214*6b31ba18SAntoniu Miclaus 
215*6b31ba18SAntoniu Miclaus 	return IIO_VAL_INT_PLUS_NANO;
216*6b31ba18SAntoniu Miclaus }
217*6b31ba18SAntoniu Miclaus 
ad4080_get_dec_rate(struct iio_dev * dev,const struct iio_chan_spec * chan)218*6b31ba18SAntoniu Miclaus static unsigned int ad4080_get_dec_rate(struct iio_dev *dev,
219*6b31ba18SAntoniu Miclaus 					const struct iio_chan_spec *chan)
220*6b31ba18SAntoniu Miclaus {
221*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(dev);
222*6b31ba18SAntoniu Miclaus 	int ret;
223*6b31ba18SAntoniu Miclaus 	unsigned int data;
224*6b31ba18SAntoniu Miclaus 
225*6b31ba18SAntoniu Miclaus 	ret = regmap_read(st->regmap, AD4080_REG_FILTER_CONFIG, &data);
226*6b31ba18SAntoniu Miclaus 	if (ret)
227*6b31ba18SAntoniu Miclaus 		return ret;
228*6b31ba18SAntoniu Miclaus 
229*6b31ba18SAntoniu Miclaus 	return 1 << (FIELD_GET(AD4080_FILTER_CONFIG_SINC_DEC_RATE_MSK, data) + 1);
230*6b31ba18SAntoniu Miclaus }
231*6b31ba18SAntoniu Miclaus 
ad4080_set_dec_rate(struct iio_dev * dev,const struct iio_chan_spec * chan,unsigned int mode)232*6b31ba18SAntoniu Miclaus static int ad4080_set_dec_rate(struct iio_dev *dev,
233*6b31ba18SAntoniu Miclaus 			       const struct iio_chan_spec *chan,
234*6b31ba18SAntoniu Miclaus 			       unsigned int mode)
235*6b31ba18SAntoniu Miclaus {
236*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(dev);
237*6b31ba18SAntoniu Miclaus 
238*6b31ba18SAntoniu Miclaus 	guard(mutex)(&st->lock);
239*6b31ba18SAntoniu Miclaus 
240*6b31ba18SAntoniu Miclaus 	if ((st->filter_type >= SINC_5 && mode >= 512) || mode < 2)
241*6b31ba18SAntoniu Miclaus 		return -EINVAL;
242*6b31ba18SAntoniu Miclaus 
243*6b31ba18SAntoniu Miclaus 	return regmap_update_bits(st->regmap, AD4080_REG_FILTER_CONFIG,
244*6b31ba18SAntoniu Miclaus 				  AD4080_FILTER_CONFIG_SINC_DEC_RATE_MSK,
245*6b31ba18SAntoniu Miclaus 				  FIELD_PREP(AD4080_FILTER_CONFIG_SINC_DEC_RATE_MSK,
246*6b31ba18SAntoniu Miclaus 					     (ilog2(mode) - 1)));
247*6b31ba18SAntoniu Miclaus }
248*6b31ba18SAntoniu Miclaus 
ad4080_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)249*6b31ba18SAntoniu Miclaus static int ad4080_read_raw(struct iio_dev *indio_dev,
250*6b31ba18SAntoniu Miclaus 			   struct iio_chan_spec const *chan,
251*6b31ba18SAntoniu Miclaus 			   int *val, int *val2, long m)
252*6b31ba18SAntoniu Miclaus {
253*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(indio_dev);
254*6b31ba18SAntoniu Miclaus 	int dec_rate;
255*6b31ba18SAntoniu Miclaus 
256*6b31ba18SAntoniu Miclaus 	switch (m) {
257*6b31ba18SAntoniu Miclaus 	case IIO_CHAN_INFO_SCALE:
258*6b31ba18SAntoniu Miclaus 		return ad4080_get_scale(st, val, val2);
259*6b31ba18SAntoniu Miclaus 	case IIO_CHAN_INFO_SAMP_FREQ:
260*6b31ba18SAntoniu Miclaus 		dec_rate = ad4080_get_dec_rate(indio_dev, chan);
261*6b31ba18SAntoniu Miclaus 		if (dec_rate < 0)
262*6b31ba18SAntoniu Miclaus 			return dec_rate;
263*6b31ba18SAntoniu Miclaus 		if (st->filter_type == SINC_5_COMP)
264*6b31ba18SAntoniu Miclaus 			dec_rate *= 2;
265*6b31ba18SAntoniu Miclaus 		if (st->filter_type)
266*6b31ba18SAntoniu Miclaus 			*val = DIV_ROUND_CLOSEST(st->clk_rate, dec_rate);
267*6b31ba18SAntoniu Miclaus 		else
268*6b31ba18SAntoniu Miclaus 			*val = st->clk_rate;
269*6b31ba18SAntoniu Miclaus 		return IIO_VAL_INT;
270*6b31ba18SAntoniu Miclaus 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
271*6b31ba18SAntoniu Miclaus 		if (st->filter_type == FILTER_NONE) {
272*6b31ba18SAntoniu Miclaus 			*val = 1;
273*6b31ba18SAntoniu Miclaus 		} else {
274*6b31ba18SAntoniu Miclaus 			*val = ad4080_get_dec_rate(indio_dev, chan);
275*6b31ba18SAntoniu Miclaus 			if (*val < 0)
276*6b31ba18SAntoniu Miclaus 				return *val;
277*6b31ba18SAntoniu Miclaus 		}
278*6b31ba18SAntoniu Miclaus 		return IIO_VAL_INT;
279*6b31ba18SAntoniu Miclaus 	default:
280*6b31ba18SAntoniu Miclaus 		return -EINVAL;
281*6b31ba18SAntoniu Miclaus 	}
282*6b31ba18SAntoniu Miclaus }
283*6b31ba18SAntoniu Miclaus 
ad4080_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)284*6b31ba18SAntoniu Miclaus static int ad4080_write_raw(struct iio_dev *indio_dev,
285*6b31ba18SAntoniu Miclaus 			    struct iio_chan_spec const *chan,
286*6b31ba18SAntoniu Miclaus 			    int val, int val2, long mask)
287*6b31ba18SAntoniu Miclaus {
288*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(indio_dev);
289*6b31ba18SAntoniu Miclaus 
290*6b31ba18SAntoniu Miclaus 	switch (mask) {
291*6b31ba18SAntoniu Miclaus 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
292*6b31ba18SAntoniu Miclaus 		if (st->filter_type == FILTER_NONE && val > 1)
293*6b31ba18SAntoniu Miclaus 			return -EINVAL;
294*6b31ba18SAntoniu Miclaus 
295*6b31ba18SAntoniu Miclaus 		return ad4080_set_dec_rate(indio_dev, chan, val);
296*6b31ba18SAntoniu Miclaus 	default:
297*6b31ba18SAntoniu Miclaus 		return -EINVAL;
298*6b31ba18SAntoniu Miclaus 	}
299*6b31ba18SAntoniu Miclaus }
300*6b31ba18SAntoniu Miclaus 
ad4080_lvds_sync_write(struct ad4080_state * st)301*6b31ba18SAntoniu Miclaus static int ad4080_lvds_sync_write(struct ad4080_state *st)
302*6b31ba18SAntoniu Miclaus {
303*6b31ba18SAntoniu Miclaus 	struct device *dev = regmap_get_device(st->regmap);
304*6b31ba18SAntoniu Miclaus 	int ret;
305*6b31ba18SAntoniu Miclaus 
306*6b31ba18SAntoniu Miclaus 	ret = regmap_set_bits(st->regmap, AD4080_REG_ADC_DATA_INTF_CONFIG_A,
307*6b31ba18SAntoniu Miclaus 			      AD4080_ADC_DATA_INTF_CONFIG_A_INTF_CHK_EN);
308*6b31ba18SAntoniu Miclaus 	if (ret)
309*6b31ba18SAntoniu Miclaus 		return ret;
310*6b31ba18SAntoniu Miclaus 
311*6b31ba18SAntoniu Miclaus 	ret = iio_backend_interface_data_align(st->back, 10000);
312*6b31ba18SAntoniu Miclaus 	if (ret)
313*6b31ba18SAntoniu Miclaus 		return dev_err_probe(dev, ret,
314*6b31ba18SAntoniu Miclaus 				     "Data alignment process failed\n");
315*6b31ba18SAntoniu Miclaus 
316*6b31ba18SAntoniu Miclaus 	dev_dbg(dev, "Success: Pattern correct and Locked!\n");
317*6b31ba18SAntoniu Miclaus 	return regmap_clear_bits(st->regmap, AD4080_REG_ADC_DATA_INTF_CONFIG_A,
318*6b31ba18SAntoniu Miclaus 				 AD4080_ADC_DATA_INTF_CONFIG_A_INTF_CHK_EN);
319*6b31ba18SAntoniu Miclaus }
320*6b31ba18SAntoniu Miclaus 
ad4080_get_filter_type(struct iio_dev * dev,const struct iio_chan_spec * chan)321*6b31ba18SAntoniu Miclaus static int ad4080_get_filter_type(struct iio_dev *dev,
322*6b31ba18SAntoniu Miclaus 				  const struct iio_chan_spec *chan)
323*6b31ba18SAntoniu Miclaus {
324*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(dev);
325*6b31ba18SAntoniu Miclaus 	unsigned int data;
326*6b31ba18SAntoniu Miclaus 	int ret;
327*6b31ba18SAntoniu Miclaus 
328*6b31ba18SAntoniu Miclaus 	ret = regmap_read(st->regmap, AD4080_REG_FILTER_CONFIG, &data);
329*6b31ba18SAntoniu Miclaus 	if (ret)
330*6b31ba18SAntoniu Miclaus 		return ret;
331*6b31ba18SAntoniu Miclaus 
332*6b31ba18SAntoniu Miclaus 	return FIELD_GET(AD4080_FILTER_CONFIG_FILTER_SEL_MSK, data);
333*6b31ba18SAntoniu Miclaus }
334*6b31ba18SAntoniu Miclaus 
ad4080_set_filter_type(struct iio_dev * dev,const struct iio_chan_spec * chan,unsigned int mode)335*6b31ba18SAntoniu Miclaus static int ad4080_set_filter_type(struct iio_dev *dev,
336*6b31ba18SAntoniu Miclaus 				  const struct iio_chan_spec *chan,
337*6b31ba18SAntoniu Miclaus 				  unsigned int mode)
338*6b31ba18SAntoniu Miclaus {
339*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(dev);
340*6b31ba18SAntoniu Miclaus 	int dec_rate;
341*6b31ba18SAntoniu Miclaus 	int ret;
342*6b31ba18SAntoniu Miclaus 
343*6b31ba18SAntoniu Miclaus 	guard(mutex)(&st->lock);
344*6b31ba18SAntoniu Miclaus 
345*6b31ba18SAntoniu Miclaus 	dec_rate = ad4080_get_dec_rate(dev, chan);
346*6b31ba18SAntoniu Miclaus 	if (dec_rate < 0)
347*6b31ba18SAntoniu Miclaus 		return dec_rate;
348*6b31ba18SAntoniu Miclaus 
349*6b31ba18SAntoniu Miclaus 	if (mode >= SINC_5 && dec_rate >= 512)
350*6b31ba18SAntoniu Miclaus 		return -EINVAL;
351*6b31ba18SAntoniu Miclaus 
352*6b31ba18SAntoniu Miclaus 	ret = iio_backend_filter_type_set(st->back, mode);
353*6b31ba18SAntoniu Miclaus 	if (ret)
354*6b31ba18SAntoniu Miclaus 		return ret;
355*6b31ba18SAntoniu Miclaus 
356*6b31ba18SAntoniu Miclaus 	ret = regmap_update_bits(st->regmap, AD4080_REG_FILTER_CONFIG,
357*6b31ba18SAntoniu Miclaus 				 AD4080_FILTER_CONFIG_FILTER_SEL_MSK,
358*6b31ba18SAntoniu Miclaus 				 FIELD_PREP(AD4080_FILTER_CONFIG_FILTER_SEL_MSK,
359*6b31ba18SAntoniu Miclaus 					    mode));
360*6b31ba18SAntoniu Miclaus 	if (ret)
361*6b31ba18SAntoniu Miclaus 		return ret;
362*6b31ba18SAntoniu Miclaus 
363*6b31ba18SAntoniu Miclaus 	st->filter_type = mode;
364*6b31ba18SAntoniu Miclaus 
365*6b31ba18SAntoniu Miclaus 	return 0;
366*6b31ba18SAntoniu Miclaus }
367*6b31ba18SAntoniu Miclaus 
ad4080_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)368*6b31ba18SAntoniu Miclaus static int ad4080_read_avail(struct iio_dev *indio_dev,
369*6b31ba18SAntoniu Miclaus 			     struct iio_chan_spec const *chan,
370*6b31ba18SAntoniu Miclaus 			     const int **vals, int *type, int *length,
371*6b31ba18SAntoniu Miclaus 			     long mask)
372*6b31ba18SAntoniu Miclaus {
373*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(indio_dev);
374*6b31ba18SAntoniu Miclaus 
375*6b31ba18SAntoniu Miclaus 	switch (mask) {
376*6b31ba18SAntoniu Miclaus 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
377*6b31ba18SAntoniu Miclaus 		switch (st->filter_type) {
378*6b31ba18SAntoniu Miclaus 		case FILTER_NONE:
379*6b31ba18SAntoniu Miclaus 			*vals = ad4080_dec_rate_none;
380*6b31ba18SAntoniu Miclaus 			*length = ARRAY_SIZE(ad4080_dec_rate_none);
381*6b31ba18SAntoniu Miclaus 			break;
382*6b31ba18SAntoniu Miclaus 		default:
383*6b31ba18SAntoniu Miclaus 			*vals = ad4080_dec_rate_avail;
384*6b31ba18SAntoniu Miclaus 			*length = st->filter_type >= SINC_5 ?
385*6b31ba18SAntoniu Miclaus 				  (ARRAY_SIZE(ad4080_dec_rate_avail) - 2) :
386*6b31ba18SAntoniu Miclaus 				  ARRAY_SIZE(ad4080_dec_rate_avail);
387*6b31ba18SAntoniu Miclaus 			break;
388*6b31ba18SAntoniu Miclaus 		}
389*6b31ba18SAntoniu Miclaus 		*type = IIO_VAL_INT;
390*6b31ba18SAntoniu Miclaus 		return IIO_AVAIL_LIST;
391*6b31ba18SAntoniu Miclaus 	default:
392*6b31ba18SAntoniu Miclaus 		return -EINVAL;
393*6b31ba18SAntoniu Miclaus 	}
394*6b31ba18SAntoniu Miclaus }
395*6b31ba18SAntoniu Miclaus 
396*6b31ba18SAntoniu Miclaus static const struct iio_info ad4080_iio_info = {
397*6b31ba18SAntoniu Miclaus 	.debugfs_reg_access = ad4080_reg_access,
398*6b31ba18SAntoniu Miclaus 	.read_raw = ad4080_read_raw,
399*6b31ba18SAntoniu Miclaus 	.write_raw = ad4080_write_raw,
400*6b31ba18SAntoniu Miclaus 	.read_avail = ad4080_read_avail,
401*6b31ba18SAntoniu Miclaus };
402*6b31ba18SAntoniu Miclaus 
403*6b31ba18SAntoniu Miclaus static const struct iio_enum ad4080_filter_type_enum = {
404*6b31ba18SAntoniu Miclaus 	.items = ad4080_filter_type_iio_enum,
405*6b31ba18SAntoniu Miclaus 	.num_items = ARRAY_SIZE(ad4080_filter_type_iio_enum),
406*6b31ba18SAntoniu Miclaus 	.set = ad4080_set_filter_type,
407*6b31ba18SAntoniu Miclaus 	.get = ad4080_get_filter_type,
408*6b31ba18SAntoniu Miclaus };
409*6b31ba18SAntoniu Miclaus 
410*6b31ba18SAntoniu Miclaus static struct iio_chan_spec_ext_info ad4080_ext_info[] = {
411*6b31ba18SAntoniu Miclaus 	IIO_ENUM("filter_type", IIO_SHARED_BY_ALL, &ad4080_filter_type_enum),
412*6b31ba18SAntoniu Miclaus 	IIO_ENUM_AVAILABLE("filter_type", IIO_SHARED_BY_ALL,
413*6b31ba18SAntoniu Miclaus 			   &ad4080_filter_type_enum),
414*6b31ba18SAntoniu Miclaus 	{ }
415*6b31ba18SAntoniu Miclaus };
416*6b31ba18SAntoniu Miclaus 
417*6b31ba18SAntoniu Miclaus static const struct iio_chan_spec ad4080_channel = {
418*6b31ba18SAntoniu Miclaus 	.type = IIO_VOLTAGE,
419*6b31ba18SAntoniu Miclaus 	.indexed = 1,
420*6b31ba18SAntoniu Miclaus 	.channel = 0,
421*6b31ba18SAntoniu Miclaus 	.info_mask_separate = BIT(IIO_CHAN_INFO_SCALE),
422*6b31ba18SAntoniu Miclaus 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
423*6b31ba18SAntoniu Miclaus 			BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
424*6b31ba18SAntoniu Miclaus 	.info_mask_shared_by_all_available =
425*6b31ba18SAntoniu Miclaus 			BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
426*6b31ba18SAntoniu Miclaus 	.ext_info = ad4080_ext_info,
427*6b31ba18SAntoniu Miclaus 	.scan_index = 0,
428*6b31ba18SAntoniu Miclaus 	.scan_type = {
429*6b31ba18SAntoniu Miclaus 		.sign = 's',
430*6b31ba18SAntoniu Miclaus 		.realbits = 20,
431*6b31ba18SAntoniu Miclaus 		.storagebits = 32,
432*6b31ba18SAntoniu Miclaus 	},
433*6b31ba18SAntoniu Miclaus };
434*6b31ba18SAntoniu Miclaus 
435*6b31ba18SAntoniu Miclaus static const struct ad4080_chip_info ad4080_chip_info = {
436*6b31ba18SAntoniu Miclaus 	.name = "ad4080",
437*6b31ba18SAntoniu Miclaus 	.product_id = AD4080_CHIP_ID,
438*6b31ba18SAntoniu Miclaus 	.scale_table = ad4080_scale_table,
439*6b31ba18SAntoniu Miclaus 	.num_scales = ARRAY_SIZE(ad4080_scale_table),
440*6b31ba18SAntoniu Miclaus 	.num_channels = 1,
441*6b31ba18SAntoniu Miclaus 	.channels = &ad4080_channel,
442*6b31ba18SAntoniu Miclaus };
443*6b31ba18SAntoniu Miclaus 
ad4080_setup(struct iio_dev * indio_dev)444*6b31ba18SAntoniu Miclaus static int ad4080_setup(struct iio_dev *indio_dev)
445*6b31ba18SAntoniu Miclaus {
446*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st = iio_priv(indio_dev);
447*6b31ba18SAntoniu Miclaus 	struct device *dev = regmap_get_device(st->regmap);
448*6b31ba18SAntoniu Miclaus 	unsigned int id;
449*6b31ba18SAntoniu Miclaus 	int ret;
450*6b31ba18SAntoniu Miclaus 
451*6b31ba18SAntoniu Miclaus 	ret = regmap_write(st->regmap, AD4080_REG_INTERFACE_CONFIG_A,
452*6b31ba18SAntoniu Miclaus 			   AD4080_INTERFACE_CONFIG_A_SW_RESET);
453*6b31ba18SAntoniu Miclaus 	if (ret)
454*6b31ba18SAntoniu Miclaus 		return ret;
455*6b31ba18SAntoniu Miclaus 
456*6b31ba18SAntoniu Miclaus 	ret = regmap_write(st->regmap, AD4080_REG_INTERFACE_CONFIG_A,
457*6b31ba18SAntoniu Miclaus 			   AD4080_INTERFACE_CONFIG_A_SDO_ENABLE);
458*6b31ba18SAntoniu Miclaus 	if (ret)
459*6b31ba18SAntoniu Miclaus 		return ret;
460*6b31ba18SAntoniu Miclaus 
461*6b31ba18SAntoniu Miclaus 	ret = regmap_read(st->regmap, AD4080_REG_CHIP_TYPE, &id);
462*6b31ba18SAntoniu Miclaus 	if (ret)
463*6b31ba18SAntoniu Miclaus 		return ret;
464*6b31ba18SAntoniu Miclaus 
465*6b31ba18SAntoniu Miclaus 	if (id != AD4080_CHIP_ID)
466*6b31ba18SAntoniu Miclaus 		dev_info(dev, "Unrecognized CHIP_ID 0x%X\n", id);
467*6b31ba18SAntoniu Miclaus 
468*6b31ba18SAntoniu Miclaus 	ret = regmap_set_bits(st->regmap, AD4080_REG_GPIO_CONFIG_A,
469*6b31ba18SAntoniu Miclaus 			      AD4080_GPIO_CONFIG_A_GPO_1_EN);
470*6b31ba18SAntoniu Miclaus 	if (ret)
471*6b31ba18SAntoniu Miclaus 		return ret;
472*6b31ba18SAntoniu Miclaus 
473*6b31ba18SAntoniu Miclaus 	ret = regmap_write(st->regmap, AD4080_REG_GPIO_CONFIG_B,
474*6b31ba18SAntoniu Miclaus 			   FIELD_PREP(AD4080_GPIO_CONFIG_B_GPIO_1_SEL_MSK,
475*6b31ba18SAntoniu Miclaus 				      AD4080_GPIO_CONFIG_B_GPIO_FILTER_RES_RDY));
476*6b31ba18SAntoniu Miclaus 	if (ret)
477*6b31ba18SAntoniu Miclaus 		return ret;
478*6b31ba18SAntoniu Miclaus 
479*6b31ba18SAntoniu Miclaus 	ret = iio_backend_num_lanes_set(st->back, st->num_lanes);
480*6b31ba18SAntoniu Miclaus 	if (ret)
481*6b31ba18SAntoniu Miclaus 		return ret;
482*6b31ba18SAntoniu Miclaus 
483*6b31ba18SAntoniu Miclaus 	if (!st->lvds_cnv_en)
484*6b31ba18SAntoniu Miclaus 		return 0;
485*6b31ba18SAntoniu Miclaus 
486*6b31ba18SAntoniu Miclaus 	/* Set maximum LVDS Data Transfer Latency */
487*6b31ba18SAntoniu Miclaus 	ret = regmap_update_bits(st->regmap,
488*6b31ba18SAntoniu Miclaus 				 AD4080_REG_ADC_DATA_INTF_CONFIG_B,
489*6b31ba18SAntoniu Miclaus 				 AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_CLK_CNT_MSK,
490*6b31ba18SAntoniu Miclaus 				 FIELD_PREP(AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_CLK_CNT_MSK,
491*6b31ba18SAntoniu Miclaus 					    AD4080_LVDS_CNV_CLK_CNT_MAX));
492*6b31ba18SAntoniu Miclaus 	if (ret)
493*6b31ba18SAntoniu Miclaus 		return ret;
494*6b31ba18SAntoniu Miclaus 
495*6b31ba18SAntoniu Miclaus 	if (st->num_lanes > 1) {
496*6b31ba18SAntoniu Miclaus 		ret = regmap_set_bits(st->regmap, AD4080_REG_ADC_DATA_INTF_CONFIG_A,
497*6b31ba18SAntoniu Miclaus 				      AD4080_ADC_DATA_INTF_CONFIG_A_SPI_LVDS_LANES);
498*6b31ba18SAntoniu Miclaus 		if (ret)
499*6b31ba18SAntoniu Miclaus 			return ret;
500*6b31ba18SAntoniu Miclaus 	}
501*6b31ba18SAntoniu Miclaus 
502*6b31ba18SAntoniu Miclaus 	ret = regmap_set_bits(st->regmap,
503*6b31ba18SAntoniu Miclaus 			      AD4080_REG_ADC_DATA_INTF_CONFIG_B,
504*6b31ba18SAntoniu Miclaus 			      AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_EN);
505*6b31ba18SAntoniu Miclaus 	if (ret)
506*6b31ba18SAntoniu Miclaus 		return ret;
507*6b31ba18SAntoniu Miclaus 
508*6b31ba18SAntoniu Miclaus 	return ad4080_lvds_sync_write(st);
509*6b31ba18SAntoniu Miclaus }
510*6b31ba18SAntoniu Miclaus 
ad4080_properties_parse(struct ad4080_state * st)511*6b31ba18SAntoniu Miclaus static int ad4080_properties_parse(struct ad4080_state *st)
512*6b31ba18SAntoniu Miclaus {
513*6b31ba18SAntoniu Miclaus 	struct device *dev = regmap_get_device(st->regmap);
514*6b31ba18SAntoniu Miclaus 
515*6b31ba18SAntoniu Miclaus 	st->lvds_cnv_en = device_property_read_bool(dev, "adi,lvds-cnv-enable");
516*6b31ba18SAntoniu Miclaus 
517*6b31ba18SAntoniu Miclaus 	st->num_lanes = 1;
518*6b31ba18SAntoniu Miclaus 	device_property_read_u32(dev, "adi,num-lanes", &st->num_lanes);
519*6b31ba18SAntoniu Miclaus 	if (!st->num_lanes || st->num_lanes > 2)
520*6b31ba18SAntoniu Miclaus 		return dev_err_probe(dev, -EINVAL,
521*6b31ba18SAntoniu Miclaus 				     "Invalid 'adi,num-lanes' value: %u",
522*6b31ba18SAntoniu Miclaus 				     st->num_lanes);
523*6b31ba18SAntoniu Miclaus 
524*6b31ba18SAntoniu Miclaus 	return 0;
525*6b31ba18SAntoniu Miclaus }
526*6b31ba18SAntoniu Miclaus 
ad4080_probe(struct spi_device * spi)527*6b31ba18SAntoniu Miclaus static int ad4080_probe(struct spi_device *spi)
528*6b31ba18SAntoniu Miclaus {
529*6b31ba18SAntoniu Miclaus 	struct iio_dev *indio_dev;
530*6b31ba18SAntoniu Miclaus 	struct device *dev = &spi->dev;
531*6b31ba18SAntoniu Miclaus 	struct ad4080_state *st;
532*6b31ba18SAntoniu Miclaus 	struct clk *clk;
533*6b31ba18SAntoniu Miclaus 	int ret;
534*6b31ba18SAntoniu Miclaus 
535*6b31ba18SAntoniu Miclaus 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
536*6b31ba18SAntoniu Miclaus 	if (!indio_dev)
537*6b31ba18SAntoniu Miclaus 		return -ENOMEM;
538*6b31ba18SAntoniu Miclaus 
539*6b31ba18SAntoniu Miclaus 	st = iio_priv(indio_dev);
540*6b31ba18SAntoniu Miclaus 
541*6b31ba18SAntoniu Miclaus 	ret = devm_regulator_bulk_get_enable(dev,
542*6b31ba18SAntoniu Miclaus 					     ARRAY_SIZE(ad4080_power_supplies),
543*6b31ba18SAntoniu Miclaus 					     ad4080_power_supplies);
544*6b31ba18SAntoniu Miclaus 	if (ret)
545*6b31ba18SAntoniu Miclaus 		return dev_err_probe(dev, ret,
546*6b31ba18SAntoniu Miclaus 				     "failed to get and enable supplies\n");
547*6b31ba18SAntoniu Miclaus 
548*6b31ba18SAntoniu Miclaus 	st->regmap = devm_regmap_init_spi(spi, &ad4080_regmap_config);
549*6b31ba18SAntoniu Miclaus 	if (IS_ERR(st->regmap))
550*6b31ba18SAntoniu Miclaus 		return PTR_ERR(st->regmap);
551*6b31ba18SAntoniu Miclaus 
552*6b31ba18SAntoniu Miclaus 	st->info = spi_get_device_match_data(spi);
553*6b31ba18SAntoniu Miclaus 	if (!st->info)
554*6b31ba18SAntoniu Miclaus 		return -ENODEV;
555*6b31ba18SAntoniu Miclaus 
556*6b31ba18SAntoniu Miclaus 	ret = devm_mutex_init(dev, &st->lock);
557*6b31ba18SAntoniu Miclaus 	if (ret)
558*6b31ba18SAntoniu Miclaus 		return ret;
559*6b31ba18SAntoniu Miclaus 
560*6b31ba18SAntoniu Miclaus 	indio_dev->name = st->info->name;
561*6b31ba18SAntoniu Miclaus 	indio_dev->channels = st->info->channels;
562*6b31ba18SAntoniu Miclaus 	indio_dev->num_channels = st->info->num_channels;
563*6b31ba18SAntoniu Miclaus 	indio_dev->info = &ad4080_iio_info;
564*6b31ba18SAntoniu Miclaus 
565*6b31ba18SAntoniu Miclaus 	ret = ad4080_properties_parse(st);
566*6b31ba18SAntoniu Miclaus 	if (ret)
567*6b31ba18SAntoniu Miclaus 		return ret;
568*6b31ba18SAntoniu Miclaus 
569*6b31ba18SAntoniu Miclaus 	clk = devm_clk_get_enabled(&spi->dev, "cnv");
570*6b31ba18SAntoniu Miclaus 	if (IS_ERR(clk))
571*6b31ba18SAntoniu Miclaus 		return PTR_ERR(clk);
572*6b31ba18SAntoniu Miclaus 
573*6b31ba18SAntoniu Miclaus 	st->clk_rate = clk_get_rate(clk);
574*6b31ba18SAntoniu Miclaus 
575*6b31ba18SAntoniu Miclaus 	st->back = devm_iio_backend_get(dev, NULL);
576*6b31ba18SAntoniu Miclaus 	if (IS_ERR(st->back))
577*6b31ba18SAntoniu Miclaus 		return PTR_ERR(st->back);
578*6b31ba18SAntoniu Miclaus 
579*6b31ba18SAntoniu Miclaus 	ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
580*6b31ba18SAntoniu Miclaus 	if (ret)
581*6b31ba18SAntoniu Miclaus 		return ret;
582*6b31ba18SAntoniu Miclaus 
583*6b31ba18SAntoniu Miclaus 	ret = devm_iio_backend_enable(dev, st->back);
584*6b31ba18SAntoniu Miclaus 	if (ret)
585*6b31ba18SAntoniu Miclaus 		return ret;
586*6b31ba18SAntoniu Miclaus 
587*6b31ba18SAntoniu Miclaus 	ret = ad4080_setup(indio_dev);
588*6b31ba18SAntoniu Miclaus 	if (ret)
589*6b31ba18SAntoniu Miclaus 		return ret;
590*6b31ba18SAntoniu Miclaus 
591*6b31ba18SAntoniu Miclaus 	return devm_iio_device_register(&spi->dev, indio_dev);
592*6b31ba18SAntoniu Miclaus }
593*6b31ba18SAntoniu Miclaus 
594*6b31ba18SAntoniu Miclaus static const struct spi_device_id ad4080_id[] = {
595*6b31ba18SAntoniu Miclaus 	{ "ad4080", (kernel_ulong_t)&ad4080_chip_info },
596*6b31ba18SAntoniu Miclaus 	{ }
597*6b31ba18SAntoniu Miclaus };
598*6b31ba18SAntoniu Miclaus MODULE_DEVICE_TABLE(spi, ad4080_id);
599*6b31ba18SAntoniu Miclaus 
600*6b31ba18SAntoniu Miclaus static const struct of_device_id ad4080_of_match[] = {
601*6b31ba18SAntoniu Miclaus 	{ .compatible = "adi,ad4080", &ad4080_chip_info },
602*6b31ba18SAntoniu Miclaus 	{ }
603*6b31ba18SAntoniu Miclaus };
604*6b31ba18SAntoniu Miclaus MODULE_DEVICE_TABLE(of, ad4080_of_match);
605*6b31ba18SAntoniu Miclaus 
606*6b31ba18SAntoniu Miclaus static struct spi_driver ad4080_driver = {
607*6b31ba18SAntoniu Miclaus 	.driver = {
608*6b31ba18SAntoniu Miclaus 		.name = "ad4080",
609*6b31ba18SAntoniu Miclaus 		.of_match_table = ad4080_of_match,
610*6b31ba18SAntoniu Miclaus 	},
611*6b31ba18SAntoniu Miclaus 	.probe = ad4080_probe,
612*6b31ba18SAntoniu Miclaus 	.id_table = ad4080_id,
613*6b31ba18SAntoniu Miclaus };
614*6b31ba18SAntoniu Miclaus module_spi_driver(ad4080_driver);
615*6b31ba18SAntoniu Miclaus 
616*6b31ba18SAntoniu Miclaus MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com");
617*6b31ba18SAntoniu Miclaus MODULE_DESCRIPTION("Analog Devices AD4080");
618*6b31ba18SAntoniu Miclaus MODULE_LICENSE("GPL");
619*6b31ba18SAntoniu Miclaus MODULE_IMPORT_NS("IIO_BACKEND");
620