xref: /linux/drivers/iio/dac/ad9739a.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1e77603d5SNuno Sa // SPDX-License-Identifier: GPL-2.0-only
2e77603d5SNuno Sa /*
3e77603d5SNuno Sa  * Analog Devices AD9739a SPI DAC driver
4e77603d5SNuno Sa  *
5e77603d5SNuno Sa  * Copyright 2015-2024 Analog Devices Inc.
6e77603d5SNuno Sa  */
7e77603d5SNuno Sa #include <linux/bitfield.h>
8e77603d5SNuno Sa #include <linux/bits.h>
9e77603d5SNuno Sa #include <linux/clk.h>
10e77603d5SNuno Sa #include <linux/delay.h>
11e77603d5SNuno Sa #include <linux/device.h>
12e77603d5SNuno Sa #include <linux/err.h>
13e77603d5SNuno Sa #include <linux/errno.h>
14e77603d5SNuno Sa #include <linux/gpio/consumer.h>
15e77603d5SNuno Sa #include <linux/minmax.h>
16e77603d5SNuno Sa #include <linux/module.h>
17e77603d5SNuno Sa #include <linux/mod_devicetable.h>
18e77603d5SNuno Sa #include <linux/property.h>
19e77603d5SNuno Sa #include <linux/regmap.h>
20e77603d5SNuno Sa #include <linux/spi/spi.h>
21e77603d5SNuno Sa #include <linux/units.h>
22e77603d5SNuno Sa 
23e77603d5SNuno Sa #include <linux/iio/backend.h>
24e77603d5SNuno Sa #include <linux/iio/iio.h>
25e77603d5SNuno Sa #include <linux/iio/types.h>
26e77603d5SNuno Sa 
27e77603d5SNuno Sa #define AD9739A_REG_MODE		0
28e77603d5SNuno Sa #define   AD9739A_RESET_MASK		BIT(5)
29e77603d5SNuno Sa #define AD9739A_REG_FSC_1		0x06
30e77603d5SNuno Sa #define AD9739A_REG_FSC_2		0x07
31e77603d5SNuno Sa #define   AD9739A_FSC_MSB		GENMASK(1, 0)
32e77603d5SNuno Sa #define AD9739A_REG_DEC_CNT		0x8
33e77603d5SNuno Sa #define   AD9739A_NORMAL_MODE		0
34e77603d5SNuno Sa #define   AD9739A_MIXED_MODE		2
35e77603d5SNuno Sa #define   AD9739A_DAC_DEC		GENMASK(1, 0)
36e77603d5SNuno Sa #define AD9739A_REG_LVDS_REC_CNT1	0x10
37e77603d5SNuno Sa #define   AD9739A_RCVR_LOOP_EN_MASK	GENMASK(1, 0)
38e77603d5SNuno Sa #define AD9739A_REG_LVDS_REC_CNT4	0x13
39e77603d5SNuno Sa #define   AD9739A_FINE_DEL_SKW_MASK	GENMASK(3, 0)
40e77603d5SNuno Sa #define AD9739A_REG_LVDS_REC_STAT9	0x21
41e77603d5SNuno Sa #define   AD9739A_RCVR_TRACK_AND_LOCK	(BIT(3) | BIT(0))
42e77603d5SNuno Sa #define AD9739A_REG_CROSS_CNT1		0x22
43e77603d5SNuno Sa #define AD9739A_REG_CROSS_CNT2		0x23
44e77603d5SNuno Sa #define AD9739A_REG_PHS_DET		0x24
45e77603d5SNuno Sa #define AD9739A_REG_MU_DUTY		0x25
46e77603d5SNuno Sa #define AD9739A_REG_MU_CNT1		0x26
47e77603d5SNuno Sa #define   AD9739A_MU_EN_MASK		BIT(0)
48561e2e3eSNuno Sa #define   AD9739A_MU_GAIN_MASK		BIT(1)
49e77603d5SNuno Sa #define AD9739A_REG_MU_CNT2		0x27
50e77603d5SNuno Sa #define AD9739A_REG_MU_CNT3		0x28
51e77603d5SNuno Sa #define AD9739A_REG_MU_CNT4		0x29
52e77603d5SNuno Sa #define   AD9739A_MU_CNT4_DEFAULT	0xcb
53e77603d5SNuno Sa #define AD9739A_REG_MU_STAT1		0x2A
54e77603d5SNuno Sa #define   AD9739A_MU_LOCK_MASK		BIT(0)
55e77603d5SNuno Sa #define AD9739A_REG_ANA_CNT_1		0x32
56e77603d5SNuno Sa #define AD9739A_REG_ID			0x35
57e77603d5SNuno Sa 
58e77603d5SNuno Sa #define AD9739A_ID			0x24
59e77603d5SNuno Sa #define AD9739A_REG_IS_RESERVED(reg)	\
60e77603d5SNuno Sa 	((reg) == 0x5 || (reg) == 0x9 || (reg) == 0x0E || (reg) == 0x0D || \
61e77603d5SNuno Sa 	 (reg) == 0x2B || (reg) == 0x2C || (reg) == 0x34)
62e77603d5SNuno Sa 
63e77603d5SNuno Sa #define AD9739A_FSC_MIN		8580
64e77603d5SNuno Sa #define AD9739A_FSC_MAX		31700
65e77603d5SNuno Sa #define AD9739A_FSC_RANGE	(AD9739A_FSC_MAX - AD9739A_FSC_MIN + 1)
66e77603d5SNuno Sa 
67e77603d5SNuno Sa #define AD9739A_MIN_DAC_CLK	(1600 * MEGA)
68e77603d5SNuno Sa #define AD9739A_MAX_DAC_CLK	(2500 * MEGA)
69e77603d5SNuno Sa #define AD9739A_DAC_CLK_RANGE	(AD9739A_MAX_DAC_CLK - AD9739A_MIN_DAC_CLK + 1)
70e77603d5SNuno Sa /* as recommended by the datasheet */
71e77603d5SNuno Sa #define AD9739A_LOCK_N_TRIES	3
72e77603d5SNuno Sa 
73e77603d5SNuno Sa struct ad9739a_state {
74e77603d5SNuno Sa 	struct iio_backend *back;
75e77603d5SNuno Sa 	struct regmap *regmap;
76e77603d5SNuno Sa 	unsigned long sample_rate;
77e77603d5SNuno Sa };
78e77603d5SNuno Sa 
ad9739a_oper_mode_get(struct iio_dev * indio_dev,const struct iio_chan_spec * chan)79e77603d5SNuno Sa static int ad9739a_oper_mode_get(struct iio_dev *indio_dev,
80e77603d5SNuno Sa 				 const struct iio_chan_spec *chan)
81e77603d5SNuno Sa {
82e77603d5SNuno Sa 	struct ad9739a_state *st = iio_priv(indio_dev);
83e77603d5SNuno Sa 	u32 mode;
84e77603d5SNuno Sa 	int ret;
85e77603d5SNuno Sa 
86e77603d5SNuno Sa 	ret = regmap_read(st->regmap, AD9739A_REG_DEC_CNT, &mode);
87e77603d5SNuno Sa 	if (ret)
88e77603d5SNuno Sa 		return ret;
89e77603d5SNuno Sa 
90e77603d5SNuno Sa 	mode = FIELD_GET(AD9739A_DAC_DEC, mode);
91e77603d5SNuno Sa 	/* sanity check we get valid values from the HW */
92e77603d5SNuno Sa 	if (mode != AD9739A_NORMAL_MODE && mode != AD9739A_MIXED_MODE)
93e77603d5SNuno Sa 		return -EIO;
94e77603d5SNuno Sa 	if (!mode)
95e77603d5SNuno Sa 		return AD9739A_NORMAL_MODE;
96e77603d5SNuno Sa 
97e77603d5SNuno Sa 	/*
98e77603d5SNuno Sa 	 * We get 2 from the device but for IIO modes, that means 1. Hence the
99e77603d5SNuno Sa 	 * minus 1.
100e77603d5SNuno Sa 	 */
101e77603d5SNuno Sa 	return AD9739A_MIXED_MODE - 1;
102e77603d5SNuno Sa }
103e77603d5SNuno Sa 
ad9739a_oper_mode_set(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,u32 mode)104e77603d5SNuno Sa static int ad9739a_oper_mode_set(struct iio_dev *indio_dev,
105e77603d5SNuno Sa 				 const struct iio_chan_spec *chan, u32 mode)
106e77603d5SNuno Sa {
107e77603d5SNuno Sa 	struct ad9739a_state *st = iio_priv(indio_dev);
108e77603d5SNuno Sa 
109e77603d5SNuno Sa 	/*
110e77603d5SNuno Sa 	 * On the IIO interface we have 0 and 1 for mode. But for mixed_mode, we
111e77603d5SNuno Sa 	 * need to write 2 in the device. That's what the below check is about.
112e77603d5SNuno Sa 	 */
113e77603d5SNuno Sa 	if (mode == AD9739A_MIXED_MODE - 1)
114e77603d5SNuno Sa 		mode = AD9739A_MIXED_MODE;
115e77603d5SNuno Sa 
116e77603d5SNuno Sa 	return regmap_update_bits(st->regmap, AD9739A_REG_DEC_CNT,
117e77603d5SNuno Sa 				  AD9739A_DAC_DEC, mode);
118e77603d5SNuno Sa }
119e77603d5SNuno Sa 
ad9739a_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)120e77603d5SNuno Sa static int ad9739a_read_raw(struct iio_dev *indio_dev,
121e77603d5SNuno Sa 			    struct iio_chan_spec const *chan,
122e77603d5SNuno Sa 			    int *val, int *val2, long mask)
123e77603d5SNuno Sa {
124e77603d5SNuno Sa 	struct ad9739a_state *st = iio_priv(indio_dev);
125e77603d5SNuno Sa 
126e77603d5SNuno Sa 	switch (mask) {
127e77603d5SNuno Sa 	case IIO_CHAN_INFO_SAMP_FREQ:
128e77603d5SNuno Sa 		*val = st->sample_rate;
129e77603d5SNuno Sa 		*val2 = 0;
130e77603d5SNuno Sa 		return IIO_VAL_INT_64;
131e77603d5SNuno Sa 	default:
132e77603d5SNuno Sa 		return -EINVAL;
133e77603d5SNuno Sa 	}
134e77603d5SNuno Sa }
135e77603d5SNuno Sa 
ad9739a_buffer_preenable(struct iio_dev * indio_dev)136e77603d5SNuno Sa static int ad9739a_buffer_preenable(struct iio_dev *indio_dev)
137e77603d5SNuno Sa {
138e77603d5SNuno Sa 	struct ad9739a_state *st = iio_priv(indio_dev);
139e77603d5SNuno Sa 
140e77603d5SNuno Sa 	return iio_backend_data_source_set(st->back, 0, IIO_BACKEND_EXTERNAL);
141e77603d5SNuno Sa }
142e77603d5SNuno Sa 
ad9739a_buffer_postdisable(struct iio_dev * indio_dev)143e77603d5SNuno Sa static int ad9739a_buffer_postdisable(struct iio_dev *indio_dev)
144e77603d5SNuno Sa {
145e77603d5SNuno Sa 	struct ad9739a_state *st = iio_priv(indio_dev);
146e77603d5SNuno Sa 
147e77603d5SNuno Sa 	return iio_backend_data_source_set(st->back, 0,
1482477d7b1SDavid Lechner 					   IIO_BACKEND_INTERNAL_CONTINUOUS_WAVE);
149e77603d5SNuno Sa }
150e77603d5SNuno Sa 
ad9739a_reg_accessible(struct device * dev,unsigned int reg)151e77603d5SNuno Sa static bool ad9739a_reg_accessible(struct device *dev, unsigned int reg)
152e77603d5SNuno Sa {
153e77603d5SNuno Sa 	if (AD9739A_REG_IS_RESERVED(reg))
154e77603d5SNuno Sa 		return false;
155e77603d5SNuno Sa 	if (reg > AD9739A_REG_MU_STAT1 && reg < AD9739A_REG_ANA_CNT_1)
156e77603d5SNuno Sa 		return false;
157e77603d5SNuno Sa 
158e77603d5SNuno Sa 	return true;
159e77603d5SNuno Sa }
160e77603d5SNuno Sa 
ad9739a_reset(struct device * dev,const struct ad9739a_state * st)161e77603d5SNuno Sa static int ad9739a_reset(struct device *dev, const struct ad9739a_state *st)
162e77603d5SNuno Sa {
163e77603d5SNuno Sa 	struct gpio_desc *gpio;
164e77603d5SNuno Sa 	int ret;
165e77603d5SNuno Sa 
166e77603d5SNuno Sa 	gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
167e77603d5SNuno Sa 	if (IS_ERR(gpio))
168e77603d5SNuno Sa 		return PTR_ERR(gpio);
169e77603d5SNuno Sa 	if (gpio) {
170e77603d5SNuno Sa 		/* minimum pulse width of 40ns */
171e77603d5SNuno Sa 		ndelay(40);
172e77603d5SNuno Sa 		gpiod_set_value_cansleep(gpio, 0);
173e77603d5SNuno Sa 		return 0;
174e77603d5SNuno Sa 	}
175e77603d5SNuno Sa 
176e77603d5SNuno Sa 	/* bring all registers to their default state */
177e77603d5SNuno Sa 	ret = regmap_set_bits(st->regmap, AD9739A_REG_MODE, AD9739A_RESET_MASK);
178e77603d5SNuno Sa 	if (ret)
179e77603d5SNuno Sa 		return ret;
180e77603d5SNuno Sa 
181e77603d5SNuno Sa 	ndelay(40);
182e77603d5SNuno Sa 
183e77603d5SNuno Sa 	return regmap_clear_bits(st->regmap, AD9739A_REG_MODE,
184e77603d5SNuno Sa 				 AD9739A_RESET_MASK);
185e77603d5SNuno Sa }
186e77603d5SNuno Sa 
187e77603d5SNuno Sa /*
188e77603d5SNuno Sa  * Recommended values (as per datasheet) for the dac clk common mode voltage
189e77603d5SNuno Sa  * and Mu controller. Look at table 29.
190e77603d5SNuno Sa  */
191e77603d5SNuno Sa static const struct reg_sequence ad9739a_clk_mu_ctrl[] = {
192e77603d5SNuno Sa 	/* DAC clk common mode voltage */
193e77603d5SNuno Sa 	{ AD9739A_REG_CROSS_CNT1, 0x0f },
194e77603d5SNuno Sa 	{ AD9739A_REG_CROSS_CNT2, 0x0f },
195e77603d5SNuno Sa 	/* Mu controller configuration */
196e77603d5SNuno Sa 	{ AD9739A_REG_PHS_DET, 0x30 },
197e77603d5SNuno Sa 	{ AD9739A_REG_MU_DUTY, 0x80 },
198e77603d5SNuno Sa 	{ AD9739A_REG_MU_CNT2, 0x44 },
199e77603d5SNuno Sa 	{ AD9739A_REG_MU_CNT3, 0x6c },
200e77603d5SNuno Sa };
201e77603d5SNuno Sa 
ad9739a_init(struct device * dev,const struct ad9739a_state * st)202e77603d5SNuno Sa static int ad9739a_init(struct device *dev, const struct ad9739a_state *st)
203e77603d5SNuno Sa {
204e77603d5SNuno Sa 	unsigned int i = 0, lock, fsc;
205e77603d5SNuno Sa 	u32 fsc_raw;
206e77603d5SNuno Sa 	int ret;
207e77603d5SNuno Sa 
208e77603d5SNuno Sa 	ret = regmap_multi_reg_write(st->regmap, ad9739a_clk_mu_ctrl,
209e77603d5SNuno Sa 				     ARRAY_SIZE(ad9739a_clk_mu_ctrl));
210e77603d5SNuno Sa 	if (ret)
211e77603d5SNuno Sa 		return ret;
212e77603d5SNuno Sa 
213e77603d5SNuno Sa 	/*
214e77603d5SNuno Sa 	 * Try to get the Mu lock. Repeat the below steps AD9739A_LOCK_N_TRIES
215e77603d5SNuno Sa 	 * (as specified by the datasheet) until we get the lock.
216e77603d5SNuno Sa 	 */
217e77603d5SNuno Sa 	do {
218e77603d5SNuno Sa 		ret = regmap_write(st->regmap, AD9739A_REG_MU_CNT4,
219e77603d5SNuno Sa 				   AD9739A_MU_CNT4_DEFAULT);
220e77603d5SNuno Sa 		if (ret)
221e77603d5SNuno Sa 			return ret;
222e77603d5SNuno Sa 
223e77603d5SNuno Sa 		/* Enable the Mu controller search and track mode. */
224561e2e3eSNuno Sa 		ret = regmap_write(st->regmap, AD9739A_REG_MU_CNT1,
225561e2e3eSNuno Sa 				   AD9739A_MU_EN_MASK | AD9739A_MU_GAIN_MASK);
226e77603d5SNuno Sa 		if (ret)
227e77603d5SNuno Sa 			return ret;
228e77603d5SNuno Sa 
229e77603d5SNuno Sa 		/* Ensure the DLL loop is locked */
230e77603d5SNuno Sa 		ret = regmap_read_poll_timeout(st->regmap, AD9739A_REG_MU_STAT1,
231e77603d5SNuno Sa 					       lock, lock & AD9739A_MU_LOCK_MASK,
232e77603d5SNuno Sa 					       0, 1000);
233e77603d5SNuno Sa 		if (ret && ret != -ETIMEDOUT)
234e77603d5SNuno Sa 			return ret;
235e77603d5SNuno Sa 	} while (ret && ++i < AD9739A_LOCK_N_TRIES);
236e77603d5SNuno Sa 
237e77603d5SNuno Sa 	if (i == AD9739A_LOCK_N_TRIES)
238e77603d5SNuno Sa 		return dev_err_probe(dev, ret, "Mu lock timeout\n");
239e77603d5SNuno Sa 
240e77603d5SNuno Sa 	/* Receiver tracking and lock. Same deal as the Mu controller */
241e77603d5SNuno Sa 	i = 0;
242e77603d5SNuno Sa 	do {
243e77603d5SNuno Sa 		ret = regmap_update_bits(st->regmap, AD9739A_REG_LVDS_REC_CNT4,
244e77603d5SNuno Sa 					 AD9739A_FINE_DEL_SKW_MASK,
245e77603d5SNuno Sa 					 FIELD_PREP(AD9739A_FINE_DEL_SKW_MASK, 2));
246e77603d5SNuno Sa 		if (ret)
247e77603d5SNuno Sa 			return ret;
248e77603d5SNuno Sa 
249e77603d5SNuno Sa 		/* Disable the receiver and the loop. */
250e77603d5SNuno Sa 		ret = regmap_write(st->regmap, AD9739A_REG_LVDS_REC_CNT1, 0);
251e77603d5SNuno Sa 		if (ret)
252e77603d5SNuno Sa 			return ret;
253e77603d5SNuno Sa 
254e77603d5SNuno Sa 		/*
255e77603d5SNuno Sa 		 * Re-enable the loop so it falls out of lock and begins the
256e77603d5SNuno Sa 		 * search/track routine again.
257e77603d5SNuno Sa 		 */
258e77603d5SNuno Sa 		ret = regmap_set_bits(st->regmap, AD9739A_REG_LVDS_REC_CNT1,
259e77603d5SNuno Sa 				      AD9739A_RCVR_LOOP_EN_MASK);
260e77603d5SNuno Sa 		if (ret)
261e77603d5SNuno Sa 			return ret;
262e77603d5SNuno Sa 
263e77603d5SNuno Sa 		/* Ensure the DLL loop is locked */
264e77603d5SNuno Sa 		ret = regmap_read_poll_timeout(st->regmap,
265e77603d5SNuno Sa 					       AD9739A_REG_LVDS_REC_STAT9, lock,
266e77603d5SNuno Sa 					       lock == AD9739A_RCVR_TRACK_AND_LOCK,
267e77603d5SNuno Sa 					       0, 1000);
268e77603d5SNuno Sa 		if (ret && ret != -ETIMEDOUT)
269e77603d5SNuno Sa 			return ret;
270e77603d5SNuno Sa 	} while (ret && ++i < AD9739A_LOCK_N_TRIES);
271e77603d5SNuno Sa 
272e77603d5SNuno Sa 	if (i == AD9739A_LOCK_N_TRIES)
273e77603d5SNuno Sa 		return dev_err_probe(dev, ret, "Receiver lock timeout\n");
274e77603d5SNuno Sa 
275e77603d5SNuno Sa 	ret = device_property_read_u32(dev, "adi,full-scale-microamp", &fsc);
276e77603d5SNuno Sa 	if (ret && ret == -EINVAL)
277e77603d5SNuno Sa 		return 0;
278e77603d5SNuno Sa 	if (ret)
279e77603d5SNuno Sa 		return ret;
280e77603d5SNuno Sa 	if (!in_range(fsc, AD9739A_FSC_MIN, AD9739A_FSC_RANGE))
281e77603d5SNuno Sa 		return dev_err_probe(dev, -EINVAL,
282e77603d5SNuno Sa 				     "Invalid full scale current(%u) [%u %u]\n",
283e77603d5SNuno Sa 				     fsc, AD9739A_FSC_MIN, AD9739A_FSC_MAX);
284e77603d5SNuno Sa 	/*
285e77603d5SNuno Sa 	 * IOUTFS is given by
286e77603d5SNuno Sa 	 *	Ioutfs = 0.0226 * FSC + 8.58
287e77603d5SNuno Sa 	 * and is given in mA. Hence we'll have to multiply by 10 * MILLI in
288e77603d5SNuno Sa 	 * order to get rid of the fractional.
289e77603d5SNuno Sa 	 */
290e77603d5SNuno Sa 	fsc_raw = DIV_ROUND_CLOSEST(fsc * 10 - 85800, 226);
291e77603d5SNuno Sa 
292e77603d5SNuno Sa 	ret = regmap_write(st->regmap, AD9739A_REG_FSC_1, fsc_raw & 0xff);
293e77603d5SNuno Sa 	if (ret)
294e77603d5SNuno Sa 		return ret;
295e77603d5SNuno Sa 
296e77603d5SNuno Sa 	return regmap_update_bits(st->regmap, AD9739A_REG_FSC_2,
297e77603d5SNuno Sa 				  AD9739A_FSC_MSB, fsc_raw >> 8);
298e77603d5SNuno Sa }
299e77603d5SNuno Sa 
300e77603d5SNuno Sa static const char * const ad9739a_modes_avail[] = { "normal", "mixed-mode" };
301e77603d5SNuno Sa 
302e77603d5SNuno Sa static const struct iio_enum ad9739a_modes = {
303e77603d5SNuno Sa 	.items = ad9739a_modes_avail,
304e77603d5SNuno Sa 	.num_items = ARRAY_SIZE(ad9739a_modes_avail),
305e77603d5SNuno Sa 	.get = ad9739a_oper_mode_get,
306e77603d5SNuno Sa 	.set = ad9739a_oper_mode_set,
307e77603d5SNuno Sa };
308e77603d5SNuno Sa 
309e77603d5SNuno Sa static const struct iio_chan_spec_ext_info ad9739a_ext_info[] = {
310e77603d5SNuno Sa 	IIO_ENUM_AVAILABLE("operating_mode", IIO_SEPARATE, &ad9739a_modes),
311e77603d5SNuno Sa 	IIO_ENUM("operating_mode", IIO_SEPARATE, &ad9739a_modes),
312e77603d5SNuno Sa 	{ }
313e77603d5SNuno Sa };
314e77603d5SNuno Sa 
315e77603d5SNuno Sa /*
316e77603d5SNuno Sa  * The reason for having two different channels is because we have, in reality,
317e77603d5SNuno Sa  * two sources of data:
318e77603d5SNuno Sa  *   ALTVOLTAGE: It's a Continuous Wave that's internally generated by the
319e77603d5SNuno Sa  *               backend device.
320e77603d5SNuno Sa  *   VOLTAGE: It's the typical data we can have in a DAC device and the source
321e77603d5SNuno Sa  *            of it has nothing to do with the backend. The backend will only
322e77603d5SNuno Sa  *            forward it into our data interface to be sent out.
323e77603d5SNuno Sa  */
324e77603d5SNuno Sa static struct iio_chan_spec ad9739a_channels[] = {
325e77603d5SNuno Sa 	{
326e77603d5SNuno Sa 		.type = IIO_ALTVOLTAGE,
327e77603d5SNuno Sa 		.indexed = 1,
328e77603d5SNuno Sa 		.output = 1,
329e77603d5SNuno Sa 		.scan_index = -1,
330e77603d5SNuno Sa 	},
331e77603d5SNuno Sa 	{
332e77603d5SNuno Sa 		.type = IIO_VOLTAGE,
333e77603d5SNuno Sa 		.indexed = 1,
334e77603d5SNuno Sa 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
335e77603d5SNuno Sa 		.output = 1,
336e77603d5SNuno Sa 		.ext_info = ad9739a_ext_info,
337e77603d5SNuno Sa 		.scan_type = {
338e77603d5SNuno Sa 			.sign = 's',
339e77603d5SNuno Sa 			.storagebits = 16,
340e77603d5SNuno Sa 			.realbits = 16,
341e77603d5SNuno Sa 		},
342e77603d5SNuno Sa 	}
343e77603d5SNuno Sa };
344e77603d5SNuno Sa 
345e77603d5SNuno Sa static const struct iio_info ad9739a_info = {
346e77603d5SNuno Sa 	.read_raw = ad9739a_read_raw,
347e77603d5SNuno Sa };
348e77603d5SNuno Sa 
349e77603d5SNuno Sa static const struct iio_buffer_setup_ops ad9739a_buffer_setup_ops = {
350e77603d5SNuno Sa 	.preenable = &ad9739a_buffer_preenable,
351e77603d5SNuno Sa 	.postdisable = &ad9739a_buffer_postdisable,
352e77603d5SNuno Sa };
353e77603d5SNuno Sa 
354e77603d5SNuno Sa static const struct regmap_config ad9739a_regmap_config = {
355e77603d5SNuno Sa 	.reg_bits = 8,
356e77603d5SNuno Sa 	.val_bits = 8,
357e77603d5SNuno Sa 	.readable_reg = ad9739a_reg_accessible,
358e77603d5SNuno Sa 	.writeable_reg = ad9739a_reg_accessible,
359e77603d5SNuno Sa 	.max_register = AD9739A_REG_ID,
360e77603d5SNuno Sa };
361e77603d5SNuno Sa 
ad9739a_probe(struct spi_device * spi)362e77603d5SNuno Sa static int ad9739a_probe(struct spi_device *spi)
363e77603d5SNuno Sa {
364e77603d5SNuno Sa 	struct device *dev = &spi->dev;
365e77603d5SNuno Sa 	struct iio_dev *indio_dev;
366e77603d5SNuno Sa 	struct ad9739a_state *st;
367e77603d5SNuno Sa 	unsigned int id;
368e77603d5SNuno Sa 	struct clk *clk;
369e77603d5SNuno Sa 	int ret;
370e77603d5SNuno Sa 
371e77603d5SNuno Sa 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
372e77603d5SNuno Sa 	if (!indio_dev)
373e77603d5SNuno Sa 		return -ENOMEM;
374e77603d5SNuno Sa 
375e77603d5SNuno Sa 	st = iio_priv(indio_dev);
376e77603d5SNuno Sa 
377e77603d5SNuno Sa 	clk = devm_clk_get_enabled(dev, NULL);
378e77603d5SNuno Sa 	if (IS_ERR(clk))
379e77603d5SNuno Sa 		return dev_err_probe(dev, PTR_ERR(clk), "Could not get clkin\n");
380e77603d5SNuno Sa 
381e77603d5SNuno Sa 	st->sample_rate = clk_get_rate(clk);
382e77603d5SNuno Sa 	if (!in_range(st->sample_rate, AD9739A_MIN_DAC_CLK,
383e77603d5SNuno Sa 		      AD9739A_DAC_CLK_RANGE))
384e77603d5SNuno Sa 		return dev_err_probe(dev, -EINVAL,
385e77603d5SNuno Sa 				     "Invalid dac clk range(%lu) [%lu %lu]\n",
386e77603d5SNuno Sa 				     st->sample_rate, AD9739A_MIN_DAC_CLK,
387e77603d5SNuno Sa 				     AD9739A_MAX_DAC_CLK);
388e77603d5SNuno Sa 
389e77603d5SNuno Sa 	st->regmap = devm_regmap_init_spi(spi, &ad9739a_regmap_config);
390e77603d5SNuno Sa 	if (IS_ERR(st->regmap))
391e77603d5SNuno Sa 		return PTR_ERR(st->regmap);
392e77603d5SNuno Sa 
393e77603d5SNuno Sa 	ret = regmap_read(st->regmap, AD9739A_REG_ID, &id);
394e77603d5SNuno Sa 	if (ret)
395e77603d5SNuno Sa 		return ret;
396e77603d5SNuno Sa 
397e77603d5SNuno Sa 	if (id != AD9739A_ID)
398e77603d5SNuno Sa 		dev_warn(dev, "Unrecognized CHIP_ID 0x%X", id);
399e77603d5SNuno Sa 
400e77603d5SNuno Sa 	ret = ad9739a_reset(dev, st);
401e77603d5SNuno Sa 	if (ret)
402e77603d5SNuno Sa 		return ret;
403e77603d5SNuno Sa 
404e77603d5SNuno Sa 	ret = ad9739a_init(dev, st);
405e77603d5SNuno Sa 	if (ret)
406e77603d5SNuno Sa 		return ret;
407e77603d5SNuno Sa 
408e77603d5SNuno Sa 	st->back = devm_iio_backend_get(dev, NULL);
409e77603d5SNuno Sa 	if (IS_ERR(st->back))
410e77603d5SNuno Sa 		return PTR_ERR(st->back);
411e77603d5SNuno Sa 
412e77603d5SNuno Sa 	ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
413e77603d5SNuno Sa 	if (ret)
414e77603d5SNuno Sa 		return ret;
415e77603d5SNuno Sa 
4168585632aSNuno Sa 	ret = iio_backend_extend_chan_spec(st->back, &ad9739a_channels[0]);
417e77603d5SNuno Sa 	if (ret)
418e77603d5SNuno Sa 		return ret;
419e77603d5SNuno Sa 
420e77603d5SNuno Sa 	ret = iio_backend_set_sampling_freq(st->back, 0, st->sample_rate);
421e77603d5SNuno Sa 	if (ret)
422e77603d5SNuno Sa 		return ret;
423e77603d5SNuno Sa 
424e77603d5SNuno Sa 	ret = devm_iio_backend_enable(dev, st->back);
425e77603d5SNuno Sa 	if (ret)
426e77603d5SNuno Sa 		return ret;
427e77603d5SNuno Sa 
428e77603d5SNuno Sa 	indio_dev->name = "ad9739a";
429e77603d5SNuno Sa 	indio_dev->info = &ad9739a_info;
430e77603d5SNuno Sa 	indio_dev->channels = ad9739a_channels;
431e77603d5SNuno Sa 	indio_dev->num_channels = ARRAY_SIZE(ad9739a_channels);
432e77603d5SNuno Sa 	indio_dev->setup_ops = &ad9739a_buffer_setup_ops;
433e77603d5SNuno Sa 
434*69eac4e1SNuno Sa 	ret = devm_iio_device_register(&spi->dev, indio_dev);
435*69eac4e1SNuno Sa 	if (ret)
436*69eac4e1SNuno Sa 		return ret;
437*69eac4e1SNuno Sa 
438*69eac4e1SNuno Sa 	iio_backend_debugfs_add(st->back, indio_dev);
439*69eac4e1SNuno Sa 
440*69eac4e1SNuno Sa 	return 0;
441e77603d5SNuno Sa }
442e77603d5SNuno Sa 
443e77603d5SNuno Sa static const struct of_device_id ad9739a_of_match[] = {
444e77603d5SNuno Sa 	{ .compatible = "adi,ad9739a" },
445e77603d5SNuno Sa 	{}
446e77603d5SNuno Sa };
447e77603d5SNuno Sa MODULE_DEVICE_TABLE(of, ad9739a_of_match);
448e77603d5SNuno Sa 
449e77603d5SNuno Sa static const struct spi_device_id ad9739a_id[] = {
450e77603d5SNuno Sa 	{"ad9739a"},
451e77603d5SNuno Sa 	{}
452e77603d5SNuno Sa };
453e77603d5SNuno Sa MODULE_DEVICE_TABLE(spi, ad9739a_id);
454e77603d5SNuno Sa 
455e77603d5SNuno Sa static struct spi_driver ad9739a_driver = {
456e77603d5SNuno Sa 	.driver = {
457e77603d5SNuno Sa 		.name = "ad9739a",
458e77603d5SNuno Sa 		.of_match_table = ad9739a_of_match,
459e77603d5SNuno Sa 	},
460e77603d5SNuno Sa 	.probe = ad9739a_probe,
461e77603d5SNuno Sa 	.id_table = ad9739a_id,
462e77603d5SNuno Sa };
463e77603d5SNuno Sa module_spi_driver(ad9739a_driver);
464e77603d5SNuno Sa 
465e77603d5SNuno Sa MODULE_AUTHOR("Dragos Bogdan <dragos.bogdan@analog.com>");
466e77603d5SNuno Sa MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
467e77603d5SNuno Sa MODULE_DESCRIPTION("Analog Devices AD9739 DAC");
468e77603d5SNuno Sa MODULE_LICENSE("GPL");
469e77603d5SNuno Sa MODULE_IMPORT_NS(IIO_BACKEND);
470