1 // SPDX-License-Identifier: GPL-2.0
3 * AD8460 Waveform generator DAC Driver
5 * Copyright (C) 2024 Analog Devices, Inc.
8 #include <linux/bitfield.h>
9 #include <linux/cleanup.h>
10 #include <linux/clk.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/dmaengine.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/spi/spi.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/buffer-dma.h>
24 #include <linux/iio/buffer-dmaengine.h>
25 #include <linux/iio/consumer.h>
26 #include <linux/iio/events.h>
27 #include <linux/iio/iio.h>
29 #define AD8460_CTRL_REG(x) (x)
30 #define AD8460_HVDAC_DATA_WORD(x) (0x60 + (2 * (x)))
32 #define AD8460_HV_RESET_MSK BIT(7)
33 #define AD8460_HV_SLEEP_MSK BIT(4)
34 #define AD8460_WAVE_GEN_MODE_MSK BIT(0)
36 #define AD8460_HVDAC_SLEEP_MSK BIT(3)
38 #define AD8460_FAULT_ARM_MSK BIT(7)
39 #define AD8460_FAULT_LIMIT_MSK GENMASK(6, 0)
41 #define AD8460_APG_MODE_ENABLE_MSK BIT(5)
42 #define AD8460_PATTERN_DEPTH_MSK GENMASK(3, 0)
44 #define AD8460_QUIESCENT_CURRENT_MSK GENMASK(7, 0)
46 #define AD8460_SHUTDOWN_FLAG_MSK BIT(7)
48 #define AD8460_DATA_BYTE_LOW_MSK GENMASK(7, 0)
49 #define AD8460_DATA_BYTE_HIGH_MSK GENMASK(5, 0)
50 #define AD8460_DATA_BYTE_FULL_MSK GENMASK(13, 0)
52 #define AD8460_DEFAULT_FAULT_PROTECT 0x00
53 #define AD8460_DATA_BYTE_WORD_LENGTH 2
54 #define AD8460_NUM_DATA_WORDS 16
55 #define AD8460_NOMINAL_VOLTAGE_SPAN 80
56 #define AD8460_MIN_EXT_RESISTOR_OHMS 2000
57 #define AD8460_MAX_EXT_RESISTOR_OHMS 20000
58 #define AD8460_MIN_VREFIO_UV 120000
59 #define AD8460_MAX_VREFIO_UV 1200000
60 #define AD8460_ABS_MAX_OVERVOLTAGE_UV 55000000
61 #define AD8460_ABS_MAX_OVERCURRENT_UA 1000000
62 #define AD8460_MAX_OVERTEMPERATURE_MC 150000
63 #define AD8460_MIN_OVERTEMPERATURE_MC 20000
64 #define AD8460_CURRENT_LIMIT_CONV(x) ((x) / 15625)
65 #define AD8460_VOLTAGE_LIMIT_CONV(x) ((x) / 1953000)
66 #define AD8460_TEMP_LIMIT_CONV(x) (((x) + 266640) / 6510)
68 enum ad8460_fault_type
{
69 AD8460_OVERCURRENT_SRC
,
70 AD8460_OVERCURRENT_SNK
,
71 AD8460_OVERVOLTAGE_POS
,
72 AD8460_OVERVOLTAGE_NEG
,
73 AD8460_OVERTEMPERATURE
,
77 struct spi_device
*spi
;
78 struct regmap
*regmap
;
79 struct iio_channel
*tmp_adc_channel
;
81 /* lock to protect against multiple access to the device and shared data */
84 u32 ext_resistor_ohms
;
86 * DMA (thus cache coherency maintenance) requires the
87 * transfer buffers to live in their own cache lines.
89 __le16 spi_tx_buf
__aligned(IIO_DMA_MINALIGN
);
92 static int ad8460_hv_reset(struct ad8460_state
*state
)
96 ret
= regmap_set_bits(state
->regmap
, AD8460_CTRL_REG(0x00),
103 return regmap_clear_bits(state
->regmap
, AD8460_CTRL_REG(0x00),
104 AD8460_HV_RESET_MSK
);
107 static int ad8460_reset(const struct ad8460_state
*state
)
109 struct device
*dev
= &state
->spi
->dev
;
110 struct gpio_desc
*reset
;
112 reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
114 return dev_err_probe(dev
, PTR_ERR(reset
),
115 "Failed to get reset gpio");
117 /* minimum duration of 10ns */
119 gpiod_set_value_cansleep(reset
, 1);
123 /* bring all registers to their default state */
124 return regmap_write(state
->regmap
, AD8460_CTRL_REG(0x03), 1);
127 static int ad8460_enable_apg_mode(struct ad8460_state
*state
, int val
)
131 ret
= regmap_update_bits(state
->regmap
, AD8460_CTRL_REG(0x02),
132 AD8460_APG_MODE_ENABLE_MSK
,
133 FIELD_PREP(AD8460_APG_MODE_ENABLE_MSK
, val
));
137 return regmap_update_bits(state
->regmap
, AD8460_CTRL_REG(0x00),
138 AD8460_WAVE_GEN_MODE_MSK
,
139 FIELD_PREP(AD8460_WAVE_GEN_MODE_MSK
, val
));
142 static int ad8460_read_shutdown_flag(struct ad8460_state
*state
, u64
*flag
)
146 ret
= regmap_read(state
->regmap
, AD8460_CTRL_REG(0x0E), &val
);
150 *flag
= FIELD_GET(AD8460_SHUTDOWN_FLAG_MSK
, val
);
154 static int ad8460_get_hvdac_word(struct ad8460_state
*state
, int index
, int *val
)
158 ret
= regmap_bulk_read(state
->regmap
, AD8460_HVDAC_DATA_WORD(index
),
159 &state
->spi_tx_buf
, AD8460_DATA_BYTE_WORD_LENGTH
);
163 *val
= le16_to_cpu(state
->spi_tx_buf
);
168 static int ad8460_set_hvdac_word(struct ad8460_state
*state
, int index
, int val
)
170 state
->spi_tx_buf
= cpu_to_le16(FIELD_PREP(AD8460_DATA_BYTE_FULL_MSK
, val
));
172 return regmap_bulk_write(state
->regmap
, AD8460_HVDAC_DATA_WORD(index
),
173 &state
->spi_tx_buf
, AD8460_DATA_BYTE_WORD_LENGTH
);
176 static ssize_t
ad8460_dac_input_read(struct iio_dev
*indio_dev
, uintptr_t private,
177 const struct iio_chan_spec
*chan
, char *buf
)
179 struct ad8460_state
*state
= iio_priv(indio_dev
);
183 ret
= ad8460_get_hvdac_word(state
, private, ®
);
187 return sysfs_emit(buf
, "%u\n", reg
);
190 static ssize_t
ad8460_dac_input_write(struct iio_dev
*indio_dev
, uintptr_t private,
191 const struct iio_chan_spec
*chan
,
192 const char *buf
, size_t len
)
194 struct ad8460_state
*state
= iio_priv(indio_dev
);
198 ret
= kstrtou32(buf
, 10, ®
);
202 guard(mutex
)(&state
->lock
);
204 return ad8460_set_hvdac_word(state
, private, reg
);
207 static ssize_t
ad8460_read_symbol(struct iio_dev
*indio_dev
, uintptr_t private,
208 const struct iio_chan_spec
*chan
, char *buf
)
210 struct ad8460_state
*state
= iio_priv(indio_dev
);
214 ret
= regmap_read(state
->regmap
, AD8460_CTRL_REG(0x02), ®
);
218 return sysfs_emit(buf
, "%lu\n", FIELD_GET(AD8460_PATTERN_DEPTH_MSK
, reg
));
221 static ssize_t
ad8460_write_symbol(struct iio_dev
*indio_dev
, uintptr_t private,
222 const struct iio_chan_spec
*chan
,
223 const char *buf
, size_t len
)
225 struct ad8460_state
*state
= iio_priv(indio_dev
);
229 ret
= kstrtou16(buf
, 10, &sym
);
233 guard(mutex
)(&state
->lock
);
235 return regmap_update_bits(state
->regmap
,
236 AD8460_CTRL_REG(0x02),
237 AD8460_PATTERN_DEPTH_MSK
,
238 FIELD_PREP(AD8460_PATTERN_DEPTH_MSK
, sym
));
241 static ssize_t
ad8460_read_toggle_en(struct iio_dev
*indio_dev
, uintptr_t private,
242 const struct iio_chan_spec
*chan
, char *buf
)
244 struct ad8460_state
*state
= iio_priv(indio_dev
);
248 ret
= regmap_read(state
->regmap
, AD8460_CTRL_REG(0x02), ®
);
252 return sysfs_emit(buf
, "%ld\n", FIELD_GET(AD8460_APG_MODE_ENABLE_MSK
, reg
));
255 static ssize_t
ad8460_write_toggle_en(struct iio_dev
*indio_dev
, uintptr_t private,
256 const struct iio_chan_spec
*chan
,
257 const char *buf
, size_t len
)
259 struct ad8460_state
*state
= iio_priv(indio_dev
);
263 ret
= kstrtobool(buf
, &toggle_en
);
267 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
)
268 return ad8460_enable_apg_mode(state
, toggle_en
);
272 static ssize_t
ad8460_read_powerdown(struct iio_dev
*indio_dev
, uintptr_t private,
273 const struct iio_chan_spec
*chan
, char *buf
)
275 struct ad8460_state
*state
= iio_priv(indio_dev
);
279 ret
= regmap_read(state
->regmap
, AD8460_CTRL_REG(0x01), ®
);
283 return sysfs_emit(buf
, "%ld\n", FIELD_GET(AD8460_HVDAC_SLEEP_MSK
, reg
));
286 static ssize_t
ad8460_write_powerdown(struct iio_dev
*indio_dev
, uintptr_t private,
287 const struct iio_chan_spec
*chan
,
288 const char *buf
, size_t len
)
290 struct ad8460_state
*state
= iio_priv(indio_dev
);
295 ret
= kstrtobool(buf
, &pwr_down
);
299 guard(mutex
)(&state
->lock
);
302 * If powerdown is set, HVDAC is enabled and the HV driver is
303 * enabled via HV_RESET in case it is in shutdown mode,
304 * If powerdown is cleared, HVDAC is set to shutdown state
305 * as well as the HV driver. Quiescent current decreases and ouput is
306 * floating (high impedance).
309 ret
= regmap_update_bits(state
->regmap
, AD8460_CTRL_REG(0x01),
310 AD8460_HVDAC_SLEEP_MSK
,
311 FIELD_PREP(AD8460_HVDAC_SLEEP_MSK
, pwr_down
));
316 ret
= ad8460_read_shutdown_flag(state
, &sdn_flag
);
321 ret
= ad8460_hv_reset(state
);
327 ret
= regmap_update_bits(state
->regmap
, AD8460_CTRL_REG(0x00),
329 FIELD_PREP(AD8460_HV_SLEEP_MSK
, !pwr_down
));
336 static const char * const ad8460_powerdown_modes
[] = {
340 static int ad8460_get_powerdown_mode(struct iio_dev
*indio_dev
,
341 const struct iio_chan_spec
*chan
)
346 static int ad8460_set_powerdown_mode(struct iio_dev
*indio_dev
,
347 const struct iio_chan_spec
*chan
,
353 static int ad8460_set_sample(struct ad8460_state
*state
, int val
)
357 ret
= ad8460_enable_apg_mode(state
, 1);
361 guard(mutex
)(&state
->lock
);
362 ret
= ad8460_set_hvdac_word(state
, 0, val
);
366 return regmap_update_bits(state
->regmap
, AD8460_CTRL_REG(0x02),
367 AD8460_PATTERN_DEPTH_MSK
,
368 FIELD_PREP(AD8460_PATTERN_DEPTH_MSK
, 0));
371 static int ad8460_set_fault_threshold(struct ad8460_state
*state
,
372 enum ad8460_fault_type fault
,
373 unsigned int threshold
)
375 return regmap_update_bits(state
->regmap
, AD8460_CTRL_REG(0x08 + fault
),
376 AD8460_FAULT_LIMIT_MSK
,
377 FIELD_PREP(AD8460_FAULT_LIMIT_MSK
, threshold
));
380 static int ad8460_get_fault_threshold(struct ad8460_state
*state
,
381 enum ad8460_fault_type fault
,
382 unsigned int *threshold
)
387 ret
= regmap_read(state
->regmap
, AD8460_CTRL_REG(0x08 + fault
), &val
);
391 *threshold
= FIELD_GET(AD8460_FAULT_LIMIT_MSK
, val
);
396 static int ad8460_set_fault_threshold_en(struct ad8460_state
*state
,
397 enum ad8460_fault_type fault
, bool en
)
399 return regmap_update_bits(state
->regmap
, AD8460_CTRL_REG(0x08 + fault
),
400 AD8460_FAULT_ARM_MSK
,
401 FIELD_PREP(AD8460_FAULT_ARM_MSK
, en
));
404 static int ad8460_get_fault_threshold_en(struct ad8460_state
*state
,
405 enum ad8460_fault_type fault
, bool *en
)
410 ret
= regmap_read(state
->regmap
, AD8460_CTRL_REG(0x08 + fault
), &val
);
414 *en
= FIELD_GET(AD8460_FAULT_ARM_MSK
, val
);
419 static int ad8460_write_raw(struct iio_dev
*indio_dev
,
420 struct iio_chan_spec
const *chan
, int val
, int val2
,
423 struct ad8460_state
*state
= iio_priv(indio_dev
);
426 case IIO_CHAN_INFO_RAW
:
427 switch (chan
->type
) {
429 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
)
430 return ad8460_set_sample(state
, val
);
433 return regmap_write(state
->regmap
, AD8460_CTRL_REG(0x04),
434 FIELD_PREP(AD8460_QUIESCENT_CURRENT_MSK
, val
));
443 static int ad8460_read_raw(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
444 int *val
, int *val2
, long mask
)
446 struct ad8460_state
*state
= iio_priv(indio_dev
);
450 case IIO_CHAN_INFO_RAW
:
451 switch (chan
->type
) {
453 scoped_guard(mutex
, &state
->lock
) {
454 ret
= ad8460_get_hvdac_word(state
, 0, &data
);
461 ret
= regmap_read(state
->regmap
, AD8460_CTRL_REG(0x04),
468 ret
= iio_read_channel_raw(state
->tmp_adc_channel
, &data
);
476 case IIO_CHAN_INFO_SAMP_FREQ
:
477 *val
= clk_get_rate(state
->sync_clk
);
479 case IIO_CHAN_INFO_SCALE
:
481 * vCONV = vNOMINAL_SPAN * (DAC_CODE / 2**14) - 40V
482 * vMAX = vNOMINAL_SPAN * (2**14 / 2**14) - 40V
483 * vMIN = vNOMINAL_SPAN * (0 / 2**14) - 40V
484 * vADJ = vCONV * (2000 / rSET) * (vREF / 1.2)
485 * vSPAN = vADJ_MAX - vADJ_MIN
486 * See datasheet page 49, section FULL-SCALE REDUCTION
488 *val
= AD8460_NOMINAL_VOLTAGE_SPAN
* 2000 * state
->refio_1p2v_mv
;
489 *val2
= state
->ext_resistor_ohms
* 1200;
490 return IIO_VAL_FRACTIONAL
;
496 static int ad8460_select_fault_type(int chan_type
, enum iio_event_direction dir
)
501 case IIO_EV_DIR_RISING
:
502 return AD8460_OVERVOLTAGE_POS
;
503 case IIO_EV_DIR_FALLING
:
504 return AD8460_OVERVOLTAGE_NEG
;
510 case IIO_EV_DIR_RISING
:
511 return AD8460_OVERCURRENT_SRC
;
512 case IIO_EV_DIR_FALLING
:
513 return AD8460_OVERCURRENT_SNK
;
519 case IIO_EV_DIR_RISING
:
520 return AD8460_OVERTEMPERATURE
;
529 static int ad8460_write_event_value(struct iio_dev
*indio_dev
,
530 const struct iio_chan_spec
*chan
,
531 enum iio_event_type type
,
532 enum iio_event_direction dir
,
533 enum iio_event_info info
, int val
, int val2
)
535 struct ad8460_state
*state
= iio_priv(indio_dev
);
538 if (type
!= IIO_EV_TYPE_THRESH
)
541 if (info
!= IIO_EV_INFO_VALUE
)
544 fault
= ad8460_select_fault_type(chan
->type
, dir
);
548 return ad8460_set_fault_threshold(state
, fault
, val
);
551 static int ad8460_read_event_value(struct iio_dev
*indio_dev
,
552 const struct iio_chan_spec
*chan
,
553 enum iio_event_type type
,
554 enum iio_event_direction dir
,
555 enum iio_event_info info
, int *val
, int *val2
)
557 struct ad8460_state
*state
= iio_priv(indio_dev
);
560 if (type
!= IIO_EV_TYPE_THRESH
)
563 if (info
!= IIO_EV_INFO_VALUE
)
566 fault
= ad8460_select_fault_type(chan
->type
, dir
);
570 return ad8460_get_fault_threshold(state
, fault
, val
);
573 static int ad8460_write_event_config(struct iio_dev
*indio_dev
,
574 const struct iio_chan_spec
*chan
,
575 enum iio_event_type type
,
576 enum iio_event_direction dir
, bool val
)
578 struct ad8460_state
*state
= iio_priv(indio_dev
);
581 if (type
!= IIO_EV_TYPE_THRESH
)
584 fault
= ad8460_select_fault_type(chan
->type
, dir
);
588 return ad8460_set_fault_threshold_en(state
, fault
, val
);
591 static int ad8460_read_event_config(struct iio_dev
*indio_dev
,
592 const struct iio_chan_spec
*chan
,
593 enum iio_event_type type
,
594 enum iio_event_direction dir
)
596 struct ad8460_state
*state
= iio_priv(indio_dev
);
600 if (type
!= IIO_EV_TYPE_THRESH
)
603 fault
= ad8460_select_fault_type(chan
->type
, dir
);
607 ret
= ad8460_get_fault_threshold_en(state
, fault
, &en
);
614 static int ad8460_reg_access(struct iio_dev
*indio_dev
, unsigned int reg
,
615 unsigned int writeval
, unsigned int *readval
)
617 struct ad8460_state
*state
= iio_priv(indio_dev
);
620 return regmap_read(state
->regmap
, reg
, readval
);
622 return regmap_write(state
->regmap
, reg
, writeval
);
625 static int ad8460_buffer_preenable(struct iio_dev
*indio_dev
)
627 struct ad8460_state
*state
= iio_priv(indio_dev
);
629 return ad8460_enable_apg_mode(state
, 0);
632 static int ad8460_buffer_postdisable(struct iio_dev
*indio_dev
)
634 struct ad8460_state
*state
= iio_priv(indio_dev
);
636 return ad8460_enable_apg_mode(state
, 1);
639 static const struct iio_buffer_setup_ops ad8460_buffer_setup_ops
= {
640 .preenable
= &ad8460_buffer_preenable
,
641 .postdisable
= &ad8460_buffer_postdisable
,
644 static const struct iio_info ad8460_info
= {
645 .read_raw
= &ad8460_read_raw
,
646 .write_raw
= &ad8460_write_raw
,
647 .write_event_value
= &ad8460_write_event_value
,
648 .read_event_value
= &ad8460_read_event_value
,
649 .write_event_config
= &ad8460_write_event_config
,
650 .read_event_config
= &ad8460_read_event_config
,
651 .debugfs_reg_access
= &ad8460_reg_access
,
654 static const struct iio_enum ad8460_powerdown_mode_enum
= {
655 .items
= ad8460_powerdown_modes
,
656 .num_items
= ARRAY_SIZE(ad8460_powerdown_modes
),
657 .get
= ad8460_get_powerdown_mode
,
658 .set
= ad8460_set_powerdown_mode
,
661 #define AD8460_CHAN_EXT_INFO(_name, _what, _read, _write) { \
665 .private = (_what), \
666 .shared = IIO_SEPARATE, \
669 static const struct iio_chan_spec_ext_info ad8460_ext_info
[] = {
670 AD8460_CHAN_EXT_INFO("raw0", 0, ad8460_dac_input_read
,
671 ad8460_dac_input_write
),
672 AD8460_CHAN_EXT_INFO("raw1", 1, ad8460_dac_input_read
,
673 ad8460_dac_input_write
),
674 AD8460_CHAN_EXT_INFO("raw2", 2, ad8460_dac_input_read
,
675 ad8460_dac_input_write
),
676 AD8460_CHAN_EXT_INFO("raw3", 3, ad8460_dac_input_read
,
677 ad8460_dac_input_write
),
678 AD8460_CHAN_EXT_INFO("raw4", 4, ad8460_dac_input_read
,
679 ad8460_dac_input_write
),
680 AD8460_CHAN_EXT_INFO("raw5", 5, ad8460_dac_input_read
,
681 ad8460_dac_input_write
),
682 AD8460_CHAN_EXT_INFO("raw6", 6, ad8460_dac_input_read
,
683 ad8460_dac_input_write
),
684 AD8460_CHAN_EXT_INFO("raw7", 7, ad8460_dac_input_read
,
685 ad8460_dac_input_write
),
686 AD8460_CHAN_EXT_INFO("raw8", 8, ad8460_dac_input_read
,
687 ad8460_dac_input_write
),
688 AD8460_CHAN_EXT_INFO("raw9", 9, ad8460_dac_input_read
,
689 ad8460_dac_input_write
),
690 AD8460_CHAN_EXT_INFO("raw10", 10, ad8460_dac_input_read
,
691 ad8460_dac_input_write
),
692 AD8460_CHAN_EXT_INFO("raw11", 11, ad8460_dac_input_read
,
693 ad8460_dac_input_write
),
694 AD8460_CHAN_EXT_INFO("raw12", 12, ad8460_dac_input_read
,
695 ad8460_dac_input_write
),
696 AD8460_CHAN_EXT_INFO("raw13", 13, ad8460_dac_input_read
,
697 ad8460_dac_input_write
),
698 AD8460_CHAN_EXT_INFO("raw14", 14, ad8460_dac_input_read
,
699 ad8460_dac_input_write
),
700 AD8460_CHAN_EXT_INFO("raw15", 15, ad8460_dac_input_read
,
701 ad8460_dac_input_write
),
702 AD8460_CHAN_EXT_INFO("toggle_en", 0, ad8460_read_toggle_en
,
703 ad8460_write_toggle_en
),
704 AD8460_CHAN_EXT_INFO("symbol", 0, ad8460_read_symbol
,
705 ad8460_write_symbol
),
706 AD8460_CHAN_EXT_INFO("powerdown", 0, ad8460_read_powerdown
,
707 ad8460_write_powerdown
),
708 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, &ad8460_powerdown_mode_enum
),
709 IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE
,
710 &ad8460_powerdown_mode_enum
),
714 static const struct iio_event_spec ad8460_events
[] = {
716 .type
= IIO_EV_TYPE_THRESH
,
717 .dir
= IIO_EV_DIR_RISING
,
718 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
719 BIT(IIO_EV_INFO_ENABLE
),
722 .type
= IIO_EV_TYPE_THRESH
,
723 .dir
= IIO_EV_DIR_FALLING
,
724 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
725 BIT(IIO_EV_INFO_ENABLE
),
729 #define AD8460_VOLTAGE_CHAN { \
730 .type = IIO_VOLTAGE, \
731 .info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
732 BIT(IIO_CHAN_INFO_RAW) | \
733 BIT(IIO_CHAN_INFO_SCALE), \
742 .endianness = IIO_CPU, \
744 .ext_info = ad8460_ext_info, \
745 .event_spec = ad8460_events, \
746 .num_event_specs = ARRAY_SIZE(ad8460_events), \
749 #define AD8460_CURRENT_CHAN { \
750 .type = IIO_CURRENT, \
751 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
756 .event_spec = ad8460_events, \
757 .num_event_specs = ARRAY_SIZE(ad8460_events), \
760 #define AD8460_TEMP_CHAN { \
762 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
766 .event_spec = ad8460_events, \
767 .num_event_specs = 1, \
770 static const struct iio_chan_spec ad8460_channels
[] = {
775 static const struct iio_chan_spec ad8460_channels_with_tmp_adc
[] = {
781 static const struct regmap_config ad8460_regmap_config
= {
784 .max_register
= 0x7F,
787 static const char * const ad8460_supplies
[] = {
788 "avdd_3p3v", "dvdd_3p3v", "vcc_5v", "hvcc", "hvee", "vref_5v"
791 static int ad8460_probe(struct spi_device
*spi
)
793 struct device
*dev
= &spi
->dev
;
794 struct ad8460_state
*state
;
795 struct iio_dev
*indio_dev
;
799 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*state
));
803 state
= iio_priv(indio_dev
);
805 indio_dev
->name
= "ad8460";
806 indio_dev
->info
= &ad8460_info
;
810 state
->regmap
= devm_regmap_init_spi(spi
, &ad8460_regmap_config
);
811 if (IS_ERR(state
->regmap
))
812 return dev_err_probe(dev
, PTR_ERR(state
->regmap
),
813 "Failed to initialize regmap");
815 ret
= devm_mutex_init(dev
, &state
->lock
);
819 state
->sync_clk
= devm_clk_get_enabled(dev
, NULL
);
820 if (IS_ERR(state
->sync_clk
))
821 return dev_err_probe(dev
, PTR_ERR(state
->sync_clk
),
822 "Failed to get sync clk\n");
824 state
->tmp_adc_channel
= devm_iio_channel_get(dev
, "ad8460-tmp");
825 if (IS_ERR(state
->tmp_adc_channel
)) {
826 if (PTR_ERR(state
->tmp_adc_channel
) == -EPROBE_DEFER
)
827 return -EPROBE_DEFER
;
828 indio_dev
->channels
= ad8460_channels
;
829 indio_dev
->num_channels
= ARRAY_SIZE(ad8460_channels
);
831 indio_dev
->channels
= ad8460_channels_with_tmp_adc
;
832 indio_dev
->num_channels
= ARRAY_SIZE(ad8460_channels_with_tmp_adc
);
835 ret
= devm_regulator_bulk_get_enable(dev
, ARRAY_SIZE(ad8460_supplies
),
838 return dev_err_probe(dev
, ret
,
839 "Failed to enable power supplies\n");
841 ret
= devm_regulator_get_enable_read_voltage(dev
, "refio_1p2v");
842 if (ret
< 0 && ret
!= -ENODEV
)
843 return dev_err_probe(dev
, ret
, "Failed to get reference voltage\n");
845 state
->refio_1p2v_mv
= ret
== -ENODEV
? 1200 : ret
/ 1000;
847 if (!in_range(state
->refio_1p2v_mv
, AD8460_MIN_VREFIO_UV
/ 1000,
848 AD8460_MAX_VREFIO_UV
/ 1000))
849 return dev_err_probe(dev
, -EINVAL
,
850 "Invalid ref voltage range(%u mV) [%u mV, %u mV]\n",
851 state
->refio_1p2v_mv
,
852 AD8460_MIN_VREFIO_UV
/ 1000,
853 AD8460_MAX_VREFIO_UV
/ 1000);
855 ret
= device_property_read_u32(dev
, "adi,external-resistor-ohms",
856 &state
->ext_resistor_ohms
);
858 state
->ext_resistor_ohms
= 2000;
859 else if (!in_range(state
->ext_resistor_ohms
, AD8460_MIN_EXT_RESISTOR_OHMS
,
860 AD8460_MAX_EXT_RESISTOR_OHMS
))
861 return dev_err_probe(dev
, -EINVAL
,
862 "Invalid resistor set range(%u) [%u, %u]\n",
863 state
->ext_resistor_ohms
,
864 AD8460_MIN_EXT_RESISTOR_OHMS
,
865 AD8460_MAX_EXT_RESISTOR_OHMS
);
867 ret
= device_property_read_u32_array(dev
, "adi,range-microamp",
868 tmp
, ARRAY_SIZE(tmp
));
870 if (in_range(tmp
[1], 0, AD8460_ABS_MAX_OVERCURRENT_UA
))
871 regmap_write(state
->regmap
, AD8460_CTRL_REG(0x08),
872 FIELD_PREP(AD8460_FAULT_ARM_MSK
, 1) |
873 AD8460_CURRENT_LIMIT_CONV(tmp
[1]));
875 if (in_range(tmp
[0], -AD8460_ABS_MAX_OVERCURRENT_UA
, 0))
876 regmap_write(state
->regmap
, AD8460_CTRL_REG(0x09),
877 FIELD_PREP(AD8460_FAULT_ARM_MSK
, 1) |
878 AD8460_CURRENT_LIMIT_CONV(abs(tmp
[0])));
881 ret
= device_property_read_u32_array(dev
, "adi,range-microvolt",
882 tmp
, ARRAY_SIZE(tmp
));
884 if (in_range(tmp
[1], 0, AD8460_ABS_MAX_OVERVOLTAGE_UV
))
885 regmap_write(state
->regmap
, AD8460_CTRL_REG(0x0A),
886 FIELD_PREP(AD8460_FAULT_ARM_MSK
, 1) |
887 AD8460_VOLTAGE_LIMIT_CONV(tmp
[1]));
889 if (in_range(tmp
[0], -AD8460_ABS_MAX_OVERVOLTAGE_UV
, 0))
890 regmap_write(state
->regmap
, AD8460_CTRL_REG(0x0B),
891 FIELD_PREP(AD8460_FAULT_ARM_MSK
, 1) |
892 AD8460_VOLTAGE_LIMIT_CONV(abs(tmp
[0])));
895 ret
= device_property_read_u32(dev
, "adi,max-millicelsius", &temp
);
897 if (in_range(temp
, AD8460_MIN_OVERTEMPERATURE_MC
,
898 AD8460_MAX_OVERTEMPERATURE_MC
))
899 regmap_write(state
->regmap
, AD8460_CTRL_REG(0x0C),
900 FIELD_PREP(AD8460_FAULT_ARM_MSK
, 1) |
901 AD8460_TEMP_LIMIT_CONV(abs(temp
)));
904 ret
= ad8460_reset(state
);
908 /* Enables DAC by default */
909 ret
= regmap_clear_bits(state
->regmap
, AD8460_CTRL_REG(0x01),
910 AD8460_HVDAC_SLEEP_MSK
);
914 indio_dev
->modes
= INDIO_DIRECT_MODE
;
915 indio_dev
->setup_ops
= &ad8460_buffer_setup_ops
;
917 ret
= devm_iio_dmaengine_buffer_setup_ext(dev
, indio_dev
, "tx",
918 IIO_BUFFER_DIRECTION_OUT
);
920 return dev_err_probe(dev
, ret
,
921 "Failed to get DMA buffer\n");
923 return devm_iio_device_register(dev
, indio_dev
);
926 static const struct of_device_id ad8460_of_match
[] = {
927 { .compatible
= "adi,ad8460" },
930 MODULE_DEVICE_TABLE(of
, ad8460_of_match
);
932 static const struct spi_device_id ad8460_spi_match
[] = {
933 { .name
= "ad8460" },
936 MODULE_DEVICE_TABLE(spi
, ad8460_spi_match
);
938 static struct spi_driver ad8460_driver
= {
941 .of_match_table
= ad8460_of_match
,
943 .probe
= ad8460_probe
,
944 .id_table
= ad8460_spi_match
,
946 module_spi_driver(ad8460_driver
);
948 MODULE_AUTHOR("Mariel Tinaco <mariel.tinaco@analog.com");
949 MODULE_DESCRIPTION("AD8460 DAC driver");
950 MODULE_LICENSE("GPL");
951 MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER");