1 // SPDX-License-Identifier: GPL-2.0+
3 * AD5770R Digital to analog converters driver
5 * Copyright 2018 Analog Devices Inc.
8 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/spi/spi.h>
21 #define ADI_SPI_IF_CONFIG_A 0x00
22 #define ADI_SPI_IF_CONFIG_B 0x01
23 #define ADI_SPI_IF_DEVICE_CONFIG 0x02
24 #define ADI_SPI_IF_CHIP_TYPE 0x03
25 #define ADI_SPI_IF_PRODUCT_ID_L 0x04
26 #define ADI_SPI_IF_PRODUCT_ID_H 0x05
27 #define ADI_SPI_IF_CHIP_GRADE 0x06
28 #define ADI_SPI_IF_SCRACTH_PAD 0x0A
29 #define ADI_SPI_IF_SPI_REVISION 0x0B
30 #define ADI_SPI_IF_SPI_VENDOR_L 0x0C
31 #define ADI_SPI_IF_SPI_VENDOR_H 0x0D
32 #define ADI_SPI_IF_SPI_STREAM_MODE 0x0E
33 #define ADI_SPI_IF_CONFIG_C 0x10
34 #define ADI_SPI_IF_STATUS_A 0x11
36 /* ADI_SPI_IF_CONFIG_A */
37 #define ADI_SPI_IF_SW_RESET_MSK (BIT(0) | BIT(7))
38 #define ADI_SPI_IF_SW_RESET_SEL(x) ((x) & ADI_SPI_IF_SW_RESET_MSK)
39 #define ADI_SPI_IF_ADDR_ASC_MSK (BIT(2) | BIT(5))
40 #define ADI_SPI_IF_ADDR_ASC_SEL(x) (((x) << 2) & ADI_SPI_IF_ADDR_ASC_MSK)
42 /* ADI_SPI_IF_CONFIG_B */
43 #define ADI_SPI_IF_SINGLE_INS_MSK BIT(7)
44 #define ADI_SPI_IF_SINGLE_INS_SEL(x) FIELD_PREP(ADI_SPI_IF_SINGLE_INS_MSK, x)
45 #define ADI_SPI_IF_SHORT_INS_MSK BIT(7)
46 #define ADI_SPI_IF_SHORT_INS_SEL(x) FIELD_PREP(ADI_SPI_IF_SINGLE_INS_MSK, x)
48 /* ADI_SPI_IF_CONFIG_C */
49 #define ADI_SPI_IF_STRICT_REG_MSK BIT(5)
50 #define ADI_SPI_IF_STRICT_REG_GET(x) FIELD_GET(ADI_SPI_IF_STRICT_REG_MSK, x)
52 /* AD5770R configuration registers */
53 #define AD5770R_CHANNEL_CONFIG 0x14
54 #define AD5770R_OUTPUT_RANGE(ch) (0x15 + (ch))
55 #define AD5770R_FILTER_RESISTOR(ch) (0x1D + (ch))
56 #define AD5770R_REFERENCE 0x1B
57 #define AD5770R_DAC_LSB(ch) (0x26 + 2 * (ch))
58 #define AD5770R_DAC_MSB(ch) (0x27 + 2 * (ch))
59 #define AD5770R_CH_SELECT 0x34
60 #define AD5770R_CH_ENABLE 0x44
62 /* AD5770R_CHANNEL_CONFIG */
63 #define AD5770R_CFG_CH0_SINK_EN(x) (((x) & 0x1) << 7)
64 #define AD5770R_CFG_SHUTDOWN_B(x, ch) (((x) & 0x1) << (ch))
66 /* AD5770R_OUTPUT_RANGE */
67 #define AD5770R_RANGE_OUTPUT_SCALING(x) (((x) & GENMASK(5, 0)) << 2)
68 #define AD5770R_RANGE_MODE(x) ((x) & GENMASK(1, 0))
70 /* AD5770R_REFERENCE */
71 #define AD5770R_REF_RESISTOR_SEL(x) (((x) & 0x1) << 2)
72 #define AD5770R_REF_SEL(x) ((x) & GENMASK(1, 0))
74 /* AD5770R_CH_ENABLE */
75 #define AD5770R_CH_SET(x, ch) (((x) & 0x1) << (ch))
77 #define AD5770R_MAX_CHANNELS 6
78 #define AD5770R_MAX_CH_MODES 14
79 #define AD5770R_LOW_VREF_mV 1250
80 #define AD5770R_HIGH_VREF_mV 2500
82 enum ad5770r_ch0_modes
{
83 AD5770R_CH0_0_300
= 0,
85 AD5770R_CH0_NEG_60_300
88 enum ad5770r_ch1_modes
{
89 AD5770R_CH1_0_140_LOW_HEAD
= 1,
90 AD5770R_CH1_0_140_LOW_NOISE
,
94 enum ad5770r_ch2_5_modes
{
95 AD5770R_CH_LOW_RANGE
= 0,
100 AD5770R_EXT_2_5_V
= 0,
101 AD5770R_INT_1_25_V_OUT_ON
,
103 AD5770R_INT_1_25_V_OUT_OFF
106 enum ad5770r_output_filter_resistor
{
107 AD5770R_FILTER_60_OHM
= 0x0,
108 AD5770R_FILTER_5_6_KOHM
= 0x5,
109 AD5770R_FILTER_11_2_KOHM
,
110 AD5770R_FILTER_22_2_KOHM
,
111 AD5770R_FILTER_44_4_KOHM
,
112 AD5770R_FILTER_104_KOHM
,
115 struct ad5770r_out_range
{
121 * struct ad5770R_state - driver instance specific data
124 * @vref_reg: fixed regulator for reference configuration
125 * @gpio_reset: gpio descriptor
126 * @output_mode: array contains channels output ranges
127 * @vref: reference value
128 * @ch_pwr_down: powerdown flags
129 * @internal_ref: internal reference flag
130 * @external_res: external 2.5k resistor flag
131 * @transf_buf: cache aligned buffer for spi read/write
133 struct ad5770r_state
{
134 struct spi_device
*spi
;
135 struct regmap
*regmap
;
136 struct regulator
*vref_reg
;
137 struct gpio_desc
*gpio_reset
;
138 struct ad5770r_out_range output_mode
[AD5770R_MAX_CHANNELS
];
140 bool ch_pwr_down
[AD5770R_MAX_CHANNELS
];
143 u8 transf_buf
[2] ____cacheline_aligned
;
146 static const struct regmap_config ad5770r_spi_regmap_config
= {
149 .read_flag_mask
= BIT(7),
152 struct ad5770r_output_modes
{
159 static struct ad5770r_output_modes ad5770r_rng_tbl
[] = {
160 { 0, AD5770R_CH0_0_300
, 0, 300 },
161 { 0, AD5770R_CH0_NEG_60_0
, -60, 0 },
162 { 0, AD5770R_CH0_NEG_60_300
, -60, 300 },
163 { 1, AD5770R_CH1_0_140_LOW_HEAD
, 0, 140 },
164 { 1, AD5770R_CH1_0_140_LOW_NOISE
, 0, 140 },
165 { 1, AD5770R_CH1_0_250
, 0, 250 },
166 { 2, AD5770R_CH_LOW_RANGE
, 0, 55 },
167 { 2, AD5770R_CH_HIGH_RANGE
, 0, 150 },
168 { 3, AD5770R_CH_LOW_RANGE
, 0, 45 },
169 { 3, AD5770R_CH_HIGH_RANGE
, 0, 100 },
170 { 4, AD5770R_CH_LOW_RANGE
, 0, 45 },
171 { 4, AD5770R_CH_HIGH_RANGE
, 0, 100 },
172 { 5, AD5770R_CH_LOW_RANGE
, 0, 45 },
173 { 5, AD5770R_CH_HIGH_RANGE
, 0, 100 },
176 static const unsigned int ad5770r_filter_freqs
[] = {
177 153, 357, 715, 1400, 2800, 262000,
180 static const unsigned int ad5770r_filter_reg_vals
[] = {
181 AD5770R_FILTER_104_KOHM
,
182 AD5770R_FILTER_44_4_KOHM
,
183 AD5770R_FILTER_22_2_KOHM
,
184 AD5770R_FILTER_11_2_KOHM
,
185 AD5770R_FILTER_5_6_KOHM
,
186 AD5770R_FILTER_60_OHM
189 static int ad5770r_set_output_mode(struct ad5770r_state
*st
,
190 const struct ad5770r_out_range
*out_mode
,
195 regval
= AD5770R_RANGE_OUTPUT_SCALING(out_mode
->out_scale
) |
196 AD5770R_RANGE_MODE(out_mode
->out_range_mode
);
198 return regmap_write(st
->regmap
,
199 AD5770R_OUTPUT_RANGE(channel
), regval
);
202 static int ad5770r_set_reference(struct ad5770r_state
*st
)
206 regval
= AD5770R_REF_RESISTOR_SEL(st
->external_res
);
208 if (st
->internal_ref
) {
209 regval
|= AD5770R_REF_SEL(AD5770R_INT_1_25_V_OUT_OFF
);
212 case AD5770R_LOW_VREF_mV
:
213 regval
|= AD5770R_REF_SEL(AD5770R_EXT_1_25_V
);
215 case AD5770R_HIGH_VREF_mV
:
216 regval
|= AD5770R_REF_SEL(AD5770R_EXT_2_5_V
);
219 regval
= AD5770R_REF_SEL(AD5770R_INT_1_25_V_OUT_OFF
);
224 return regmap_write(st
->regmap
, AD5770R_REFERENCE
, regval
);
227 static int ad5770r_soft_reset(struct ad5770r_state
*st
)
229 return regmap_write(st
->regmap
, ADI_SPI_IF_CONFIG_A
,
230 ADI_SPI_IF_SW_RESET_SEL(1));
233 static int ad5770r_reset(struct ad5770r_state
*st
)
235 /* Perform software reset if no GPIO provided */
237 return ad5770r_soft_reset(st
);
239 gpiod_set_value_cansleep(st
->gpio_reset
, 0);
240 usleep_range(10, 20);
241 gpiod_set_value_cansleep(st
->gpio_reset
, 1);
243 /* data must not be written during reset timeframe */
244 usleep_range(100, 200);
249 static int ad5770r_get_range(struct ad5770r_state
*st
,
250 int ch
, int *min
, int *max
)
253 u8 tbl_ch
, tbl_mode
, out_range
;
255 out_range
= st
->output_mode
[ch
].out_range_mode
;
257 for (i
= 0; i
< AD5770R_MAX_CH_MODES
; i
++) {
258 tbl_ch
= ad5770r_rng_tbl
[i
].ch
;
259 tbl_mode
= ad5770r_rng_tbl
[i
].mode
;
260 if (tbl_ch
== ch
&& tbl_mode
== out_range
) {
261 *min
= ad5770r_rng_tbl
[i
].min
;
262 *max
= ad5770r_rng_tbl
[i
].max
;
270 static int ad5770r_get_filter_freq(struct iio_dev
*indio_dev
,
271 const struct iio_chan_spec
*chan
, int *freq
)
273 struct ad5770r_state
*st
= iio_priv(indio_dev
);
275 unsigned int regval
, i
;
277 ret
= regmap_read(st
->regmap
,
278 AD5770R_FILTER_RESISTOR(chan
->channel
), ®val
);
282 for (i
= 0; i
< ARRAY_SIZE(ad5770r_filter_reg_vals
); i
++)
283 if (regval
== ad5770r_filter_reg_vals
[i
])
285 if (i
== ARRAY_SIZE(ad5770r_filter_reg_vals
))
288 *freq
= ad5770r_filter_freqs
[i
];
293 static int ad5770r_set_filter_freq(struct iio_dev
*indio_dev
,
294 const struct iio_chan_spec
*chan
,
297 struct ad5770r_state
*st
= iio_priv(indio_dev
);
298 unsigned int regval
, i
;
300 for (i
= 0; i
< ARRAY_SIZE(ad5770r_filter_freqs
); i
++)
301 if (ad5770r_filter_freqs
[i
] >= freq
)
303 if (i
== ARRAY_SIZE(ad5770r_filter_freqs
))
306 regval
= ad5770r_filter_reg_vals
[i
];
308 return regmap_write(st
->regmap
, AD5770R_FILTER_RESISTOR(chan
->channel
),
312 static int ad5770r_read_raw(struct iio_dev
*indio_dev
,
313 struct iio_chan_spec
const *chan
,
314 int *val
, int *val2
, long info
)
316 struct ad5770r_state
*st
= iio_priv(indio_dev
);
321 case IIO_CHAN_INFO_RAW
:
322 ret
= regmap_bulk_read(st
->regmap
,
328 buf16
= st
->transf_buf
[0] + (st
->transf_buf
[1] << 8);
331 case IIO_CHAN_INFO_SCALE
:
332 ret
= ad5770r_get_range(st
, chan
->channel
, &min
, &max
);
336 /* There is no sign bit. (negative current is mapped from 0)
337 * (sourced/sinked) current = raw * scale + offset
338 * where offset in case of CH0 can be negative.
341 return IIO_VAL_FRACTIONAL_LOG2
;
342 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
343 return ad5770r_get_filter_freq(indio_dev
, chan
, val
);
344 case IIO_CHAN_INFO_OFFSET
:
345 ret
= ad5770r_get_range(st
, chan
->channel
, &min
, &max
);
355 static int ad5770r_write_raw(struct iio_dev
*indio_dev
,
356 struct iio_chan_spec
const *chan
,
357 int val
, int val2
, long info
)
359 struct ad5770r_state
*st
= iio_priv(indio_dev
);
362 case IIO_CHAN_INFO_RAW
:
363 st
->transf_buf
[0] = ((u16
)val
>> 6);
364 st
->transf_buf
[1] = (val
& GENMASK(5, 0)) << 2;
365 return regmap_bulk_write(st
->regmap
, chan
->address
,
367 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
368 return ad5770r_set_filter_freq(indio_dev
, chan
, val
);
374 static int ad5770r_read_freq_avail(struct iio_dev
*indio_dev
,
375 struct iio_chan_spec
const *chan
,
376 const int **vals
, int *type
, int *length
,
380 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
382 *vals
= ad5770r_filter_freqs
;
383 *length
= ARRAY_SIZE(ad5770r_filter_freqs
);
384 return IIO_AVAIL_LIST
;
390 static int ad5770r_reg_access(struct iio_dev
*indio_dev
,
392 unsigned int writeval
,
393 unsigned int *readval
)
395 struct ad5770r_state
*st
= iio_priv(indio_dev
);
398 return regmap_read(st
->regmap
, reg
, readval
);
400 return regmap_write(st
->regmap
, reg
, writeval
);
403 static const struct iio_info ad5770r_info
= {
404 .read_raw
= ad5770r_read_raw
,
405 .write_raw
= ad5770r_write_raw
,
406 .read_avail
= ad5770r_read_freq_avail
,
407 .debugfs_reg_access
= &ad5770r_reg_access
,
410 static int ad5770r_store_output_range(struct ad5770r_state
*st
,
411 int min
, int max
, int index
)
415 for (i
= 0; i
< AD5770R_MAX_CH_MODES
; i
++) {
416 if (ad5770r_rng_tbl
[i
].ch
!= index
)
418 if (ad5770r_rng_tbl
[i
].min
!= min
||
419 ad5770r_rng_tbl
[i
].max
!= max
)
421 st
->output_mode
[index
].out_range_mode
= ad5770r_rng_tbl
[i
].mode
;
429 static ssize_t
ad5770r_read_dac_powerdown(struct iio_dev
*indio_dev
,
431 const struct iio_chan_spec
*chan
,
434 struct ad5770r_state
*st
= iio_priv(indio_dev
);
436 return sprintf(buf
, "%d\n", st
->ch_pwr_down
[chan
->channel
]);
439 static ssize_t
ad5770r_write_dac_powerdown(struct iio_dev
*indio_dev
,
441 const struct iio_chan_spec
*chan
,
442 const char *buf
, size_t len
)
444 struct ad5770r_state
*st
= iio_priv(indio_dev
);
450 ret
= kstrtobool(buf
, &readin
);
456 regval
= AD5770R_CFG_SHUTDOWN_B(readin
, chan
->channel
);
457 if (chan
->channel
== 0 &&
458 st
->output_mode
[0].out_range_mode
> AD5770R_CH0_0_300
) {
459 regval
|= AD5770R_CFG_CH0_SINK_EN(readin
);
460 mask
= BIT(chan
->channel
) + BIT(7);
462 mask
= BIT(chan
->channel
);
464 ret
= regmap_update_bits(st
->regmap
, AD5770R_CHANNEL_CONFIG
, mask
,
469 regval
= AD5770R_CH_SET(readin
, chan
->channel
);
470 ret
= regmap_update_bits(st
->regmap
, AD5770R_CH_ENABLE
,
471 BIT(chan
->channel
), regval
);
475 st
->ch_pwr_down
[chan
->channel
] = !readin
;
480 static const struct iio_chan_spec_ext_info ad5770r_ext_info
[] = {
483 .read
= ad5770r_read_dac_powerdown
,
484 .write
= ad5770r_write_dac_powerdown
,
485 .shared
= IIO_SEPARATE
,
490 #define AD5770R_IDAC_CHANNEL(index, reg) { \
491 .type = IIO_CURRENT, \
496 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
497 BIT(IIO_CHAN_INFO_SCALE) | \
498 BIT(IIO_CHAN_INFO_OFFSET) | \
499 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
500 .info_mask_shared_by_type_available = \
501 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
502 .ext_info = ad5770r_ext_info, \
505 static const struct iio_chan_spec ad5770r_channels
[] = {
506 AD5770R_IDAC_CHANNEL(0, AD5770R_DAC_MSB(0)),
507 AD5770R_IDAC_CHANNEL(1, AD5770R_DAC_MSB(1)),
508 AD5770R_IDAC_CHANNEL(2, AD5770R_DAC_MSB(2)),
509 AD5770R_IDAC_CHANNEL(3, AD5770R_DAC_MSB(3)),
510 AD5770R_IDAC_CHANNEL(4, AD5770R_DAC_MSB(4)),
511 AD5770R_IDAC_CHANNEL(5, AD5770R_DAC_MSB(5)),
514 static int ad5770r_channel_config(struct ad5770r_state
*st
)
516 int ret
, tmp
[2], min
, max
;
518 struct fwnode_handle
*child
;
520 num
= device_get_child_node_count(&st
->spi
->dev
);
521 if (num
!= AD5770R_MAX_CHANNELS
)
524 device_for_each_child_node(&st
->spi
->dev
, child
) {
525 ret
= fwnode_property_read_u32(child
, "num", &num
);
528 if (num
> AD5770R_MAX_CHANNELS
)
531 ret
= fwnode_property_read_u32_array(child
,
532 "adi,range-microamp",
539 ret
= ad5770r_store_output_range(st
, min
, max
, num
);
547 static int ad5770r_init(struct ad5770r_state
*st
)
551 st
->gpio_reset
= devm_gpiod_get_optional(&st
->spi
->dev
, "reset",
553 if (IS_ERR(st
->gpio_reset
))
554 return PTR_ERR(st
->gpio_reset
);
556 /* Perform a reset */
557 ret
= ad5770r_reset(st
);
561 /* Set output range */
562 ret
= ad5770r_channel_config(st
);
566 for (i
= 0; i
< AD5770R_MAX_CHANNELS
; i
++) {
567 ret
= ad5770r_set_output_mode(st
, &st
->output_mode
[i
], i
);
572 st
->external_res
= fwnode_property_read_bool(st
->spi
->dev
.fwnode
,
573 "adi,external-resistor");
575 ret
= ad5770r_set_reference(st
);
579 /* Set outputs off */
580 ret
= regmap_write(st
->regmap
, AD5770R_CHANNEL_CONFIG
, 0x00);
584 ret
= regmap_write(st
->regmap
, AD5770R_CH_ENABLE
, 0x00);
588 for (i
= 0; i
< AD5770R_MAX_CHANNELS
; i
++)
589 st
->ch_pwr_down
[i
] = true;
594 static void ad5770r_disable_regulator(void *data
)
596 struct ad5770r_state
*st
= data
;
598 regulator_disable(st
->vref_reg
);
601 static int ad5770r_probe(struct spi_device
*spi
)
603 struct ad5770r_state
*st
;
604 struct iio_dev
*indio_dev
;
605 struct regmap
*regmap
;
608 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
612 st
= iio_priv(indio_dev
);
613 spi_set_drvdata(spi
, indio_dev
);
617 regmap
= devm_regmap_init_spi(spi
, &ad5770r_spi_regmap_config
);
618 if (IS_ERR(regmap
)) {
619 dev_err(&spi
->dev
, "Error initializing spi regmap: %ld\n",
621 return PTR_ERR(regmap
);
625 st
->vref_reg
= devm_regulator_get_optional(&spi
->dev
, "vref");
626 if (!IS_ERR(st
->vref_reg
)) {
627 ret
= regulator_enable(st
->vref_reg
);
630 "Failed to enable vref regulators: %d\n", ret
);
634 ret
= devm_add_action_or_reset(&spi
->dev
,
635 ad5770r_disable_regulator
,
640 ret
= regulator_get_voltage(st
->vref_reg
);
644 st
->vref
= ret
/ 1000;
646 if (PTR_ERR(st
->vref_reg
) == -ENODEV
) {
647 st
->vref
= AD5770R_LOW_VREF_mV
;
648 st
->internal_ref
= true;
650 return PTR_ERR(st
->vref_reg
);
654 indio_dev
->dev
.parent
= &spi
->dev
;
655 indio_dev
->name
= spi_get_device_id(spi
)->name
;
656 indio_dev
->info
= &ad5770r_info
;
657 indio_dev
->modes
= INDIO_DIRECT_MODE
;
658 indio_dev
->channels
= ad5770r_channels
;
659 indio_dev
->num_channels
= ARRAY_SIZE(ad5770r_channels
);
661 ret
= ad5770r_init(st
);
663 dev_err(&spi
->dev
, "AD5770R init failed\n");
667 return devm_iio_device_register(&st
->spi
->dev
, indio_dev
);
670 static const struct of_device_id ad5770r_of_id
[] = {
671 { .compatible
= "adi,ad5770r", },
674 MODULE_DEVICE_TABLE(of
, ad5770r_of_id
);
676 static const struct spi_device_id ad5770r_id
[] = {
680 MODULE_DEVICE_TABLE(spi
, ad5770r_id
);
682 static struct spi_driver ad5770r_driver
= {
684 .name
= KBUILD_MODNAME
,
685 .of_match_table
= ad5770r_of_id
,
687 .probe
= ad5770r_probe
,
688 .id_table
= ad5770r_id
,
691 module_spi_driver(ad5770r_driver
);
693 MODULE_AUTHOR("Mircea Caprioru <mircea.caprioru@analog.com>");
694 MODULE_DESCRIPTION("Analog Devices AD5770R IDAC");
695 MODULE_LICENSE("GPL v2");