9 #include <linux/device.h>
12 #include <linux/module.h>
14 #include <linux/kernel.h>
16 #include <linux/slab.h>
25 #define AD5421_REG_DAC_DATA 0x1
26 #define AD5421_REG_CTRL 0x2
27 #define AD5421_REG_OFFSET 0x3
28 #define AD5421_REG_GAIN 0x4
31 #define AD5421_REG_LOAD_DAC 0x5
32 #define AD5421_REG_FAULT 0x5
33 #define AD5421_REG_FORCE_ALARM_CURRENT 0x6
34 #define AD5421_REG_RESET 0x7
35 #define AD5421_REG_START_CONVERSION 0x8
36 #define AD5421_REG_NOOP 0x9
38 #define AD5421_CTRL_WATCHDOG_DISABLE BIT(12)
39 #define AD5421_CTRL_AUTO_FAULT_READBACK BIT(11)
40 #define AD5421_CTRL_MIN_CURRENT BIT(9)
41 #define AD5421_CTRL_ADC_SOURCE_TEMP BIT(8)
42 #define AD5421_CTRL_ADC_ENABLE BIT(7)
43 #define AD5421_CTRL_PWR_DOWN_INT_VREF BIT(6)
45 #define AD5421_FAULT_SPI BIT(15)
46 #define AD5421_FAULT_PEC BIT(14)
47 #define AD5421_FAULT_OVER_CURRENT BIT(13)
48 #define AD5421_FAULT_UNDER_CURRENT BIT(12)
49 #define AD5421_FAULT_TEMP_OVER_140 BIT(11)
50 #define AD5421_FAULT_TEMP_OVER_100 BIT(10)
51 #define AD5421_FAULT_UNDER_VOLTAGE_6V BIT(9)
52 #define AD5421_FAULT_UNDER_VOLTAGE_12V BIT(8)
55 #define AD5421_FAULT_TRIGGER_IRQ \
56 (AD5421_FAULT_SPI | AD5421_FAULT_PEC | AD5421_FAULT_OVER_CURRENT | \
57 AD5421_FAULT_UNDER_CURRENT | AD5421_FAULT_TEMP_OVER_140)
94 .scan_type =
IIO_ST(
'u', 16, 16, 0),
105 static int ad5421_write_unlocked(
struct iio_dev *indio_dev,
106 unsigned int reg,
unsigned int val)
112 return spi_write(st->
spi, &st->data[0].
d8[1], 3);
115 static int ad5421_write(
struct iio_dev *indio_dev,
unsigned int reg,
121 ret = ad5421_write_unlocked(indio_dev, reg, val);
127 static int ad5421_read(
struct iio_dev *indio_dev,
unsigned int reg)
134 .tx_buf = &st->data[0].
d8[1],
138 .rx_buf = &st->data[1].
d8[1],
143 spi_message_init(&
m);
144 spi_message_add_tail(&t[0], &
m);
145 spi_message_add_tail(&t[1], &
m);
160 static int ad5421_update_ctrl(
struct iio_dev *indio_dev,
unsigned int set,
183 unsigned int old_fault = 0;
195 ad5421_update_ctrl(indio_dev, 0, 0);
213 events = (old_fault ^ fault) & fault;
256 static void ad5421_get_current_min_max(
struct ad5421_state *st,
257 unsigned int *
min,
unsigned int *
max)
281 static inline unsigned int ad5421_get_offset(
struct ad5421_state *st)
285 ad5421_get_current_min_max(st, &min, &max);
286 return (min * (1 << 16)) / (max -
min);
289 static inline unsigned int ad5421_get_scale(
struct ad5421_state *st)
293 ad5421_get_current_min_max(st, &min, &max);
294 return ((max - min) * 1000) / (1 << 16);
297 static int ad5421_read_raw(
struct iio_dev *indio_dev,
315 *val2 = ad5421_get_scale(st);
318 *val = ad5421_get_offset(st);
337 static int ad5421_write_raw(
struct iio_dev *indio_dev,
340 const unsigned int max_val = 1 << 16;
344 if (val >= max_val || val < 0)
350 if (val >= max_val || val < 0)
355 if (val >= max_val || val < 0)
366 static int ad5421_write_event_config(
struct iio_dev *indio_dev,
397 static int ad5421_read_event_config(
struct iio_dev *indio_dev,
421 static int ad5421_read_event_value(
struct iio_dev *indio_dev,
u64 event_code,
443 static const struct iio_info ad5421_info = {
444 .read_raw = ad5421_read_raw,
445 .write_raw = ad5421_write_raw,
446 .read_event_config = ad5421_read_event_config,
447 .write_event_config = ad5421_write_event_config,
448 .read_event_value = ad5421_read_event_value,
460 if (indio_dev ==
NULL) {
461 dev_err(&spi->
dev,
"Failed to allocate iio device\n");
465 st = iio_priv(indio_dev);
466 spi_set_drvdata(spi, indio_dev);
470 indio_dev->
dev.parent = &spi->
dev;
471 indio_dev->
name =
"ad5421";
472 indio_dev->
info = &ad5421_info;
474 indio_dev->
channels = ad5421_channels;
489 ad5421_update_ctrl(indio_dev, 0, 0);
494 ad5421_fault_handler,
504 dev_err(&spi->
dev,
"Failed to register iio device: %d\n", ret);
521 struct iio_dev *indio_dev = spi_get_drvdata(spi);
536 .probe = ad5421_probe,