1 // SPDX-License-Identifier: GPL-2.0
3 * AD5758 Digital to analog converters driver
5 * Copyright 2018 Analog Devices Inc.
7 * TODO: Currently CRC is not supported in this driver
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)
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
94 * struct ad5758_state - driver instance specific data
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
{
111 struct ad5758_state
{
112 struct spi_device
*spi
;
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
;
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
{
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],
193 .tx_buf
= &st
->d32
[1],
194 .rx_buf
= &st
->d32
[2],
200 st
->d32
[0] = cpu_to_be32(
201 (AD5758_WR_FLAG_MSK(AD5758_TWO_STAGE_READBACK_SELECT
) << 24) |
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
));
209 return (be32_to_cpu(st
->d32
[2]) >> 8) & 0xFFFF;
212 static int ad5758_spi_reg_write(struct ad5758_state
*st
,
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
,
224 unsigned long int mask
,
229 regval
= ad5758_spi_reg_read(st
, addr
);
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
)
249 for (i
= 0; i
< size
; i
++) {
257 static int ad5758_wait_for_task_complete(struct ad5758_state
*st
,
261 unsigned int timeout
;
266 ret
= ad5758_spi_reg_read(st
, reg
);
273 usleep_range(100, 1000);
276 dev_err(&st
->spi
->dev
,
277 "Error reading bit 0x%x in 0x%x register\n", mask
, reg
);
282 static int ad5758_calib_mem_refresh(struct ad5758_state
*st
)
286 ret
= ad5758_spi_reg_write(st
, AD5758_KEY
,
287 AD5758_KEY_CODE_CALIB_MEM_REFRESH
);
289 dev_err(&st
->spi
->dev
,
290 "Failed to initiate a calibration memory refresh\n");
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
)
303 ret
= ad5758_spi_reg_write(st
, AD5758_KEY
, AD5758_KEY_CODE_RESET_1
);
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);
315 static int ad5758_set_dc_dc_conv_mode(struct ad5758_state
*st
,
316 enum ad5758_dc_dc_mode mode
)
321 * The ENABLE_PPC_BUFFERS bit must be set prior to enabling PPC current
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));
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
));
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
);
347 st
->dc_dc_mode
= mode
;
352 static int ad5758_set_dc_dc_ilim(struct ad5758_state
*st
, unsigned int ilim
)
356 ret
= ad5758_spi_write_mask(st
, AD5758_DCDC_CONFIG2
,
357 AD5758_DCDC_CONFIG2_ILIMIT_MSK
,
358 AD5758_DCDC_CONFIG2_ILIMIT_MODE(ilim
));
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
)
374 unsigned long int mask
;
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
);
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
;
397 s64 diff_new
, diff_old
;
398 u64 sr_step
, calc_slew_time
;
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
),
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
) {
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
)
450 ret
= ad5758_spi_write_mask(st
, AD5758_DAC_CONFIG
,
451 AD5758_DAC_CONFIG_RANGE_MSK
,
452 AD5758_DAC_CONFIG_RANGE_MODE(range
));
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
)
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
));
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);
486 /* Perform a software reset */
487 return ad5758_soft_reset(st
);
491 static int ad5758_reg_access(struct iio_dev
*indio_dev
,
493 unsigned int writeval
,
494 unsigned int *readval
)
496 struct ad5758_state
*st
= iio_priv(indio_dev
);
499 mutex_lock(&st
->lock
);
501 ret
= ad5758_spi_reg_read(st
, reg
);
503 mutex_unlock(&st
->lock
);
510 ret
= ad5758_spi_reg_write(st
, reg
, writeval
);
512 mutex_unlock(&st
->lock
);
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
);
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
);
534 case IIO_CHAN_INFO_SCALE
:
535 min
= st
->out_range
.min
;
536 max
= st
->out_range
.max
;
537 *val
= (max
- min
) / 1000;
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;
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
);
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
);
568 static ssize_t
ad5758_read_powerdown(struct iio_dev
*indio_dev
,
570 const struct iio_chan_spec
*chan
,
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
,
580 struct iio_chan_spec
const *chan
,
581 const char *buf
, size_t len
)
583 struct ad5758_state
*st
= iio_priv(indio_dev
);
585 unsigned int dc_dc_mode
, dac_config_mode
, val
;
586 unsigned long int dac_config_msk
;
589 ret
= kstrtobool(buf
, &pwr_down
);
593 mutex_lock(&st
->lock
);
595 dc_dc_mode
= AD5758_DCDC_MODE_POWER_OFF
;
598 dc_dc_mode
= st
->dc_dc_mode
;
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
,
613 st
->pwr_down
= pwr_down
;
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
[] = {
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), \
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
)
658 case AD5758_DCDC_MODE_DPC_CURRENT
:
659 case AD5758_DCDC_MODE_DPC_VOLTAGE
:
660 case AD5758_DCDC_MODE_PPC_CURRENT
:
667 static int ad5758_crc_disable(struct ad5758_state
*st
)
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
,
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
;
697 static int ad5758_parse_dt(struct ad5758_state
*st
)
699 unsigned int tmp
, tmparray
[2], size
;
700 const struct ad5758_range
*range
;
704 ret
= device_property_read_u32(&st
->spi
->dev
,
705 "adi,dc-dc-ilim-microamp", &tmp
);
707 dev_dbg(&st
->spi
->dev
,
708 "Missing \"dc-dc-ilim-microamp\" property\n");
710 index
= bsearch(&tmp
, ad5758_dc_dc_ilim
,
711 ARRAY_SIZE(ad5758_dc_dc_ilim
),
712 sizeof(int), cmpfunc
);
714 dev_dbg(&st
->spi
->dev
, "dc-dc-ilim out of range\n");
716 st
->dc_dc_ilim
= index
- ad5758_dc_dc_ilim
;
719 ret
= device_property_read_u32(&st
->spi
->dev
, "adi,dc-dc-mode",
722 dev_err(&st
->spi
->dev
, "Missing \"dc-dc-mode\" property\n");
726 if (!ad5758_is_valid_mode(st
->dc_dc_mode
))
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",
734 dev_err(&st
->spi
->dev
,
735 "Missing \"range-microvolt\" property\n");
738 range
= ad5758_voltage_range
;
739 size
= ARRAY_SIZE(ad5758_voltage_range
);
741 ret
= device_property_read_u32_array(&st
->spi
->dev
,
742 "adi,range-microamp",
745 dev_err(&st
->spi
->dev
,
746 "Missing \"range-microamp\" property\n");
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]);
755 dev_err(&st
->spi
->dev
, "range invalid\n");
759 ret
= device_property_read_u32(&st
->spi
->dev
, "adi,slew-time-us", &tmp
);
761 dev_dbg(&st
->spi
->dev
, "Missing \"slew-time-us\" property\n");
770 static int ad5758_init(struct ad5758_state
*st
)
774 st
->gpio_reset
= devm_gpiod_get_optional(&st
->spi
->dev
, "reset",
776 if (IS_ERR(st
->gpio_reset
))
777 return PTR_ERR(st
->gpio_reset
);
779 /* Disable CRC checks */
780 ret
= ad5758_crc_disable(st
);
784 /* Perform a reset */
785 ret
= ad5758_reset(st
);
789 /* Disable CRC checks */
790 ret
= ad5758_crc_disable(st
);
794 /* Perform a calibration memory refresh */
795 ret
= ad5758_calib_mem_refresh(st
);
799 regval
= ad5758_spi_reg_read(st
, AD5758_DIGITAL_DIAG_RESULTS
);
803 /* Clear all the error flags */
804 ret
= ad5758_spi_reg_write(st
, AD5758_DIGITAL_DIAG_RESULTS
, regval
);
808 /* Set the dc-to-dc current limit */
809 ret
= ad5758_set_dc_dc_ilim(st
, st
->dc_dc_ilim
);
813 /* Configure the dc-to-dc controller mode */
814 ret
= ad5758_set_dc_dc_conv_mode(st
, st
->dc_dc_mode
);
818 /* Configure the output range */
819 ret
= ad5758_set_out_range(st
, st
->out_range
.reg
);
823 /* Enable Slew Rate Control, set the slew rate clock and step */
825 ret
= ad5758_slew_rate_config(st
);
830 /* Power up the DAC and internal (INT) amplifiers */
831 ret
= ad5758_internal_buffers_en(st
, 1);
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
;
847 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
851 st
= iio_priv(indio_dev
);
852 spi_set_drvdata(spi
, indio_dev
);
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
);
868 if (st
->dc_dc_mode
== AD5758_DCDC_MODE_DPC_VOLTAGE
)
869 indio_dev
->channels
= ad5758_voltage_ch
;
871 indio_dev
->channels
= ad5758_current_ch
;
873 ret
= ad5758_init(st
);
875 dev_err(&spi
->dev
, "AD5758 init failed\n");
879 return devm_iio_device_register(&st
->spi
->dev
, indio_dev
);
882 static const struct spi_device_id ad5758_id
[] = {
886 MODULE_DEVICE_TABLE(spi
, ad5758_id
);
888 static struct spi_driver ad5758_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");