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/delay.h>
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/iio/iio.h>
12 #include <linux/module.h>
13 #include <linux/spi/spi.h>
14 #include <asm/unaligned.h>
16 #define AD5766_UPPER_WORD_SPI_MASK GENMASK(31, 16)
17 #define AD5766_LOWER_WORD_SPI_MASK GENMASK(15, 0)
18 #define AD5766_DITHER_SOURCE_MASK(ch) GENMASK(((2 * ch) + 1), (2 * ch))
19 #define AD5766_DITHER_SOURCE(ch, source) BIT((ch * 2) + source)
20 #define AD5766_DITHER_SCALE_MASK(x) AD5766_DITHER_SOURCE_MASK(x)
21 #define AD5766_DITHER_SCALE(ch, scale) (scale << (ch * 2))
22 #define AD5766_DITHER_ENABLE_MASK(ch) BIT(ch)
23 #define AD5766_DITHER_ENABLE(ch, state) ((!state) << ch)
24 #define AD5766_DITHER_INVERT_MASK(ch) BIT(ch)
25 #define AD5766_DITHER_INVERT(ch, state) (state << ch)
27 #define AD5766_CMD_NOP_MUX_OUT 0x00
28 #define AD5766_CMD_SDO_CNTRL 0x01
29 #define AD5766_CMD_WR_IN_REG(x) (0x10 | ((x) & GENMASK(3, 0)))
30 #define AD5766_CMD_WR_DAC_REG(x) (0x20 | ((x) & GENMASK(3, 0)))
31 #define AD5766_CMD_SW_LDAC 0x30
32 #define AD5766_CMD_SPAN_REG 0x40
33 #define AD5766_CMD_WR_PWR_DITHER 0x51
34 #define AD5766_CMD_WR_DAC_REG_ALL 0x60
35 #define AD5766_CMD_SW_FULL_RESET 0x70
36 #define AD5766_CMD_READBACK_REG(x) (0x80 | ((x) & GENMASK(3, 0)))
37 #define AD5766_CMD_DITHER_SIG_1 0x90
38 #define AD5766_CMD_DITHER_SIG_2 0xA0
39 #define AD5766_CMD_INV_DITHER 0xB0
40 #define AD5766_CMD_DITHER_SCALE_1 0xC0
41 #define AD5766_CMD_DITHER_SCALE_2 0xD0
43 #define AD5766_FULL_RESET_CODE 0x1234
50 enum ad5766_voltage_range {
51 AD5766_VOLTAGE_RANGE_M20V_0V,
52 AD5766_VOLTAGE_RANGE_M16V_to_0V,
53 AD5766_VOLTAGE_RANGE_M10V_to_0V,
54 AD5766_VOLTAGE_RANGE_M12V_to_14V,
55 AD5766_VOLTAGE_RANGE_M16V_to_10V,
56 AD5766_VOLTAGE_RANGE_M10V_to_6V,
57 AD5766_VOLTAGE_RANGE_M5V_to_5V,
58 AD5766_VOLTAGE_RANGE_M10V_to_10V,
62 * struct ad5766_chip_info - chip specific information
63 * @num_channels: number of channels
64 * @channels: channel specification
66 struct ad5766_chip_info {
67 unsigned int num_channels;
68 const struct iio_chan_spec *channels;
78 * Dither signal can also be scaled.
79 * Available dither scale strings corresponding to "dither_scale" field in
80 * "struct ad5766_state".
82 static const char * const ad5766_dither_scales[] = {
90 * struct ad5766_state - driver instance specific data
92 * @lock: Lock used to restrict concurrent access to SPI device
93 * @chip_info: Chip model specific constants
94 * @gpio_reset: Reset GPIO, used to reset the device
95 * @crt_range: Current selected output range
96 * @dither_enable: Power enable bit for each channel dither block (for
97 * example, D15 = DAC 15,D8 = DAC 8, and D0 = DAC 0)
98 * 0 - Normal operation, 1 - Power down
99 * @dither_invert: Inverts the dither signal applied to the selected DAC
101 * @dither_source: Selects between 2 possible sources:
103 * Two bits are used for each channel
104 * @dither_scale: Two bits are used for each of the 16 channels:
105 * 0: 1 SCALING, 1: 0.75 SCALING, 2: 0.5 SCALING,
107 * @data: SPI transfer buffers
109 struct ad5766_state {
110 struct spi_device *spi;
112 const struct ad5766_chip_info *chip_info;
113 struct gpio_desc *gpio_reset;
114 enum ad5766_voltage_range crt_range;
123 } data[3] ____cacheline_aligned;
126 struct ad5766_span_tbl {
131 static const struct ad5766_span_tbl ad5766_span_tbl[] = {
132 [AD5766_VOLTAGE_RANGE_M20V_0V] = {-20, 0},
133 [AD5766_VOLTAGE_RANGE_M16V_to_0V] = {-16, 0},
134 [AD5766_VOLTAGE_RANGE_M10V_to_0V] = {-10, 0},
135 [AD5766_VOLTAGE_RANGE_M12V_to_14V] = {-12, 14},
136 [AD5766_VOLTAGE_RANGE_M16V_to_10V] = {-16, 10},
137 [AD5766_VOLTAGE_RANGE_M10V_to_6V] = {-10, 6},
138 [AD5766_VOLTAGE_RANGE_M5V_to_5V] = {-5, 5},
139 [AD5766_VOLTAGE_RANGE_M10V_to_10V] = {-10, 10},
142 static int __ad5766_spi_read(struct ad5766_state *st, u8 dac, int *val)
145 struct spi_transfer xfers[] = {
147 .tx_buf = &st->data[0].d32,
152 .tx_buf = &st->data[1].d32,
153 .rx_buf = &st->data[2].d32,
159 st->data[0].d32 = AD5766_CMD_READBACK_REG(dac);
160 st->data[1].d32 = AD5766_CMD_NOP_MUX_OUT;
162 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
166 *val = st->data[2].w16[1];
171 static int __ad5766_spi_write(struct ad5766_state *st, u8 command, u16 data)
173 st->data[0].b8[0] = command;
174 put_unaligned_be16(data, &st->data[0].b8[1]);
176 return spi_write(st->spi, &st->data[0].b8[0], 3);
179 static int ad5766_read(struct iio_dev *indio_dev, u8 dac, int *val)
181 struct ad5766_state *st = iio_priv(indio_dev);
184 mutex_lock(&st->lock);
185 ret = __ad5766_spi_read(st, dac, val);
186 mutex_unlock(&st->lock);
191 static int ad5766_write(struct iio_dev *indio_dev, u8 dac, u16 data)
193 struct ad5766_state *st = iio_priv(indio_dev);
196 mutex_lock(&st->lock);
197 ret = __ad5766_spi_write(st, AD5766_CMD_WR_DAC_REG(dac), data);
198 mutex_unlock(&st->lock);
203 static int ad5766_reset(struct ad5766_state *st)
207 if (st->gpio_reset) {
208 gpiod_set_value_cansleep(st->gpio_reset, 1);
209 ndelay(100); /* t_reset >= 100ns */
210 gpiod_set_value_cansleep(st->gpio_reset, 0);
212 ret = __ad5766_spi_write(st, AD5766_CMD_SW_FULL_RESET,
213 AD5766_FULL_RESET_CODE);
219 * Minimum time between a reset and the subsequent successful write is
227 static int ad5766_read_raw(struct iio_dev *indio_dev,
228 struct iio_chan_spec const *chan,
233 struct ad5766_state *st = iio_priv(indio_dev);
237 case IIO_CHAN_INFO_RAW:
238 ret = ad5766_read(indio_dev, chan->address, val);
243 case IIO_CHAN_INFO_OFFSET:
244 *val = ad5766_span_tbl[st->crt_range].min;
247 case IIO_CHAN_INFO_SCALE:
248 *val = ad5766_span_tbl[st->crt_range].max -
249 ad5766_span_tbl[st->crt_range].min;
250 *val2 = st->chip_info->channels[0].scan_type.realbits;
252 return IIO_VAL_FRACTIONAL_LOG2;
258 static int ad5766_write_raw(struct iio_dev *indio_dev,
259 struct iio_chan_spec const *chan,
265 case IIO_CHAN_INFO_RAW:
267 const int max_val = GENMASK(chan->scan_type.realbits - 1, 0);
269 if (val > max_val || val < 0)
271 val <<= chan->scan_type.shift;
272 return ad5766_write(indio_dev, chan->address, val);
279 static const struct iio_info ad5766_info = {
280 .read_raw = ad5766_read_raw,
281 .write_raw = ad5766_write_raw,
284 static int ad5766_get_dither_source(struct iio_dev *dev,
285 const struct iio_chan_spec *chan)
287 struct ad5766_state *st = iio_priv(dev);
290 source = st->dither_source & AD5766_DITHER_SOURCE_MASK(chan->channel);
291 source = source >> (chan->channel * 2);
297 static int ad5766_set_dither_source(struct iio_dev *dev,
298 const struct iio_chan_spec *chan,
301 struct ad5766_state *st = iio_priv(dev);
305 st->dither_source &= ~AD5766_DITHER_SOURCE_MASK(chan->channel);
306 st->dither_source |= AD5766_DITHER_SOURCE(chan->channel, source);
308 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_source);
309 ret = ad5766_write(dev, AD5766_CMD_DITHER_SIG_1, val);
313 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_source);
315 return ad5766_write(dev, AD5766_CMD_DITHER_SIG_2, val);
318 static int ad5766_get_dither_scale(struct iio_dev *dev,
319 const struct iio_chan_spec *chan)
321 struct ad5766_state *st = iio_priv(dev);
324 scale = st->dither_scale & AD5766_DITHER_SCALE_MASK(chan->channel);
326 return (scale >> (chan->channel * 2));
329 static int ad5766_set_dither_scale(struct iio_dev *dev,
330 const struct iio_chan_spec *chan,
334 struct ad5766_state *st = iio_priv(dev);
337 st->dither_scale &= ~AD5766_DITHER_SCALE_MASK(chan->channel);
338 st->dither_scale |= AD5766_DITHER_SCALE(chan->channel, scale);
340 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_scale);
341 ret = ad5766_write(dev, AD5766_CMD_DITHER_SCALE_1, val);
344 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_scale);
346 return ad5766_write(dev, AD5766_CMD_DITHER_SCALE_2, val);
349 static const struct iio_enum ad5766_dither_scale_enum = {
350 .items = ad5766_dither_scales,
351 .num_items = ARRAY_SIZE(ad5766_dither_scales),
352 .set = ad5766_set_dither_scale,
353 .get = ad5766_get_dither_scale,
356 static ssize_t ad5766_read_ext(struct iio_dev *indio_dev,
358 const struct iio_chan_spec *chan,
361 struct ad5766_state *st = iio_priv(indio_dev);
364 case AD5766_DITHER_ENABLE:
365 return sprintf(buf, "%u\n",
366 !(st->dither_enable & BIT(chan->channel)));
368 case AD5766_DITHER_INVERT:
369 return sprintf(buf, "%u\n",
370 !!(st->dither_invert & BIT(chan->channel)));
372 case AD5766_DITHER_SOURCE:
373 return sprintf(buf, "%d\n",
374 ad5766_get_dither_source(indio_dev, chan));
380 static ssize_t ad5766_write_ext(struct iio_dev *indio_dev,
382 const struct iio_chan_spec *chan,
383 const char *buf, size_t len)
385 struct ad5766_state *st = iio_priv(indio_dev);
389 ret = kstrtobool(buf, &readin);
394 case AD5766_DITHER_ENABLE:
395 st->dither_enable &= ~AD5766_DITHER_ENABLE_MASK(chan->channel);
396 st->dither_enable |= AD5766_DITHER_ENABLE(chan->channel,
398 ret = ad5766_write(indio_dev, AD5766_CMD_WR_PWR_DITHER,
401 case AD5766_DITHER_INVERT:
402 st->dither_invert &= ~AD5766_DITHER_INVERT_MASK(chan->channel);
403 st->dither_invert |= AD5766_DITHER_INVERT(chan->channel,
405 ret = ad5766_write(indio_dev, AD5766_CMD_INV_DITHER,
408 case AD5766_DITHER_SOURCE:
409 ret = ad5766_set_dither_source(indio_dev, chan, readin);
415 return ret ? ret : len;
418 #define _AD5766_CHAN_EXT_INFO(_name, _what, _shared) { \
420 .read = ad5766_read_ext, \
421 .write = ad5766_write_ext, \
426 #define IIO_ENUM_AVAILABLE_SHARED(_name, _shared, _e) \
428 .name = (_name "_available"), \
430 .read = iio_enum_available_read, \
431 .private = (uintptr_t)(_e), \
434 static const struct iio_chan_spec_ext_info ad5766_ext_info[] = {
436 _AD5766_CHAN_EXT_INFO("dither_enable", AD5766_DITHER_ENABLE,
438 _AD5766_CHAN_EXT_INFO("dither_invert", AD5766_DITHER_INVERT,
440 _AD5766_CHAN_EXT_INFO("dither_source", AD5766_DITHER_SOURCE,
442 IIO_ENUM("dither_scale", IIO_SEPARATE, &ad5766_dither_scale_enum),
443 IIO_ENUM_AVAILABLE_SHARED("dither_scale",
445 &ad5766_dither_scale_enum),
449 #define AD576x_CHANNEL(_chan, _bits) { \
450 .type = IIO_VOLTAGE, \
453 .channel = (_chan), \
454 .address = (_chan), \
455 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
456 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | \
457 BIT(IIO_CHAN_INFO_SCALE), \
460 .realbits = (_bits), \
462 .shift = 16 - (_bits), \
464 .ext_info = ad5766_ext_info, \
467 #define DECLARE_AD576x_CHANNELS(_name, _bits) \
468 const struct iio_chan_spec _name[] = { \
469 AD576x_CHANNEL(0, (_bits)), \
470 AD576x_CHANNEL(1, (_bits)), \
471 AD576x_CHANNEL(2, (_bits)), \
472 AD576x_CHANNEL(3, (_bits)), \
473 AD576x_CHANNEL(4, (_bits)), \
474 AD576x_CHANNEL(5, (_bits)), \
475 AD576x_CHANNEL(6, (_bits)), \
476 AD576x_CHANNEL(7, (_bits)), \
477 AD576x_CHANNEL(8, (_bits)), \
478 AD576x_CHANNEL(9, (_bits)), \
479 AD576x_CHANNEL(10, (_bits)), \
480 AD576x_CHANNEL(11, (_bits)), \
481 AD576x_CHANNEL(12, (_bits)), \
482 AD576x_CHANNEL(13, (_bits)), \
483 AD576x_CHANNEL(14, (_bits)), \
484 AD576x_CHANNEL(15, (_bits)), \
487 static DECLARE_AD576x_CHANNELS(ad5766_channels, 16);
488 static DECLARE_AD576x_CHANNELS(ad5767_channels, 12);
490 static const struct ad5766_chip_info ad5766_chip_infos[] = {
492 .num_channels = ARRAY_SIZE(ad5766_channels),
493 .channels = ad5766_channels,
496 .num_channels = ARRAY_SIZE(ad5767_channels),
497 .channels = ad5767_channels,
501 static int ad5766_get_output_range(struct ad5766_state *st)
503 int i, ret, min, max, tmp[2];
505 ret = device_property_read_u32_array(&st->spi->dev,
506 "output-range-voltage",
513 for (i = 0; i < ARRAY_SIZE(ad5766_span_tbl); i++) {
514 if (ad5766_span_tbl[i].min != min ||
515 ad5766_span_tbl[i].max != max)
526 static int ad5766_default_setup(struct ad5766_state *st)
531 /* Always issue a reset before writing to the span register. */
532 ret = ad5766_reset(st);
536 ret = ad5766_get_output_range(st);
540 /* Dither power down */
541 st->dither_enable = GENMASK(15, 0);
542 ret = __ad5766_spi_write(st, AD5766_CMD_WR_PWR_DITHER,
547 st->dither_source = 0;
548 for (i = 0; i < ARRAY_SIZE(ad5766_channels); i++)
549 st->dither_source |= AD5766_DITHER_SOURCE(i, 0);
550 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_source);
551 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SIG_1, val);
555 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_source);
556 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SIG_2, val);
560 st->dither_scale = 0;
561 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_scale);
562 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SCALE_1, val);
566 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_scale);
567 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SCALE_2, val);
571 st->dither_invert = 0;
572 ret = __ad5766_spi_write(st, AD5766_CMD_INV_DITHER, st->dither_invert);
576 return __ad5766_spi_write(st, AD5766_CMD_SPAN_REG, st->crt_range);
579 static int ad5766_probe(struct spi_device *spi)
581 enum ad5766_type type;
582 struct iio_dev *indio_dev;
583 struct ad5766_state *st;
586 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
590 st = iio_priv(indio_dev);
591 mutex_init(&st->lock);
594 type = spi_get_device_id(spi)->driver_data;
595 st->chip_info = &ad5766_chip_infos[type];
597 indio_dev->channels = st->chip_info->channels;
598 indio_dev->num_channels = st->chip_info->num_channels;
599 indio_dev->info = &ad5766_info;
600 indio_dev->dev.parent = &spi->dev;
601 indio_dev->dev.of_node = spi->dev.of_node;
602 indio_dev->name = spi_get_device_id(spi)->name;
603 indio_dev->modes = INDIO_DIRECT_MODE;
605 st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset",
607 if (IS_ERR(st->gpio_reset))
608 return PTR_ERR(st->gpio_reset);
610 ret = ad5766_default_setup(st);
614 return devm_iio_device_register(&spi->dev, indio_dev);
617 static const struct of_device_id ad5766_dt_match[] = {
618 { .compatible = "adi,ad5766" },
619 { .compatible = "adi,ad5767" },
622 MODULE_DEVICE_TABLE(of, ad5766_dt_match);
624 static const struct spi_device_id ad5766_spi_ids[] = {
625 { "ad5766", ID_AD5766 },
626 { "ad5767", ID_AD5767 },
629 MODULE_DEVICE_TABLE(spi, ad5766_spi_ids);
631 static struct spi_driver ad5766_driver = {
634 .of_match_table = ad5766_dt_match,
636 .probe = ad5766_probe,
637 .id_table = ad5766_spi_ids,
639 module_spi_driver(ad5766_driver);
642 MODULE_DESCRIPTION("Analog Devices AD5766/AD5767 DACs");
643 MODULE_LICENSE("GPL v2");