xref: /linux/drivers/iio/dac/ad5758.c (revision 889600e21e3be388a6817c2a0dac0411df860751)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AD5758 Digital to analog converters driver
4  *
5  * Copyright 2018 Analog Devices Inc.
6  *
7  * TODO: Currently CRC is not supported in this driver
8  */
9 #include <linux/bsearch.h>
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/property.h>
14 #include <linux/spi/spi.h>
15 #include <linux/gpio/consumer.h>
16 
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 
20 /* AD5758 registers definition */
21 #define AD5758_NOP				0x00
22 #define AD5758_DAC_INPUT			0x01
23 #define AD5758_DAC_OUTPUT			0x02
24 #define AD5758_CLEAR_CODE			0x03
25 #define AD5758_USER_GAIN			0x04
26 #define AD5758_USER_OFFSET			0x05
27 #define AD5758_DAC_CONFIG			0x06
28 #define AD5758_SW_LDAC				0x07
29 #define AD5758_KEY				0x08
30 #define AD5758_GP_CONFIG1			0x09
31 #define AD5758_GP_CONFIG2			0x0A
32 #define AD5758_DCDC_CONFIG1			0x0B
33 #define AD5758_DCDC_CONFIG2			0x0C
34 #define AD5758_WDT_CONFIG			0x0F
35 #define AD5758_DIGITAL_DIAG_CONFIG		0x10
36 #define AD5758_ADC_CONFIG			0x11
37 #define AD5758_FAULT_PIN_CONFIG			0x12
38 #define AD5758_TWO_STAGE_READBACK_SELECT	0x13
39 #define AD5758_DIGITAL_DIAG_RESULTS		0x14
40 #define AD5758_ANALOG_DIAG_RESULTS		0x15
41 #define AD5758_STATUS				0x16
42 #define AD5758_CHIP_ID				0x17
43 #define AD5758_FREQ_MONITOR			0x18
44 #define AD5758_DEVICE_ID_0			0x19
45 #define AD5758_DEVICE_ID_1			0x1A
46 #define AD5758_DEVICE_ID_2			0x1B
47 #define AD5758_DEVICE_ID_3			0x1C
48 
49 /* AD5758_DAC_CONFIG */
50 #define AD5758_DAC_CONFIG_RANGE_MSK		GENMASK(3, 0)
51 #define AD5758_DAC_CONFIG_RANGE_MODE(x)		(((x) & 0xF) << 0)
52 #define AD5758_DAC_CONFIG_INT_EN_MSK		BIT(5)
53 #define AD5758_DAC_CONFIG_INT_EN_MODE(x)	(((x) & 0x1) << 5)
54 #define AD5758_DAC_CONFIG_OUT_EN_MSK		BIT(6)
55 #define AD5758_DAC_CONFIG_OUT_EN_MODE(x)	(((x) & 0x1) << 6)
56 #define AD5758_DAC_CONFIG_SR_EN_MSK		BIT(8)
57 #define AD5758_DAC_CONFIG_SR_EN_MODE(x)		(((x) & 0x1) << 8)
58 #define AD5758_DAC_CONFIG_SR_CLOCK_MSK		GENMASK(12, 9)
59 #define AD5758_DAC_CONFIG_SR_CLOCK_MODE(x)	(((x) & 0xF) << 9)
60 #define AD5758_DAC_CONFIG_SR_STEP_MSK		GENMASK(15, 13)
61 #define AD5758_DAC_CONFIG_SR_STEP_MODE(x)	(((x) & 0x7) << 13)
62 
63 /* AD5758_KEY */
64 #define AD5758_KEY_CODE_RESET_1			0x15FA
65 #define AD5758_KEY_CODE_RESET_2			0xAF51
66 #define AD5758_KEY_CODE_SINGLE_ADC_CONV		0x1ADC
67 #define AD5758_KEY_CODE_RESET_WDT		0x0D06
68 #define AD5758_KEY_CODE_CALIB_MEM_REFRESH	0xFCBA
69 
70 /* AD5758_DCDC_CONFIG1 */
71 #define AD5758_DCDC_CONFIG1_DCDC_VPROG_MSK	GENMASK(4, 0)
72 #define AD5758_DCDC_CONFIG1_DCDC_VPROG_MODE(x)	(((x) & 0x1F) << 0)
73 #define AD5758_DCDC_CONFIG1_DCDC_MODE_MSK	GENMASK(6, 5)
74 #define AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(x)	(((x) & 0x3) << 5)
75 
76 /* AD5758_DCDC_CONFIG2 */
77 #define AD5758_DCDC_CONFIG2_ILIMIT_MSK		GENMASK(3, 1)
78 #define AD5758_DCDC_CONFIG2_ILIMIT_MODE(x)	(((x) & 0x7) << 1)
79 #define AD5758_DCDC_CONFIG2_INTR_SAT_3WI_MSK	BIT(11)
80 #define AD5758_DCDC_CONFIG2_BUSY_3WI_MSK	BIT(12)
81 
82 /* AD5758_DIGITAL_DIAG_RESULTS */
83 #define AD5758_CAL_MEM_UNREFRESHED_MSK		BIT(15)
84 
85 /* AD5758_ADC_CONFIG */
86 #define AD5758_ADC_CONFIG_PPC_BUF_EN(x)		(((x) & 0x1) << 11)
87 #define AD5758_ADC_CONFIG_PPC_BUF_MSK		BIT(11)
88 
89 #define AD5758_WR_FLAG_MSK(x)		(0x80 | ((x) & 0x1F))
90 
91 #define AD5758_FULL_SCALE_MICRO	65535000000ULL
92 
93 struct ad5758_range {
94 	int reg;
95 	int min;
96 	int max;
97 };
98 
99 /**
100  * struct ad5758_state - driver instance specific data
101  * @spi:	spi_device
102  * @lock:	mutex lock
103  * @gpio_reset:	gpio descriptor for the reset line
104  * @out_range:	struct which stores the output range
105  * @dc_dc_mode:	variable which stores the mode of operation
106  * @dc_dc_ilim:	variable which stores the dc-to-dc converter current limit
107  * @slew_time:	variable which stores the target slew time
108  * @pwr_down:	variable which contains whether a channel is powered down or not
109  * @d32:	spi transfer buffers
110  */
111 struct ad5758_state {
112 	struct spi_device *spi;
113 	struct mutex lock;
114 	struct gpio_desc *gpio_reset;
115 	struct ad5758_range out_range;
116 	unsigned int dc_dc_mode;
117 	unsigned int dc_dc_ilim;
118 	unsigned int slew_time;
119 	bool pwr_down;
120 	__be32 d32[3];
121 };
122 
123 /*
124  * Output ranges corresponding to bits [3:0] from DAC_CONFIG register
125  * 0000: 0 V to 5 V voltage range
126  * 0001: 0 V to 10 V voltage range
127  * 0010: ±5 V voltage range
128  * 0011: ±10 V voltage range
129  * 1000: 0 mA to 20 mA current range
130  * 1001: 0 mA to 24 mA current range
131  * 1010: 4 mA to 20 mA current range
132  * 1011: ±20 mA current range
133  * 1100: ±24 mA current range
134  * 1101: -1 mA to +22 mA current range
135  */
136 enum ad5758_output_range {
137 	AD5758_RANGE_0V_5V,
138 	AD5758_RANGE_0V_10V,
139 	AD5758_RANGE_PLUSMINUS_5V,
140 	AD5758_RANGE_PLUSMINUS_10V,
141 	AD5758_RANGE_0mA_20mA = 8,
142 	AD5758_RANGE_0mA_24mA,
143 	AD5758_RANGE_4mA_24mA,
144 	AD5758_RANGE_PLUSMINUS_20mA,
145 	AD5758_RANGE_PLUSMINUS_24mA,
146 	AD5758_RANGE_MINUS_1mA_PLUS_22mA,
147 };
148 
149 enum ad5758_dc_dc_mode {
150 	AD5758_DCDC_MODE_POWER_OFF,
151 	AD5758_DCDC_MODE_DPC_CURRENT,
152 	AD5758_DCDC_MODE_DPC_VOLTAGE,
153 	AD5758_DCDC_MODE_PPC_CURRENT,
154 };
155 
156 static const struct ad5758_range ad5758_voltage_range[] = {
157 	{ AD5758_RANGE_0V_5V, 0, 5000000 },
158 	{ AD5758_RANGE_0V_10V, 0, 10000000 },
159 	{ AD5758_RANGE_PLUSMINUS_5V, -5000000, 5000000 },
160 	{ AD5758_RANGE_PLUSMINUS_10V, -10000000, 10000000 }
161 };
162 
163 static const struct ad5758_range ad5758_current_range[] = {
164 	{ AD5758_RANGE_0mA_20mA, 0, 20000},
165 	{ AD5758_RANGE_0mA_24mA, 0, 24000 },
166 	{ AD5758_RANGE_4mA_24mA, 4, 24000 },
167 	{ AD5758_RANGE_PLUSMINUS_20mA, -20000, 20000 },
168 	{ AD5758_RANGE_PLUSMINUS_24mA, -24000, 24000 },
169 	{ AD5758_RANGE_MINUS_1mA_PLUS_22mA, -1000, 22000 },
170 };
171 
172 static const int ad5758_sr_clk[16] = {
173 	240000, 200000, 150000, 128000, 64000, 32000, 16000, 8000, 4000, 2000,
174 	1000, 512, 256, 128, 64, 16
175 };
176 
177 static const int ad5758_sr_step[8] = {
178 	4, 12, 64, 120, 256, 500, 1820, 2048
179 };
180 
181 static const int ad5758_dc_dc_ilim[6] = {
182 	150000, 200000, 250000, 300000, 350000, 400000
183 };
184 
ad5758_spi_reg_read(struct ad5758_state * st,unsigned int addr)185 static int ad5758_spi_reg_read(struct ad5758_state *st, unsigned int addr)
186 {
187 	struct spi_transfer t[] = {
188 		{
189 			.tx_buf = &st->d32[0],
190 			.len = 4,
191 			.cs_change = 1,
192 		}, {
193 			.tx_buf = &st->d32[1],
194 			.rx_buf = &st->d32[2],
195 			.len = 4,
196 		},
197 	};
198 	int ret;
199 
200 	st->d32[0] = cpu_to_be32(
201 		(AD5758_WR_FLAG_MSK(AD5758_TWO_STAGE_READBACK_SELECT) << 24) |
202 		(addr << 8));
203 	st->d32[1] = cpu_to_be32(AD5758_WR_FLAG_MSK(AD5758_NOP) << 24);
204 
205 	ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
206 	if (ret < 0)
207 		return ret;
208 
209 	return (be32_to_cpu(st->d32[2]) >> 8) & 0xFFFF;
210 }
211 
ad5758_spi_reg_write(struct ad5758_state * st,unsigned int addr,unsigned int val)212 static int ad5758_spi_reg_write(struct ad5758_state *st,
213 				unsigned int addr,
214 				unsigned int val)
215 {
216 	st->d32[0] = cpu_to_be32((AD5758_WR_FLAG_MSK(addr) << 24) |
217 				 ((val & 0xFFFF) << 8));
218 
219 	return spi_write(st->spi, &st->d32[0], sizeof(st->d32[0]));
220 }
221 
ad5758_spi_write_mask(struct ad5758_state * st,unsigned int addr,unsigned long int mask,unsigned int val)222 static int ad5758_spi_write_mask(struct ad5758_state *st,
223 				 unsigned int addr,
224 				 unsigned long int mask,
225 				 unsigned int val)
226 {
227 	int regval;
228 
229 	regval = ad5758_spi_reg_read(st, addr);
230 	if (regval < 0)
231 		return regval;
232 
233 	regval &= ~mask;
234 	regval |= val;
235 
236 	return ad5758_spi_reg_write(st, addr, regval);
237 }
238 
cmpfunc(const void * a,const void * b)239 static int cmpfunc(const void *a, const void *b)
240 {
241 	return *(int *)a - *(int *)b;
242 }
243 
ad5758_find_closest_match(const int * array,unsigned int size,int val)244 static int ad5758_find_closest_match(const int *array,
245 				     unsigned int size, int val)
246 {
247 	int i;
248 
249 	for (i = 0; i < size; i++) {
250 		if (val <= array[i])
251 			return i;
252 	}
253 
254 	return size - 1;
255 }
256 
ad5758_wait_for_task_complete(struct ad5758_state * st,unsigned int reg,unsigned int mask)257 static int ad5758_wait_for_task_complete(struct ad5758_state *st,
258 					 unsigned int reg,
259 					 unsigned int mask)
260 {
261 	unsigned int timeout;
262 	int ret;
263 
264 	timeout = 10;
265 	do {
266 		ret = ad5758_spi_reg_read(st, reg);
267 		if (ret < 0)
268 			return ret;
269 
270 		if (!(ret & mask))
271 			return 0;
272 
273 		usleep_range(100, 1000);
274 	} while (--timeout);
275 
276 	dev_err(&st->spi->dev,
277 		"Error reading bit 0x%x in 0x%x register\n", mask, reg);
278 
279 	return -EIO;
280 }
281 
ad5758_calib_mem_refresh(struct ad5758_state * st)282 static int ad5758_calib_mem_refresh(struct ad5758_state *st)
283 {
284 	int ret;
285 
286 	ret = ad5758_spi_reg_write(st, AD5758_KEY,
287 				   AD5758_KEY_CODE_CALIB_MEM_REFRESH);
288 	if (ret < 0) {
289 		dev_err(&st->spi->dev,
290 			"Failed to initiate a calibration memory refresh\n");
291 		return ret;
292 	}
293 
294 	/* Wait to allow time for the internal calibrations to complete */
295 	return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
296 					     AD5758_CAL_MEM_UNREFRESHED_MSK);
297 }
298 
ad5758_soft_reset(struct ad5758_state * st)299 static int ad5758_soft_reset(struct ad5758_state *st)
300 {
301 	int ret;
302 
303 	ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1);
304 	if (ret < 0)
305 		return ret;
306 
307 	ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2);
308 
309 	/* Perform a software reset and wait at least 100us */
310 	usleep_range(100, 1000);
311 
312 	return ret;
313 }
314 
ad5758_set_dc_dc_conv_mode(struct ad5758_state * st,enum ad5758_dc_dc_mode mode)315 static int ad5758_set_dc_dc_conv_mode(struct ad5758_state *st,
316 				      enum ad5758_dc_dc_mode mode)
317 {
318 	int ret;
319 
320 	/*
321 	 * The ENABLE_PPC_BUFFERS bit must be set prior to enabling PPC current
322 	 * mode.
323 	 */
324 	if (mode == AD5758_DCDC_MODE_PPC_CURRENT) {
325 		ret  = ad5758_spi_write_mask(st, AD5758_ADC_CONFIG,
326 				    AD5758_ADC_CONFIG_PPC_BUF_MSK,
327 				    AD5758_ADC_CONFIG_PPC_BUF_EN(1));
328 		if (ret < 0)
329 			return ret;
330 	}
331 
332 	ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1,
333 				    AD5758_DCDC_CONFIG1_DCDC_MODE_MSK,
334 				    AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(mode));
335 	if (ret < 0)
336 		return ret;
337 
338 	/*
339 	 * Poll the BUSY_3WI bit in the DCDC_CONFIG2 register until it is 0.
340 	 * This allows the 3-wire interface communication to complete.
341 	 */
342 	ret = ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
343 					    AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
344 	if (ret < 0)
345 		return ret;
346 
347 	st->dc_dc_mode = mode;
348 
349 	return ret;
350 }
351 
ad5758_set_dc_dc_ilim(struct ad5758_state * st,unsigned int ilim)352 static int ad5758_set_dc_dc_ilim(struct ad5758_state *st, unsigned int ilim)
353 {
354 	int ret;
355 
356 	ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG2,
357 				    AD5758_DCDC_CONFIG2_ILIMIT_MSK,
358 				    AD5758_DCDC_CONFIG2_ILIMIT_MODE(ilim));
359 	if (ret < 0)
360 		return ret;
361 	/*
362 	 * Poll the BUSY_3WI bit in the DCDC_CONFIG2 register until it is 0.
363 	 * This allows the 3-wire interface communication to complete.
364 	 */
365 	return ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
366 					     AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
367 }
368 
ad5758_slew_rate_set(struct ad5758_state * st,unsigned int sr_clk_idx,unsigned int sr_step_idx)369 static int ad5758_slew_rate_set(struct ad5758_state *st,
370 				unsigned int sr_clk_idx,
371 				unsigned int sr_step_idx)
372 {
373 	unsigned int mode;
374 	unsigned long int mask;
375 	int ret;
376 
377 	mask = AD5758_DAC_CONFIG_SR_EN_MSK |
378 	       AD5758_DAC_CONFIG_SR_CLOCK_MSK |
379 	       AD5758_DAC_CONFIG_SR_STEP_MSK;
380 	mode = AD5758_DAC_CONFIG_SR_EN_MODE(1) |
381 	       AD5758_DAC_CONFIG_SR_STEP_MODE(sr_step_idx) |
382 	       AD5758_DAC_CONFIG_SR_CLOCK_MODE(sr_clk_idx);
383 
384 	ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, mask, mode);
385 	if (ret < 0)
386 		return ret;
387 
388 	/* Wait to allow time for the internal calibrations to complete */
389 	return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
390 					     AD5758_CAL_MEM_UNREFRESHED_MSK);
391 }
392 
ad5758_slew_rate_config(struct ad5758_state * st)393 static int ad5758_slew_rate_config(struct ad5758_state *st)
394 {
395 	unsigned int sr_clk_idx, sr_step_idx;
396 	int i, res;
397 	s64 diff_new, diff_old;
398 	u64 sr_step, calc_slew_time;
399 
400 	sr_clk_idx = 0;
401 	sr_step_idx = 0;
402 	diff_old = S64_MAX;
403 	/*
404 	 * The slew time can be determined by using the formula:
405 	 * Slew Time = (Full Scale Out / (Step Size x Update Clk Freq))
406 	 * where Slew time is expressed in microseconds
407 	 * Given the desired slew time, the following algorithm determines the
408 	 * best match for the step size and the update clock frequency.
409 	 */
410 	for (i = 0; i < ARRAY_SIZE(ad5758_sr_clk); i++) {
411 		/*
412 		 * Go through each valid update clock freq and determine a raw
413 		 * value for the step size by using the formula:
414 		 * Step Size = Full Scale Out / (Update Clk Freq * Slew Time)
415 		 */
416 		sr_step = AD5758_FULL_SCALE_MICRO;
417 		do_div(sr_step, ad5758_sr_clk[i]);
418 		do_div(sr_step, st->slew_time);
419 		/*
420 		 * After a raw value for step size was determined, find the
421 		 * closest valid match
422 		 */
423 		res = ad5758_find_closest_match(ad5758_sr_step,
424 						ARRAY_SIZE(ad5758_sr_step),
425 						sr_step);
426 		/* Calculate the slew time */
427 		calc_slew_time = AD5758_FULL_SCALE_MICRO;
428 		do_div(calc_slew_time, ad5758_sr_step[res]);
429 		do_div(calc_slew_time, ad5758_sr_clk[i]);
430 		/*
431 		 * Determine with how many microseconds the calculated slew time
432 		 * is different from the desired slew time and store the diff
433 		 * for the next iteration
434 		 */
435 		diff_new = abs(st->slew_time - calc_slew_time);
436 		if (diff_new < diff_old) {
437 			diff_old = diff_new;
438 			sr_clk_idx = i;
439 			sr_step_idx = res;
440 		}
441 	}
442 
443 	return ad5758_slew_rate_set(st, sr_clk_idx, sr_step_idx);
444 }
445 
ad5758_set_out_range(struct ad5758_state * st,int range)446 static int ad5758_set_out_range(struct ad5758_state *st, int range)
447 {
448 	int ret;
449 
450 	ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
451 				    AD5758_DAC_CONFIG_RANGE_MSK,
452 				    AD5758_DAC_CONFIG_RANGE_MODE(range));
453 	if (ret < 0)
454 		return ret;
455 
456 	/* Wait to allow time for the internal calibrations to complete */
457 	return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
458 					     AD5758_CAL_MEM_UNREFRESHED_MSK);
459 }
460 
ad5758_internal_buffers_en(struct ad5758_state * st,bool enable)461 static int ad5758_internal_buffers_en(struct ad5758_state *st, bool enable)
462 {
463 	int ret;
464 
465 	ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
466 				    AD5758_DAC_CONFIG_INT_EN_MSK,
467 				    AD5758_DAC_CONFIG_INT_EN_MODE(enable));
468 	if (ret < 0)
469 		return ret;
470 
471 	/* Wait to allow time for the internal calibrations to complete */
472 	return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
473 					     AD5758_CAL_MEM_UNREFRESHED_MSK);
474 }
475 
ad5758_reset(struct ad5758_state * st)476 static int ad5758_reset(struct ad5758_state *st)
477 {
478 	if (st->gpio_reset) {
479 		gpiod_set_value(st->gpio_reset, 0);
480 		usleep_range(100, 1000);
481 		gpiod_set_value(st->gpio_reset, 1);
482 		usleep_range(100, 1000);
483 
484 		return 0;
485 	} else {
486 		/* Perform a software reset */
487 		return ad5758_soft_reset(st);
488 	}
489 }
490 
ad5758_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)491 static int ad5758_reg_access(struct iio_dev *indio_dev,
492 			     unsigned int reg,
493 			     unsigned int writeval,
494 			     unsigned int *readval)
495 {
496 	struct ad5758_state *st = iio_priv(indio_dev);
497 	int ret;
498 
499 	mutex_lock(&st->lock);
500 	if (readval) {
501 		ret = ad5758_spi_reg_read(st, reg);
502 		if (ret < 0) {
503 			mutex_unlock(&st->lock);
504 			return ret;
505 		}
506 
507 		*readval = ret;
508 		ret = 0;
509 	} else {
510 		ret = ad5758_spi_reg_write(st, reg, writeval);
511 	}
512 	mutex_unlock(&st->lock);
513 
514 	return ret;
515 }
516 
ad5758_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long info)517 static int ad5758_read_raw(struct iio_dev *indio_dev,
518 			   struct iio_chan_spec const *chan,
519 			   int *val, int *val2, long info)
520 {
521 	struct ad5758_state *st = iio_priv(indio_dev);
522 	int max, min, ret;
523 
524 	switch (info) {
525 	case IIO_CHAN_INFO_RAW:
526 		mutex_lock(&st->lock);
527 		ret = ad5758_spi_reg_read(st, AD5758_DAC_INPUT);
528 		mutex_unlock(&st->lock);
529 		if (ret < 0)
530 			return ret;
531 
532 		*val = ret;
533 		return IIO_VAL_INT;
534 	case IIO_CHAN_INFO_SCALE:
535 		min = st->out_range.min;
536 		max = st->out_range.max;
537 		*val = (max - min) / 1000;
538 		*val2 = 16;
539 		return IIO_VAL_FRACTIONAL_LOG2;
540 	case IIO_CHAN_INFO_OFFSET:
541 		min = st->out_range.min;
542 		max = st->out_range.max;
543 		*val = ((min * (1 << 16)) / (max - min)) / 1000;
544 		return IIO_VAL_INT;
545 	default:
546 		return -EINVAL;
547 	}
548 }
549 
ad5758_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long info)550 static int ad5758_write_raw(struct iio_dev *indio_dev,
551 			    struct iio_chan_spec const *chan,
552 			    int val, int val2, long info)
553 {
554 	struct ad5758_state *st = iio_priv(indio_dev);
555 	int ret;
556 
557 	switch (info) {
558 	case IIO_CHAN_INFO_RAW:
559 		mutex_lock(&st->lock);
560 		ret = ad5758_spi_reg_write(st, AD5758_DAC_INPUT, val);
561 		mutex_unlock(&st->lock);
562 		return ret;
563 	default:
564 		return -EINVAL;
565 	}
566 }
567 
ad5758_read_powerdown(struct iio_dev * indio_dev,uintptr_t priv,const struct iio_chan_spec * chan,char * buf)568 static ssize_t ad5758_read_powerdown(struct iio_dev *indio_dev,
569 				     uintptr_t priv,
570 				     const struct iio_chan_spec *chan,
571 				     char *buf)
572 {
573 	struct ad5758_state *st = iio_priv(indio_dev);
574 
575 	return sysfs_emit(buf, "%d\n", st->pwr_down);
576 }
577 
ad5758_write_powerdown(struct iio_dev * indio_dev,uintptr_t priv,struct iio_chan_spec const * chan,const char * buf,size_t len)578 static ssize_t ad5758_write_powerdown(struct iio_dev *indio_dev,
579 				      uintptr_t priv,
580 				      struct iio_chan_spec const *chan,
581 				      const char *buf, size_t len)
582 {
583 	struct ad5758_state *st = iio_priv(indio_dev);
584 	bool pwr_down;
585 	unsigned int dac_config_mode, val;
586 	unsigned long int dac_config_msk;
587 	int ret;
588 
589 	ret = kstrtobool(buf, &pwr_down);
590 	if (ret)
591 		return ret;
592 
593 	mutex_lock(&st->lock);
594 	if (pwr_down)
595 		val = 0;
596 	else
597 		val = 1;
598 
599 	dac_config_mode = AD5758_DAC_CONFIG_OUT_EN_MODE(val) |
600 			  AD5758_DAC_CONFIG_INT_EN_MODE(val);
601 	dac_config_msk = AD5758_DAC_CONFIG_OUT_EN_MSK |
602 			 AD5758_DAC_CONFIG_INT_EN_MSK;
603 
604 	ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
605 				    dac_config_msk,
606 				    dac_config_mode);
607 	if (ret < 0)
608 		goto err_unlock;
609 
610 	st->pwr_down = pwr_down;
611 
612 err_unlock:
613 	mutex_unlock(&st->lock);
614 
615 	return ret ? ret : len;
616 }
617 
618 static const struct iio_info ad5758_info = {
619 	.read_raw = ad5758_read_raw,
620 	.write_raw = ad5758_write_raw,
621 	.debugfs_reg_access = &ad5758_reg_access,
622 };
623 
624 static const struct iio_chan_spec_ext_info ad5758_ext_info[] = {
625 	{
626 		.name = "powerdown",
627 		.read = ad5758_read_powerdown,
628 		.write = ad5758_write_powerdown,
629 		.shared = IIO_SHARED_BY_TYPE,
630 	},
631 	{ }
632 };
633 
634 #define AD5758_DAC_CHAN(_chan_type) {				\
635 	.type = (_chan_type),					\
636 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_RAW) |	\
637 		BIT(IIO_CHAN_INFO_SCALE) |			\
638 		BIT(IIO_CHAN_INFO_OFFSET),			\
639 	.indexed = 1,						\
640 	.output = 1,						\
641 	.ext_info = ad5758_ext_info,				\
642 }
643 
644 static const struct iio_chan_spec ad5758_voltage_ch[] = {
645 	AD5758_DAC_CHAN(IIO_VOLTAGE)
646 };
647 
648 static const struct iio_chan_spec ad5758_current_ch[] = {
649 	AD5758_DAC_CHAN(IIO_CURRENT)
650 };
651 
ad5758_is_valid_mode(enum ad5758_dc_dc_mode mode)652 static bool ad5758_is_valid_mode(enum ad5758_dc_dc_mode mode)
653 {
654 	switch (mode) {
655 	case AD5758_DCDC_MODE_DPC_CURRENT:
656 	case AD5758_DCDC_MODE_DPC_VOLTAGE:
657 	case AD5758_DCDC_MODE_PPC_CURRENT:
658 		return true;
659 	default:
660 		return false;
661 	}
662 }
663 
ad5758_crc_disable(struct ad5758_state * st)664 static int ad5758_crc_disable(struct ad5758_state *st)
665 {
666 	unsigned int mask;
667 
668 	mask = (AD5758_WR_FLAG_MSK(AD5758_DIGITAL_DIAG_CONFIG) << 24) | 0x5C3A;
669 	st->d32[0] = cpu_to_be32(mask);
670 
671 	return spi_write(st->spi, &st->d32[0], 4);
672 }
673 
ad5758_find_out_range(struct ad5758_state * st,const struct ad5758_range * range,unsigned int size,int min,int max)674 static int ad5758_find_out_range(struct ad5758_state *st,
675 				 const struct ad5758_range *range,
676 				 unsigned int size,
677 				 int min, int max)
678 {
679 	int i;
680 
681 	for (i = 0; i < size; i++) {
682 		if ((min == range[i].min) && (max == range[i].max)) {
683 			st->out_range.reg = range[i].reg;
684 			st->out_range.min = range[i].min;
685 			st->out_range.max = range[i].max;
686 
687 			return 0;
688 		}
689 	}
690 
691 	return -EINVAL;
692 }
693 
ad5758_parse_dt(struct ad5758_state * st)694 static int ad5758_parse_dt(struct ad5758_state *st)
695 {
696 	unsigned int tmp, tmparray[2], size;
697 	const struct ad5758_range *range;
698 	int *index, ret;
699 
700 	st->dc_dc_ilim = 0;
701 	ret = device_property_read_u32(&st->spi->dev,
702 				       "adi,dc-dc-ilim-microamp", &tmp);
703 	if (ret) {
704 		dev_dbg(&st->spi->dev,
705 			"Missing \"dc-dc-ilim-microamp\" property\n");
706 	} else {
707 		index = bsearch(&tmp, ad5758_dc_dc_ilim,
708 				ARRAY_SIZE(ad5758_dc_dc_ilim),
709 				sizeof(int), cmpfunc);
710 		if (!index)
711 			dev_dbg(&st->spi->dev, "dc-dc-ilim out of range\n");
712 		else
713 			st->dc_dc_ilim = index - ad5758_dc_dc_ilim;
714 	}
715 
716 	ret = device_property_read_u32(&st->spi->dev, "adi,dc-dc-mode",
717 				       &st->dc_dc_mode);
718 	if (ret) {
719 		dev_err(&st->spi->dev, "Missing \"dc-dc-mode\" property\n");
720 		return ret;
721 	}
722 
723 	if (!ad5758_is_valid_mode(st->dc_dc_mode))
724 		return -EINVAL;
725 
726 	if (st->dc_dc_mode == AD5758_DCDC_MODE_DPC_VOLTAGE) {
727 		ret = device_property_read_u32_array(&st->spi->dev,
728 						     "adi,range-microvolt",
729 						     tmparray, 2);
730 		if (ret) {
731 			dev_err(&st->spi->dev,
732 				"Missing \"range-microvolt\" property\n");
733 			return ret;
734 		}
735 		range = ad5758_voltage_range;
736 		size = ARRAY_SIZE(ad5758_voltage_range);
737 	} else {
738 		ret = device_property_read_u32_array(&st->spi->dev,
739 						     "adi,range-microamp",
740 						     tmparray, 2);
741 		if (ret) {
742 			dev_err(&st->spi->dev,
743 				"Missing \"range-microamp\" property\n");
744 			return ret;
745 		}
746 		range = ad5758_current_range;
747 		size = ARRAY_SIZE(ad5758_current_range);
748 	}
749 
750 	ret = ad5758_find_out_range(st, range, size, tmparray[0], tmparray[1]);
751 	if (ret) {
752 		dev_err(&st->spi->dev, "range invalid\n");
753 		return ret;
754 	}
755 
756 	ret = device_property_read_u32(&st->spi->dev, "adi,slew-time-us", &tmp);
757 	if (ret) {
758 		dev_dbg(&st->spi->dev, "Missing \"slew-time-us\" property\n");
759 		st->slew_time = 0;
760 	} else {
761 		st->slew_time = tmp;
762 	}
763 
764 	return 0;
765 }
766 
ad5758_init(struct ad5758_state * st)767 static int ad5758_init(struct ad5758_state *st)
768 {
769 	int regval, ret;
770 
771 	st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset",
772 						 GPIOD_OUT_HIGH);
773 	if (IS_ERR(st->gpio_reset))
774 		return PTR_ERR(st->gpio_reset);
775 
776 	/* Disable CRC checks */
777 	ret = ad5758_crc_disable(st);
778 	if (ret < 0)
779 		return ret;
780 
781 	/* Perform a reset */
782 	ret = ad5758_reset(st);
783 	if (ret < 0)
784 		return ret;
785 
786 	/* Disable CRC checks */
787 	ret = ad5758_crc_disable(st);
788 	if (ret < 0)
789 		return ret;
790 
791 	/* Perform a calibration memory refresh */
792 	ret = ad5758_calib_mem_refresh(st);
793 	if (ret < 0)
794 		return ret;
795 
796 	regval = ad5758_spi_reg_read(st, AD5758_DIGITAL_DIAG_RESULTS);
797 	if (regval < 0)
798 		return regval;
799 
800 	/* Clear all the error flags */
801 	ret = ad5758_spi_reg_write(st, AD5758_DIGITAL_DIAG_RESULTS, regval);
802 	if (ret < 0)
803 		return ret;
804 
805 	/* Set the dc-to-dc current limit */
806 	ret = ad5758_set_dc_dc_ilim(st, st->dc_dc_ilim);
807 	if (ret < 0)
808 		return ret;
809 
810 	/* Configure the dc-to-dc controller mode */
811 	ret = ad5758_set_dc_dc_conv_mode(st, st->dc_dc_mode);
812 	if (ret < 0)
813 		return ret;
814 
815 	/* Configure the output range */
816 	ret = ad5758_set_out_range(st, st->out_range.reg);
817 	if (ret < 0)
818 		return ret;
819 
820 	/* Enable Slew Rate Control, set the slew rate clock and step */
821 	if (st->slew_time) {
822 		ret = ad5758_slew_rate_config(st);
823 		if (ret < 0)
824 			return ret;
825 	}
826 
827 	/* Power up the DAC and internal (INT) amplifiers */
828 	ret = ad5758_internal_buffers_en(st, 1);
829 	if (ret < 0)
830 		return ret;
831 
832 	/* Enable VIOUT */
833 	return ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
834 				     AD5758_DAC_CONFIG_OUT_EN_MSK,
835 				     AD5758_DAC_CONFIG_OUT_EN_MODE(1));
836 }
837 
ad5758_probe(struct spi_device * spi)838 static int ad5758_probe(struct spi_device *spi)
839 {
840 	struct ad5758_state *st;
841 	struct iio_dev *indio_dev;
842 	int ret;
843 
844 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
845 	if (!indio_dev)
846 		return -ENOMEM;
847 
848 	st = iio_priv(indio_dev);
849 	spi_set_drvdata(spi, indio_dev);
850 
851 	st->spi = spi;
852 
853 	ret = devm_mutex_init(&spi->dev, &st->lock);
854 	if (ret)
855 		return ret;
856 
857 	indio_dev->name = spi_get_device_id(spi)->name;
858 	indio_dev->info = &ad5758_info;
859 	indio_dev->modes = INDIO_DIRECT_MODE;
860 	indio_dev->num_channels = 1;
861 
862 	ret = ad5758_parse_dt(st);
863 	if (ret < 0)
864 		return ret;
865 
866 	if (st->dc_dc_mode == AD5758_DCDC_MODE_DPC_VOLTAGE)
867 		indio_dev->channels = ad5758_voltage_ch;
868 	else
869 		indio_dev->channels = ad5758_current_ch;
870 
871 	ret = ad5758_init(st);
872 	if (ret < 0) {
873 		dev_err(&spi->dev, "AD5758 init failed\n");
874 		return ret;
875 	}
876 
877 	return devm_iio_device_register(&st->spi->dev, indio_dev);
878 }
879 
880 static const struct spi_device_id ad5758_id[] = {
881 	{ .name = "ad5758" },
882 	{ }
883 };
884 MODULE_DEVICE_TABLE(spi, ad5758_id);
885 
886 static const struct of_device_id ad5758_of_match[] = {
887         { .compatible = "adi,ad5758" },
888         { },
889 };
890 MODULE_DEVICE_TABLE(of, ad5758_of_match);
891 
892 static struct spi_driver ad5758_driver = {
893 	.driver = {
894 		.name = KBUILD_MODNAME,
895 		.of_match_table = ad5758_of_match,
896 	},
897 	.probe = ad5758_probe,
898 	.id_table = ad5758_id,
899 };
900 
901 module_spi_driver(ad5758_driver);
902 
903 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
904 MODULE_DESCRIPTION("Analog Devices AD5758 DAC");
905 MODULE_LICENSE("GPL v2");
906