9 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
14 #include <linux/slab.h>
21 #define AD5755_NUM_CHANNELS 4
23 #define AD5755_ADDR(x) ((x) << 16)
25 #define AD5755_WRITE_REG_DATA(chan) (chan)
26 #define AD5755_WRITE_REG_GAIN(chan) (0x08 | (chan))
27 #define AD5755_WRITE_REG_OFFSET(chan) (0x10 | (chan))
28 #define AD5755_WRITE_REG_CTRL(chan) (0x1c | (chan))
30 #define AD5755_READ_REG_DATA(chan) (chan)
31 #define AD5755_READ_REG_CTRL(chan) (0x4 | (chan))
32 #define AD5755_READ_REG_GAIN(chan) (0x8 | (chan))
33 #define AD5755_READ_REG_OFFSET(chan) (0xc | (chan))
34 #define AD5755_READ_REG_CLEAR(chan) (0x10 | (chan))
35 #define AD5755_READ_REG_SLEW(chan) (0x14 | (chan))
36 #define AD5755_READ_REG_STATUS 0x18
37 #define AD5755_READ_REG_MAIN 0x19
38 #define AD5755_READ_REG_DC_DC 0x1a
40 #define AD5755_CTRL_REG_SLEW 0x0
41 #define AD5755_CTRL_REG_MAIN 0x1
42 #define AD5755_CTRL_REG_DAC 0x2
43 #define AD5755_CTRL_REG_DC_DC 0x3
44 #define AD5755_CTRL_REG_SW 0x4
46 #define AD5755_READ_FLAG 0x800000
48 #define AD5755_NOOP 0x1CE000
50 #define AD5755_DAC_INT_EN BIT(8)
51 #define AD5755_DAC_CLR_EN BIT(7)
52 #define AD5755_DAC_OUT_EN BIT(6)
53 #define AD5755_DAC_INT_CURRENT_SENSE_RESISTOR BIT(5)
54 #define AD5755_DAC_DC_DC_EN BIT(4)
55 #define AD5755_DAC_VOLTAGE_OVERRANGE_EN BIT(3)
57 #define AD5755_DC_DC_MAXV 0
58 #define AD5755_DC_DC_FREQ_SHIFT 2
59 #define AD5755_DC_DC_PHASE_SHIFT 4
60 #define AD5755_EXT_DC_DC_COMP_RES BIT(6)
62 #define AD5755_SLEW_STEP_SIZE_SHIFT 0
63 #define AD5755_SLEW_RATE_SHIFT 3
64 #define AD5755_SLEW_ENABLE BIT(12)
112 static int ad5755_write_unlocked(
struct iio_dev *indio_dev,
113 unsigned int reg,
unsigned int val)
119 return spi_write(st->
spi, &st->data[0].
d8[1], 3);
122 static int ad5755_write_ctrl_unlocked(
struct iio_dev *indio_dev,
125 return ad5755_write_unlocked(indio_dev,
129 static int ad5755_write(
struct iio_dev *indio_dev,
unsigned int reg,
135 ret = ad5755_write_unlocked(indio_dev, reg, val);
141 static int ad5755_write_ctrl(
struct iio_dev *indio_dev,
unsigned int channel,
142 unsigned int reg,
unsigned int val)
147 ret = ad5755_write_ctrl_unlocked(indio_dev, channel, reg, val);
153 static int ad5755_read(
struct iio_dev *indio_dev,
unsigned int addr)
160 .tx_buf = &st->data[0].
d8[1],
164 .tx_buf = &st->data[1].
d8[1],
165 .rx_buf = &st->data[1].
d8[1],
170 spi_message_init(&
m);
171 spi_message_add_tail(&t[0], &
m);
172 spi_message_add_tail(&t[1], &
m);
188 static int ad5755_update_dac_ctrl(
struct iio_dev *indio_dev,
189 unsigned int channel,
unsigned int set,
unsigned int clr)
197 ret = ad5755_write_ctrl_unlocked(indio_dev, channel,
203 static int ad5755_set_channel_pwr_down(
struct iio_dev *indio_dev,
204 unsigned int channel,
bool pwr_down)
207 unsigned int mask =
BIT(channel);
211 if ((
bool)(st->
pwr_down & mask) == pwr_down)
216 ad5755_update_dac_ctrl(indio_dev, channel,
219 ad5755_update_dac_ctrl(indio_dev, channel,
223 ad5755_update_dac_ctrl(indio_dev, channel,
234 static const int ad5755_min_max_table[][2] = {
248 *min = ad5755_min_max_table[
mode][0];
249 *max = ad5755_min_max_table[
mode][1];
252 static inline int ad5755_get_offset(
struct ad5755_state *st,
257 ad5755_get_min_max(st, chan, &min, &max);
258 return (min * (1 << chan->
scan_type.realbits)) / (max -
min);
261 static inline int ad5755_get_scale(
struct ad5755_state *st,
266 ad5755_get_min_max(st, chan, &min, &max);
267 return ((max - min) * 1000000000ULL) >> chan->
scan_type.realbits;
270 static int ad5755_chan_reg_info(
struct ad5755_state *st,
272 unsigned int *reg,
unsigned int *shift,
unsigned int *
offset)
306 static int ad5755_read_raw(
struct iio_dev *indio_dev,
307 const struct iio_chan_spec *chan,
int *val,
int *val2,
long info)
316 *val2 = ad5755_get_scale(st, chan);
319 *val = ad5755_get_offset(st, chan);
322 ret = ad5755_chan_reg_info(st, chan, info,
false,
323 ®, &shift, &offset);
327 ret = ad5755_read(indio_dev, reg);
331 *val = (ret -
offset) >> shift;
339 static int ad5755_write_raw(
struct iio_dev *indio_dev,
340 const struct iio_chan_spec *chan,
int val,
int val2,
long info)
346 ret = ad5755_chan_reg_info(st, chan, info,
true,
347 ®, &shift, &offset);
354 if (val < 0 || val > 0xffff)
357 return ad5755_write(indio_dev, reg, val);
370 struct iio_chan_spec const *chan,
const char *buf,
size_t len)
379 ret = ad5755_set_channel_pwr_down(indio_dev, chan->
channel, pwr_down);
380 return ret ? ret : len;
383 static const struct iio_info ad5755_info = {
384 .read_raw = ad5755_read_raw,
385 .write_raw = ad5755_write_raw,
392 .read = ad5755_read_powerdown,
393 .write = ad5755_write_powerdown,
398 #define AD5755_CHANNEL(_bits) { \
401 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
402 IIO_CHAN_INFO_SCALE_SEPARATE_BIT | \
403 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
404 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
405 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT, \
406 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)), \
407 .ext_info = ad5755_ext_info, \
413 .has_voltage_out =
true,
418 .has_voltage_out =
false,
423 .has_voltage_out =
true,
428 .has_voltage_out =
false,
474 val = pdata->
dac[
i].slew.step_size <<
476 val |= pdata->
dac[
i].slew.rate <<
478 if (pdata->
dac[i].slew.enable)
481 ret = ad5755_write_ctrl(indio_dev, i,
488 if (!ad5755_is_valid_mode(st, pdata->
dac[i].mode))
492 if (!pdata->
dac[i].ext_current_sense_resistor)
494 if (pdata->
dac[i].enable_voltage_overrange)
496 val |= pdata->
dac[
i].mode;
498 ret = ad5755_update_dac_ctrl(indio_dev, i, val, 0);
527 channels[
i] = st->
chip_info->channel_template;
530 if (pdata && ad5755_is_voltage_mode(pdata->
dac[i].mode))
541 #define AD5755_DEFAULT_DAC_PDATA { \
542 .mode = AD5755_MODE_CURRENT_4mA_20mA, \
543 .ext_current_sense_resistor = true, \
544 .enable_voltage_overrange = false, \
547 .rate = AD5755_SLEW_RATE_64k, \
548 .step_size = AD5755_SLEW_STEP_SIZE_1, \
553 .ext_dc_dc_compenstation_resistor =
false,
574 if (indio_dev ==
NULL) {
575 dev_err(&spi->
dev,
"Failed to allocate iio device\n");
579 st = iio_priv(indio_dev);
580 spi_set_drvdata(spi, indio_dev);
586 indio_dev->
dev.parent = &spi->
dev;
588 indio_dev->
info = &ad5755_info;
593 pdata = &ad5755_default_pdata;
595 ret = ad5755_init_channels(indio_dev, pdata);
599 ret = ad5755_setup_pdata(indio_dev, pdata);
605 dev_err(&spi->
dev,
"Failed to register iio device: %d\n", ret);
619 struct iio_dev *indio_dev = spi_get_drvdata(spi);
642 .probe = ad5755_probe,
644 .id_table = ad5755_id,