Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / dac / ad5758.c
bloba513c70faefa1a895fe183aa2ab482be0492da30
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AD5758 Digital to analog converters driver
5 * Copyright 2018 Analog Devices Inc.
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>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
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
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)
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
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)
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)
82 /* AD5758_DIGITAL_DIAG_RESULTS */
83 #define AD5758_CAL_MEM_UNREFRESHED_MSK BIT(15)
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)
89 #define AD5758_WR_FLAG_MSK(x) (0x80 | ((x) & 0x1F))
91 #define AD5758_FULL_SCALE_MICRO 65535000000ULL
93 /**
94 * struct ad5758_state - driver instance specific data
95 * @spi: spi_device
96 * @lock: mutex lock
97 * @out_range: struct which stores the output range
98 * @dc_dc_mode: variable which stores the mode of operation
99 * @dc_dc_ilim: variable which stores the dc-to-dc converter current limit
100 * @slew_time: variable which stores the target slew time
101 * @pwr_down: variable which contains whether a channel is powered down or not
102 * @data: spi transfer buffers
105 struct ad5758_range {
106 int reg;
107 int min;
108 int max;
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];
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
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,
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,
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 }
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 },
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
177 static const int ad5758_sr_step[8] = {
178 4, 12, 64, 120, 256, 500, 1820, 2048
181 static const int ad5758_dc_dc_ilim[6] = {
182 150000, 200000, 250000, 300000, 350000, 400000
185 static int ad5758_spi_reg_read(struct ad5758_state *st, unsigned int addr)
187 struct spi_transfer t[] = {
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,
198 int ret;
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);
205 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
206 if (ret < 0)
207 return ret;
209 return (be32_to_cpu(st->d32[2]) >> 8) & 0xFFFF;
212 static int ad5758_spi_reg_write(struct ad5758_state *st,
213 unsigned int addr,
214 unsigned int val)
216 st->d32[0] = cpu_to_be32((AD5758_WR_FLAG_MSK(addr) << 24) |
217 ((val & 0xFFFF) << 8));
219 return spi_write(st->spi, &st->d32[0], sizeof(st->d32[0]));
222 static int ad5758_spi_write_mask(struct ad5758_state *st,
223 unsigned int addr,
224 unsigned long int mask,
225 unsigned int val)
227 int regval;
229 regval = ad5758_spi_reg_read(st, addr);
230 if (regval < 0)
231 return regval;
233 regval &= ~mask;
234 regval |= val;
236 return ad5758_spi_reg_write(st, addr, regval);
239 static int cmpfunc(const void *a, const void *b)
241 return *(int *)a - *(int *)b;
244 static int ad5758_find_closest_match(const int *array,
245 unsigned int size, int val)
247 int i;
249 for (i = 0; i < size; i++) {
250 if (val <= array[i])
251 return i;
254 return size - 1;
257 static int ad5758_wait_for_task_complete(struct ad5758_state *st,
258 unsigned int reg,
259 unsigned int mask)
261 unsigned int timeout;
262 int ret;
264 timeout = 10;
265 do {
266 ret = ad5758_spi_reg_read(st, reg);
267 if (ret < 0)
268 return ret;
270 if (!(ret & mask))
271 return 0;
273 usleep_range(100, 1000);
274 } while (--timeout);
276 dev_err(&st->spi->dev,
277 "Error reading bit 0x%x in 0x%x register\n", mask, reg);
279 return -EIO;
282 static int ad5758_calib_mem_refresh(struct ad5758_state *st)
284 int ret;
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;
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);
299 static int ad5758_soft_reset(struct ad5758_state *st)
301 int ret;
303 ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_1);
304 if (ret < 0)
305 return ret;
307 ret = ad5758_spi_reg_write(st, AD5758_KEY, AD5758_KEY_CODE_RESET_2);
309 /* Perform a software reset and wait at least 100us */
310 usleep_range(100, 1000);
312 return ret;
315 static int ad5758_set_dc_dc_conv_mode(struct ad5758_state *st,
316 enum ad5758_dc_dc_mode mode)
318 int ret;
321 * The ENABLE_PPC_BUFFERS bit must be set prior to enabling PPC current
322 * mode.
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;
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;
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.
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;
347 st->dc_dc_mode = mode;
349 return ret;
352 static int ad5758_set_dc_dc_ilim(struct ad5758_state *st, unsigned int ilim)
354 int ret;
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;
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.
365 return ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
366 AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
369 static int ad5758_slew_rate_set(struct ad5758_state *st,
370 unsigned int sr_clk_idx,
371 unsigned int sr_step_idx)
373 unsigned int mode;
374 unsigned long int mask;
375 int ret;
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);
384 ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG, mask, mode);
385 if (ret < 0)
386 return ret;
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);
393 static int ad5758_slew_rate_config(struct ad5758_state *st)
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;
400 sr_clk_idx = 0;
401 sr_step_idx = 0;
402 diff_old = S64_MAX;
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.
410 for (i = 0; i < ARRAY_SIZE(ad5758_sr_clk); i++) {
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)
416 sr_step = AD5758_FULL_SCALE_MICRO;
417 do_div(sr_step, ad5758_sr_clk[i]);
418 do_div(sr_step, st->slew_time);
420 * After a raw value for step size was determined, find the
421 * closest valid match
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]);
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
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;
443 return ad5758_slew_rate_set(st, sr_clk_idx, sr_step_idx);
446 static int ad5758_set_out_range(struct ad5758_state *st, int range)
448 int ret;
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;
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);
461 static int ad5758_internal_buffers_en(struct ad5758_state *st, bool enable)
463 int ret;
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;
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);
476 static int ad5758_reset(struct ad5758_state *st)
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);
484 return 0;
485 } else {
486 /* Perform a software reset */
487 return ad5758_soft_reset(st);
491 static int ad5758_reg_access(struct iio_dev *indio_dev,
492 unsigned int reg,
493 unsigned int writeval,
494 unsigned int *readval)
496 struct ad5758_state *st = iio_priv(indio_dev);
497 int ret;
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;
507 *readval = ret;
508 ret = 0;
509 } else {
510 ret = ad5758_spi_reg_write(st, reg, writeval);
512 mutex_unlock(&st->lock);
514 return ret;
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)
521 struct ad5758_state *st = iio_priv(indio_dev);
522 int max, min, ret;
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;
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;
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)
554 struct ad5758_state *st = iio_priv(indio_dev);
555 int ret;
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;
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)
573 struct ad5758_state *st = iio_priv(indio_dev);
575 return sprintf(buf, "%d\n", st->pwr_down);
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)
583 struct ad5758_state *st = iio_priv(indio_dev);
584 bool pwr_down;
585 unsigned int dc_dc_mode, dac_config_mode, val;
586 unsigned long int dac_config_msk;
587 int ret;
589 ret = kstrtobool(buf, &pwr_down);
590 if (ret)
591 return ret;
593 mutex_lock(&st->lock);
594 if (pwr_down) {
595 dc_dc_mode = AD5758_DCDC_MODE_POWER_OFF;
596 val = 0;
597 } else {
598 dc_dc_mode = st->dc_dc_mode;
599 val = 1;
602 dac_config_mode = AD5758_DAC_CONFIG_OUT_EN_MODE(val) |
603 AD5758_DAC_CONFIG_INT_EN_MODE(val);
604 dac_config_msk = AD5758_DAC_CONFIG_OUT_EN_MSK |
605 AD5758_DAC_CONFIG_INT_EN_MSK;
607 ret = ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
608 dac_config_msk,
609 dac_config_mode);
610 if (ret < 0)
611 goto err_unlock;
613 st->pwr_down = pwr_down;
615 err_unlock:
616 mutex_unlock(&st->lock);
618 return ret ? ret : len;
621 static const struct iio_info ad5758_info = {
622 .read_raw = ad5758_read_raw,
623 .write_raw = ad5758_write_raw,
624 .debugfs_reg_access = &ad5758_reg_access,
627 static const struct iio_chan_spec_ext_info ad5758_ext_info[] = {
629 .name = "powerdown",
630 .read = ad5758_read_powerdown,
631 .write = ad5758_write_powerdown,
632 .shared = IIO_SHARED_BY_TYPE,
637 #define AD5758_DAC_CHAN(_chan_type) { \
638 .type = (_chan_type), \
639 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_RAW) | \
640 BIT(IIO_CHAN_INFO_SCALE) | \
641 BIT(IIO_CHAN_INFO_OFFSET), \
642 .indexed = 1, \
643 .output = 1, \
644 .ext_info = ad5758_ext_info, \
647 static const struct iio_chan_spec ad5758_voltage_ch[] = {
648 AD5758_DAC_CHAN(IIO_VOLTAGE)
651 static const struct iio_chan_spec ad5758_current_ch[] = {
652 AD5758_DAC_CHAN(IIO_CURRENT)
655 static bool ad5758_is_valid_mode(enum ad5758_dc_dc_mode mode)
657 switch (mode) {
658 case AD5758_DCDC_MODE_DPC_CURRENT:
659 case AD5758_DCDC_MODE_DPC_VOLTAGE:
660 case AD5758_DCDC_MODE_PPC_CURRENT:
661 return true;
662 default:
663 return false;
667 static int ad5758_crc_disable(struct ad5758_state *st)
669 unsigned int mask;
671 mask = (AD5758_WR_FLAG_MSK(AD5758_DIGITAL_DIAG_CONFIG) << 24) | 0x5C3A;
672 st->d32[0] = cpu_to_be32(mask);
674 return spi_write(st->spi, &st->d32[0], 4);
677 static int ad5758_find_out_range(struct ad5758_state *st,
678 const struct ad5758_range *range,
679 unsigned int size,
680 int min, int max)
682 int i;
684 for (i = 0; i < size; i++) {
685 if ((min == range[i].min) && (max == range[i].max)) {
686 st->out_range.reg = range[i].reg;
687 st->out_range.min = range[i].min;
688 st->out_range.max = range[i].max;
690 return 0;
694 return -EINVAL;
697 static int ad5758_parse_dt(struct ad5758_state *st)
699 unsigned int tmp, tmparray[2], size;
700 const struct ad5758_range *range;
701 int *index, ret;
703 st->dc_dc_ilim = 0;
704 ret = device_property_read_u32(&st->spi->dev,
705 "adi,dc-dc-ilim-microamp", &tmp);
706 if (ret) {
707 dev_dbg(&st->spi->dev,
708 "Missing \"dc-dc-ilim-microamp\" property\n");
709 } else {
710 index = bsearch(&tmp, ad5758_dc_dc_ilim,
711 ARRAY_SIZE(ad5758_dc_dc_ilim),
712 sizeof(int), cmpfunc);
713 if (!index)
714 dev_dbg(&st->spi->dev, "dc-dc-ilim out of range\n");
715 else
716 st->dc_dc_ilim = index - ad5758_dc_dc_ilim;
719 ret = device_property_read_u32(&st->spi->dev, "adi,dc-dc-mode",
720 &st->dc_dc_mode);
721 if (ret) {
722 dev_err(&st->spi->dev, "Missing \"dc-dc-mode\" property\n");
723 return ret;
726 if (!ad5758_is_valid_mode(st->dc_dc_mode))
727 return -EINVAL;
729 if (st->dc_dc_mode == AD5758_DCDC_MODE_DPC_VOLTAGE) {
730 ret = device_property_read_u32_array(&st->spi->dev,
731 "adi,range-microvolt",
732 tmparray, 2);
733 if (ret) {
734 dev_err(&st->spi->dev,
735 "Missing \"range-microvolt\" property\n");
736 return ret;
738 range = ad5758_voltage_range;
739 size = ARRAY_SIZE(ad5758_voltage_range);
740 } else {
741 ret = device_property_read_u32_array(&st->spi->dev,
742 "adi,range-microamp",
743 tmparray, 2);
744 if (ret) {
745 dev_err(&st->spi->dev,
746 "Missing \"range-microamp\" property\n");
747 return ret;
749 range = ad5758_current_range;
750 size = ARRAY_SIZE(ad5758_current_range);
753 ret = ad5758_find_out_range(st, range, size, tmparray[0], tmparray[1]);
754 if (ret) {
755 dev_err(&st->spi->dev, "range invalid\n");
756 return ret;
759 ret = device_property_read_u32(&st->spi->dev, "adi,slew-time-us", &tmp);
760 if (ret) {
761 dev_dbg(&st->spi->dev, "Missing \"slew-time-us\" property\n");
762 st->slew_time = 0;
763 } else {
764 st->slew_time = tmp;
767 return 0;
770 static int ad5758_init(struct ad5758_state *st)
772 int regval, ret;
774 st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset",
775 GPIOD_OUT_HIGH);
776 if (IS_ERR(st->gpio_reset))
777 return PTR_ERR(st->gpio_reset);
779 /* Disable CRC checks */
780 ret = ad5758_crc_disable(st);
781 if (ret < 0)
782 return ret;
784 /* Perform a reset */
785 ret = ad5758_reset(st);
786 if (ret < 0)
787 return ret;
789 /* Disable CRC checks */
790 ret = ad5758_crc_disable(st);
791 if (ret < 0)
792 return ret;
794 /* Perform a calibration memory refresh */
795 ret = ad5758_calib_mem_refresh(st);
796 if (ret < 0)
797 return ret;
799 regval = ad5758_spi_reg_read(st, AD5758_DIGITAL_DIAG_RESULTS);
800 if (regval < 0)
801 return regval;
803 /* Clear all the error flags */
804 ret = ad5758_spi_reg_write(st, AD5758_DIGITAL_DIAG_RESULTS, regval);
805 if (ret < 0)
806 return ret;
808 /* Set the dc-to-dc current limit */
809 ret = ad5758_set_dc_dc_ilim(st, st->dc_dc_ilim);
810 if (ret < 0)
811 return ret;
813 /* Configure the dc-to-dc controller mode */
814 ret = ad5758_set_dc_dc_conv_mode(st, st->dc_dc_mode);
815 if (ret < 0)
816 return ret;
818 /* Configure the output range */
819 ret = ad5758_set_out_range(st, st->out_range.reg);
820 if (ret < 0)
821 return ret;
823 /* Enable Slew Rate Control, set the slew rate clock and step */
824 if (st->slew_time) {
825 ret = ad5758_slew_rate_config(st);
826 if (ret < 0)
827 return ret;
830 /* Power up the DAC and internal (INT) amplifiers */
831 ret = ad5758_internal_buffers_en(st, 1);
832 if (ret < 0)
833 return ret;
835 /* Enable VIOUT */
836 return ad5758_spi_write_mask(st, AD5758_DAC_CONFIG,
837 AD5758_DAC_CONFIG_OUT_EN_MSK,
838 AD5758_DAC_CONFIG_OUT_EN_MODE(1));
841 static int ad5758_probe(struct spi_device *spi)
843 struct ad5758_state *st;
844 struct iio_dev *indio_dev;
845 int ret;
847 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
848 if (!indio_dev)
849 return -ENOMEM;
851 st = iio_priv(indio_dev);
852 spi_set_drvdata(spi, indio_dev);
854 st->spi = spi;
856 mutex_init(&st->lock);
858 indio_dev->dev.parent = &spi->dev;
859 indio_dev->name = spi_get_device_id(spi)->name;
860 indio_dev->info = &ad5758_info;
861 indio_dev->modes = INDIO_DIRECT_MODE;
862 indio_dev->num_channels = 1;
864 ret = ad5758_parse_dt(st);
865 if (ret < 0)
866 return ret;
868 if (st->dc_dc_mode == AD5758_DCDC_MODE_DPC_VOLTAGE)
869 indio_dev->channels = ad5758_voltage_ch;
870 else
871 indio_dev->channels = ad5758_current_ch;
873 ret = ad5758_init(st);
874 if (ret < 0) {
875 dev_err(&spi->dev, "AD5758 init failed\n");
876 return ret;
879 return devm_iio_device_register(&st->spi->dev, indio_dev);
882 static const struct spi_device_id ad5758_id[] = {
883 { "ad5758", 0 },
886 MODULE_DEVICE_TABLE(spi, ad5758_id);
888 static struct spi_driver ad5758_driver = {
889 .driver = {
890 .name = KBUILD_MODNAME,
892 .probe = ad5758_probe,
893 .id_table = ad5758_id,
896 module_spi_driver(ad5758_driver);
898 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
899 MODULE_DESCRIPTION("Analog Devices AD5758 DAC");
900 MODULE_LICENSE("GPL v2");