1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices AD5766, AD5767
4 * Digital to Analog Converters driver
5 * Copyright 2019-2020 Analog Devices Inc.
7 #include <linux/bitfield.h>
8 #include <linux/bitops.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/triggered_buffer.h>
14 #include <linux/iio/trigger_consumer.h>
15 #include <linux/module.h>
16 #include <linux/spi/spi.h>
17 #include <linux/unaligned.h>
19 #define AD5766_UPPER_WORD_SPI_MASK GENMASK(31, 16)
20 #define AD5766_LOWER_WORD_SPI_MASK GENMASK(15, 0)
21 #define AD5766_DITHER_SOURCE_MASK(ch) GENMASK(((2 * ch) + 1), (2 * ch))
22 #define AD5766_DITHER_SOURCE(ch, source) BIT((ch * 2) + source)
23 #define AD5766_DITHER_SCALE_MASK(x) AD5766_DITHER_SOURCE_MASK(x)
24 #define AD5766_DITHER_SCALE(ch, scale) (scale << (ch * 2))
25 #define AD5766_DITHER_ENABLE_MASK(ch) BIT(ch)
26 #define AD5766_DITHER_ENABLE(ch, state) ((!state) << ch)
27 #define AD5766_DITHER_INVERT_MASK(ch) BIT(ch)
28 #define AD5766_DITHER_INVERT(ch, state) (state << ch)
30 #define AD5766_CMD_NOP_MUX_OUT 0x00
31 #define AD5766_CMD_SDO_CNTRL 0x01
32 #define AD5766_CMD_WR_IN_REG(x) (0x10 | ((x) & GENMASK(3, 0)))
33 #define AD5766_CMD_WR_DAC_REG(x) (0x20 | ((x) & GENMASK(3, 0)))
34 #define AD5766_CMD_SW_LDAC 0x30
35 #define AD5766_CMD_SPAN_REG 0x40
36 #define AD5766_CMD_WR_PWR_DITHER 0x51
37 #define AD5766_CMD_WR_DAC_REG_ALL 0x60
38 #define AD5766_CMD_SW_FULL_RESET 0x70
39 #define AD5766_CMD_READBACK_REG(x) (0x80 | ((x) & GENMASK(3, 0)))
40 #define AD5766_CMD_DITHER_SIG_1 0x90
41 #define AD5766_CMD_DITHER_SIG_2 0xA0
42 #define AD5766_CMD_INV_DITHER 0xB0
43 #define AD5766_CMD_DITHER_SCALE_1 0xC0
44 #define AD5766_CMD_DITHER_SCALE_2 0xD0
46 #define AD5766_FULL_RESET_CODE 0x1234
53 enum ad5766_voltage_range {
54 AD5766_VOLTAGE_RANGE_M20V_0V,
55 AD5766_VOLTAGE_RANGE_M16V_to_0V,
56 AD5766_VOLTAGE_RANGE_M10V_to_0V,
57 AD5766_VOLTAGE_RANGE_M12V_to_14V,
58 AD5766_VOLTAGE_RANGE_M16V_to_10V,
59 AD5766_VOLTAGE_RANGE_M10V_to_6V,
60 AD5766_VOLTAGE_RANGE_M5V_to_5V,
61 AD5766_VOLTAGE_RANGE_M10V_to_10V,
65 * struct ad5766_chip_info - chip specific information
66 * @num_channels: number of channels
67 * @channels: channel specification
69 struct ad5766_chip_info {
70 unsigned int num_channels;
71 const struct iio_chan_spec *channels;
81 * Dither signal can also be scaled.
82 * Available dither scale strings corresponding to "dither_scale" field in
83 * "struct ad5766_state".
85 static const char * const ad5766_dither_scales[] = {
93 * struct ad5766_state - driver instance specific data
95 * @lock: Lock used to restrict concurrent access to SPI device
96 * @chip_info: Chip model specific constants
97 * @gpio_reset: Reset GPIO, used to reset the device
98 * @crt_range: Current selected output range
99 * @dither_enable: Power enable bit for each channel dither block (for
100 * example, D15 = DAC 15,D8 = DAC 8, and D0 = DAC 0)
101 * 0 - Normal operation, 1 - Power down
102 * @dither_invert: Inverts the dither signal applied to the selected DAC
104 * @dither_source: Selects between 2 possible sources:
106 * Two bits are used for each channel
107 * @dither_scale: Two bits are used for each of the 16 channels:
108 * 0: 1 SCALING, 1: 0.75 SCALING, 2: 0.5 SCALING,
110 * @data: SPI transfer buffers
112 struct ad5766_state {
113 struct spi_device *spi;
115 const struct ad5766_chip_info *chip_info;
116 struct gpio_desc *gpio_reset;
117 enum ad5766_voltage_range crt_range;
126 } data[3] __aligned(IIO_DMA_MINALIGN);
129 struct ad5766_span_tbl {
134 static const struct ad5766_span_tbl ad5766_span_tbl[] = {
135 [AD5766_VOLTAGE_RANGE_M20V_0V] = {-20, 0},
136 [AD5766_VOLTAGE_RANGE_M16V_to_0V] = {-16, 0},
137 [AD5766_VOLTAGE_RANGE_M10V_to_0V] = {-10, 0},
138 [AD5766_VOLTAGE_RANGE_M12V_to_14V] = {-12, 14},
139 [AD5766_VOLTAGE_RANGE_M16V_to_10V] = {-16, 10},
140 [AD5766_VOLTAGE_RANGE_M10V_to_6V] = {-10, 6},
141 [AD5766_VOLTAGE_RANGE_M5V_to_5V] = {-5, 5},
142 [AD5766_VOLTAGE_RANGE_M10V_to_10V] = {-10, 10},
145 static int __ad5766_spi_read(struct ad5766_state *st, u8 dac, int *val)
148 struct spi_transfer xfers[] = {
150 .tx_buf = &st->data[0].d32,
155 .tx_buf = &st->data[1].d32,
156 .rx_buf = &st->data[2].d32,
162 st->data[0].d32 = AD5766_CMD_READBACK_REG(dac);
163 st->data[1].d32 = AD5766_CMD_NOP_MUX_OUT;
165 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
169 *val = st->data[2].w16[1];
174 static int __ad5766_spi_write(struct ad5766_state *st, u8 command, u16 data)
176 st->data[0].b8[0] = command;
177 put_unaligned_be16(data, &st->data[0].b8[1]);
179 return spi_write(st->spi, &st->data[0].b8[0], 3);
182 static int ad5766_read(struct iio_dev *indio_dev, u8 dac, int *val)
184 struct ad5766_state *st = iio_priv(indio_dev);
187 mutex_lock(&st->lock);
188 ret = __ad5766_spi_read(st, dac, val);
189 mutex_unlock(&st->lock);
194 static int ad5766_write(struct iio_dev *indio_dev, u8 dac, u16 data)
196 struct ad5766_state *st = iio_priv(indio_dev);
199 mutex_lock(&st->lock);
200 ret = __ad5766_spi_write(st, AD5766_CMD_WR_DAC_REG(dac), data);
201 mutex_unlock(&st->lock);
206 static int ad5766_reset(struct ad5766_state *st)
210 if (st->gpio_reset) {
211 gpiod_set_value_cansleep(st->gpio_reset, 1);
212 ndelay(100); /* t_reset >= 100ns */
213 gpiod_set_value_cansleep(st->gpio_reset, 0);
215 ret = __ad5766_spi_write(st, AD5766_CMD_SW_FULL_RESET,
216 AD5766_FULL_RESET_CODE);
222 * Minimum time between a reset and the subsequent successful write is
230 static int ad5766_read_raw(struct iio_dev *indio_dev,
231 struct iio_chan_spec const *chan,
236 struct ad5766_state *st = iio_priv(indio_dev);
240 case IIO_CHAN_INFO_RAW:
241 ret = ad5766_read(indio_dev, chan->address, val);
246 case IIO_CHAN_INFO_OFFSET:
247 *val = ad5766_span_tbl[st->crt_range].min;
250 case IIO_CHAN_INFO_SCALE:
251 *val = ad5766_span_tbl[st->crt_range].max -
252 ad5766_span_tbl[st->crt_range].min;
253 *val2 = st->chip_info->channels[0].scan_type.realbits;
255 return IIO_VAL_FRACTIONAL_LOG2;
261 static int ad5766_write_raw(struct iio_dev *indio_dev,
262 struct iio_chan_spec const *chan,
268 case IIO_CHAN_INFO_RAW:
270 const int max_val = GENMASK(chan->scan_type.realbits - 1, 0);
272 if (val > max_val || val < 0)
274 val <<= chan->scan_type.shift;
275 return ad5766_write(indio_dev, chan->address, val);
282 static const struct iio_info ad5766_info = {
283 .read_raw = ad5766_read_raw,
284 .write_raw = ad5766_write_raw,
287 static int ad5766_get_dither_source(struct iio_dev *dev,
288 const struct iio_chan_spec *chan)
290 struct ad5766_state *st = iio_priv(dev);
293 source = st->dither_source & AD5766_DITHER_SOURCE_MASK(chan->channel);
294 source = source >> (chan->channel * 2);
300 static int ad5766_set_dither_source(struct iio_dev *dev,
301 const struct iio_chan_spec *chan,
304 struct ad5766_state *st = iio_priv(dev);
308 st->dither_source &= ~AD5766_DITHER_SOURCE_MASK(chan->channel);
309 st->dither_source |= AD5766_DITHER_SOURCE(chan->channel, source);
311 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_source);
312 ret = ad5766_write(dev, AD5766_CMD_DITHER_SIG_1, val);
316 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_source);
318 return ad5766_write(dev, AD5766_CMD_DITHER_SIG_2, val);
321 static int ad5766_get_dither_scale(struct iio_dev *dev,
322 const struct iio_chan_spec *chan)
324 struct ad5766_state *st = iio_priv(dev);
327 scale = st->dither_scale & AD5766_DITHER_SCALE_MASK(chan->channel);
329 return (scale >> (chan->channel * 2));
332 static int ad5766_set_dither_scale(struct iio_dev *dev,
333 const struct iio_chan_spec *chan,
337 struct ad5766_state *st = iio_priv(dev);
340 st->dither_scale &= ~AD5766_DITHER_SCALE_MASK(chan->channel);
341 st->dither_scale |= AD5766_DITHER_SCALE(chan->channel, scale);
343 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_scale);
344 ret = ad5766_write(dev, AD5766_CMD_DITHER_SCALE_1, val);
347 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_scale);
349 return ad5766_write(dev, AD5766_CMD_DITHER_SCALE_2, val);
352 static const struct iio_enum ad5766_dither_scale_enum = {
353 .items = ad5766_dither_scales,
354 .num_items = ARRAY_SIZE(ad5766_dither_scales),
355 .set = ad5766_set_dither_scale,
356 .get = ad5766_get_dither_scale,
359 static ssize_t ad5766_read_ext(struct iio_dev *indio_dev,
361 const struct iio_chan_spec *chan,
364 struct ad5766_state *st = iio_priv(indio_dev);
367 case AD5766_DITHER_ENABLE:
368 return sprintf(buf, "%u\n",
369 !(st->dither_enable & BIT(chan->channel)));
371 case AD5766_DITHER_INVERT:
372 return sprintf(buf, "%u\n",
373 !!(st->dither_invert & BIT(chan->channel)));
375 case AD5766_DITHER_SOURCE:
376 return sprintf(buf, "%d\n",
377 ad5766_get_dither_source(indio_dev, chan));
383 static ssize_t ad5766_write_ext(struct iio_dev *indio_dev,
385 const struct iio_chan_spec *chan,
386 const char *buf, size_t len)
388 struct ad5766_state *st = iio_priv(indio_dev);
392 ret = kstrtobool(buf, &readin);
397 case AD5766_DITHER_ENABLE:
398 st->dither_enable &= ~AD5766_DITHER_ENABLE_MASK(chan->channel);
399 st->dither_enable |= AD5766_DITHER_ENABLE(chan->channel,
401 ret = ad5766_write(indio_dev, AD5766_CMD_WR_PWR_DITHER,
404 case AD5766_DITHER_INVERT:
405 st->dither_invert &= ~AD5766_DITHER_INVERT_MASK(chan->channel);
406 st->dither_invert |= AD5766_DITHER_INVERT(chan->channel,
408 ret = ad5766_write(indio_dev, AD5766_CMD_INV_DITHER,
411 case AD5766_DITHER_SOURCE:
412 ret = ad5766_set_dither_source(indio_dev, chan, readin);
418 return ret ? ret : len;
421 #define _AD5766_CHAN_EXT_INFO(_name, _what, _shared) { \
423 .read = ad5766_read_ext, \
424 .write = ad5766_write_ext, \
429 static const struct iio_chan_spec_ext_info ad5766_ext_info[] = {
431 _AD5766_CHAN_EXT_INFO("dither_enable", AD5766_DITHER_ENABLE,
433 _AD5766_CHAN_EXT_INFO("dither_invert", AD5766_DITHER_INVERT,
435 _AD5766_CHAN_EXT_INFO("dither_source", AD5766_DITHER_SOURCE,
437 IIO_ENUM("dither_scale", IIO_SEPARATE, &ad5766_dither_scale_enum),
438 IIO_ENUM_AVAILABLE("dither_scale", IIO_SEPARATE,
439 &ad5766_dither_scale_enum),
443 #define AD576x_CHANNEL(_chan, _bits) { \
444 .type = IIO_VOLTAGE, \
447 .channel = (_chan), \
448 .address = (_chan), \
449 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
450 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | \
451 BIT(IIO_CHAN_INFO_SCALE), \
452 .scan_index = (_chan), \
455 .realbits = (_bits), \
457 .shift = 16 - (_bits), \
459 .ext_info = ad5766_ext_info, \
462 #define DECLARE_AD576x_CHANNELS(_name, _bits) \
463 const struct iio_chan_spec _name[] = { \
464 AD576x_CHANNEL(0, (_bits)), \
465 AD576x_CHANNEL(1, (_bits)), \
466 AD576x_CHANNEL(2, (_bits)), \
467 AD576x_CHANNEL(3, (_bits)), \
468 AD576x_CHANNEL(4, (_bits)), \
469 AD576x_CHANNEL(5, (_bits)), \
470 AD576x_CHANNEL(6, (_bits)), \
471 AD576x_CHANNEL(7, (_bits)), \
472 AD576x_CHANNEL(8, (_bits)), \
473 AD576x_CHANNEL(9, (_bits)), \
474 AD576x_CHANNEL(10, (_bits)), \
475 AD576x_CHANNEL(11, (_bits)), \
476 AD576x_CHANNEL(12, (_bits)), \
477 AD576x_CHANNEL(13, (_bits)), \
478 AD576x_CHANNEL(14, (_bits)), \
479 AD576x_CHANNEL(15, (_bits)), \
482 static DECLARE_AD576x_CHANNELS(ad5766_channels, 16);
483 static DECLARE_AD576x_CHANNELS(ad5767_channels, 12);
485 static const struct ad5766_chip_info ad5766_chip_infos[] = {
487 .num_channels = ARRAY_SIZE(ad5766_channels),
488 .channels = ad5766_channels,
491 .num_channels = ARRAY_SIZE(ad5767_channels),
492 .channels = ad5767_channels,
496 static int ad5766_get_output_range(struct ad5766_state *st)
498 int i, ret, min, max, tmp[2];
500 ret = device_property_read_u32_array(&st->spi->dev,
501 "output-range-microvolts",
506 min = tmp[0] / 1000000;
507 max = tmp[1] / 1000000;
508 for (i = 0; i < ARRAY_SIZE(ad5766_span_tbl); i++) {
509 if (ad5766_span_tbl[i].min != min ||
510 ad5766_span_tbl[i].max != max)
521 static int ad5766_default_setup(struct ad5766_state *st)
526 /* Always issue a reset before writing to the span register. */
527 ret = ad5766_reset(st);
531 ret = ad5766_get_output_range(st);
535 /* Dither power down */
536 st->dither_enable = GENMASK(15, 0);
537 ret = __ad5766_spi_write(st, AD5766_CMD_WR_PWR_DITHER,
542 st->dither_source = 0;
543 for (i = 0; i < ARRAY_SIZE(ad5766_channels); i++)
544 st->dither_source |= AD5766_DITHER_SOURCE(i, 0);
545 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_source);
546 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SIG_1, val);
550 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_source);
551 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SIG_2, val);
555 st->dither_scale = 0;
556 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_scale);
557 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SCALE_1, val);
561 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_scale);
562 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SCALE_2, val);
566 st->dither_invert = 0;
567 ret = __ad5766_spi_write(st, AD5766_CMD_INV_DITHER, st->dither_invert);
571 return __ad5766_spi_write(st, AD5766_CMD_SPAN_REG, st->crt_range);
574 static irqreturn_t ad5766_trigger_handler(int irq, void *p)
576 struct iio_poll_func *pf = p;
577 struct iio_dev *indio_dev = pf->indio_dev;
578 struct iio_buffer *buffer = indio_dev->buffer;
579 struct ad5766_state *st = iio_priv(indio_dev);
581 u16 data[ARRAY_SIZE(ad5766_channels)];
583 ret = iio_pop_from_buffer(buffer, data);
588 mutex_lock(&st->lock);
589 for_each_set_bit(ch, indio_dev->active_scan_mask,
590 st->chip_info->num_channels - 1)
591 __ad5766_spi_write(st, AD5766_CMD_WR_IN_REG(ch), data[i++]);
593 __ad5766_spi_write(st, AD5766_CMD_SW_LDAC,
594 *indio_dev->active_scan_mask);
595 mutex_unlock(&st->lock);
598 iio_trigger_notify_done(indio_dev->trig);
603 static int ad5766_probe(struct spi_device *spi)
605 enum ad5766_type type;
606 struct iio_dev *indio_dev;
607 struct ad5766_state *st;
610 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
614 st = iio_priv(indio_dev);
615 mutex_init(&st->lock);
618 type = spi_get_device_id(spi)->driver_data;
619 st->chip_info = &ad5766_chip_infos[type];
621 indio_dev->channels = st->chip_info->channels;
622 indio_dev->num_channels = st->chip_info->num_channels;
623 indio_dev->info = &ad5766_info;
624 indio_dev->name = spi_get_device_id(spi)->name;
625 indio_dev->modes = INDIO_DIRECT_MODE;
627 st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset",
629 if (IS_ERR(st->gpio_reset))
630 return PTR_ERR(st->gpio_reset);
632 ret = ad5766_default_setup(st);
636 /* Configure trigger buffer */
637 ret = devm_iio_triggered_buffer_setup_ext(&spi->dev, indio_dev, NULL,
638 ad5766_trigger_handler,
639 IIO_BUFFER_DIRECTION_OUT,
645 return devm_iio_device_register(&spi->dev, indio_dev);
648 static const struct of_device_id ad5766_dt_match[] = {
649 { .compatible = "adi,ad5766" },
650 { .compatible = "adi,ad5767" },
653 MODULE_DEVICE_TABLE(of, ad5766_dt_match);
655 static const struct spi_device_id ad5766_spi_ids[] = {
656 { "ad5766", ID_AD5766 },
657 { "ad5767", ID_AD5767 },
660 MODULE_DEVICE_TABLE(spi, ad5766_spi_ids);
662 static struct spi_driver ad5766_driver = {
665 .of_match_table = ad5766_dt_match,
667 .probe = ad5766_probe,
668 .id_table = ad5766_spi_ids,
670 module_spi_driver(ad5766_driver);
673 MODULE_DESCRIPTION("Analog Devices AD5766/AD5767 DACs");
674 MODULE_LICENSE("GPL v2");