12 #include <linux/device.h>
13 #include <linux/kernel.h>
15 #include <linux/slab.h>
18 #include <linux/module.h>
24 #define AD5791_RES_MASK(x) ((1 << (x)) - 1)
25 #define AD5791_DAC_MASK AD5791_RES_MASK(20)
26 #define AD5791_DAC_MSB (1 << 19)
28 #define AD5791_CMD_READ (1 << 23)
29 #define AD5791_CMD_WRITE (0 << 23)
30 #define AD5791_ADDR(addr) ((addr) << 20)
33 #define AD5791_ADDR_NOOP 0
34 #define AD5791_ADDR_DAC0 1
35 #define AD5791_ADDR_CTRL 2
36 #define AD5791_ADDR_CLRCODE 3
37 #define AD5791_ADDR_SW_CTRL 4
40 #define AD5791_CTRL_RBUF (1 << 1)
41 #define AD5791_CTRL_OPGND (1 << 2)
42 #define AD5791_CTRL_DACTRI (1 << 3)
43 #define AD5791_CTRL_BIN2SC (1 << 4)
44 #define AD5791_CTRL_SDODIS (1 << 5)
45 #define AD5761_CTRL_LINCOMP(x) ((x) << 6)
47 #define AD5791_LINCOMP_0_10 0
48 #define AD5791_LINCOMP_10_12 1
49 #define AD5791_LINCOMP_12_16 2
50 #define AD5791_LINCOMP_16_19 3
51 #define AD5791_LINCOMP_19_20 12
53 #define AD5780_LINCOMP_0_10 0
54 #define AD5780_LINCOMP_10_20 12
57 #define AD5791_SWCTRL_LDAC (1 << 0)
58 #define AD5791_SWCTRL_CLR (1 << 1)
59 #define AD5791_SWCTRL_RESET (1 << 2)
61 #define AD5791_DAC_PWRDN_6K 0
62 #define AD5791_DAC_PWRDN_3STATE 1
118 return spi_write(spi, &
data.d8[1], 3);
136 .tx_buf = &
data[1].d8[1],
137 .rx_buf = &
data[2].d8[1],
147 spi_message_init(&
msg);
148 spi_message_add_tail(&xfers[0], &
msg);
149 spi_message_add_tail(&xfers[1], &
msg);
157 static const char *
const ad5791_powerdown_modes[] = {
162 static int ad5791_get_powerdown_mode(
struct iio_dev *indio_dev,
170 static int ad5791_set_powerdown_mode(
struct iio_dev *indio_dev,
180 static const struct iio_enum ad5791_powerdown_mode_enum = {
181 .items = ad5791_powerdown_modes,
182 .num_items =
ARRAY_SIZE(ad5791_powerdown_modes),
183 .get = ad5791_get_powerdown_mode,
184 .set = ad5791_set_powerdown_mode,
187 static ssize_t ad5791_read_dac_powerdown(
struct iio_dev *indio_dev,
195 static ssize_t ad5791_write_dac_powerdown(
struct iio_dev *indio_dev,
219 return ret ? ret : len;
222 static int ad5791_get_lin_comp(
unsigned int span)
226 else if (span <= 12000)
228 else if (span <= 16000)
230 else if (span <= 19000)
236 static int ad5780_get_lin_comp(
unsigned int span)
245 .get_lin_comp = ad5780_get_lin_comp,
248 .get_lin_comp = ad5780_get_lin_comp,
251 .get_lin_comp = ad5791_get_lin_comp,
254 .get_lin_comp = ad5791_get_lin_comp,
258 static int ad5791_read_raw(
struct iio_dev *indio_dev,
270 ret = ad5791_spi_read(st->
spi, chan->
address, val);
295 .read = ad5791_read_dac_powerdown,
296 .write = ad5791_write_dac_powerdown,
298 IIO_ENUM(
"powerdown_mode",
true, &ad5791_powerdown_mode_enum),
303 #define AD5791_CHAN(bits, shift) { \
304 .type = IIO_VOLTAGE, \
307 .address = AD5791_ADDR_DAC0, \
309 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
310 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
311 IIO_CHAN_INFO_OFFSET_SHARED_BIT, \
312 .scan_type = IIO_ST('u', bits, 24, shift), \
313 .ext_info = ad5791_ext_info, \
323 static int ad5791_write_raw(
struct iio_dev *indio_dev,
336 return ad5791_spi_write(st->
spi, chan->
address, val);
343 static const struct iio_info ad5791_info = {
344 .read_raw = &ad5791_read_raw,
345 .write_raw = &ad5791_write_raw,
354 int ret, pos_voltage_uv = 0, neg_voltage_uv = 0;
357 if (indio_dev ==
NULL) {
361 st = iio_priv(indio_dev);
366 goto error_put_reg_pos;
375 goto error_put_reg_neg;
384 st->
vref_mv = (pos_voltage_uv + neg_voltage_uv) / 1000;
390 dev_warn(&spi->
dev,
"reference voltage unspecified\n");
395 goto error_disable_reg_neg;
408 goto error_disable_reg_neg;
410 spi_set_drvdata(spi, indio_dev);
411 indio_dev->
dev.parent = &spi->
dev;
412 indio_dev->
info = &ad5791_info;
420 goto error_disable_reg_neg;
424 error_disable_reg_neg:
444 struct iio_dev *indio_dev = spi_get_drvdata(spi);
477 .probe = ad5791_probe,
479 .id_table = ad5791_id,