1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD5760, AD5780, AD5781, AD5790, AD5791 Voltage Output Digital to Analog
6 * Copyright 2011 Analog Devices Inc.
9 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/module.h>
19 #include <linux/bitops.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/dac/ad5791.h>
25 #define AD5791_DAC_MASK GENMASK(19, 0)
27 #define AD5791_CMD_READ BIT(23)
28 #define AD5791_CMD_WRITE 0
29 #define AD5791_ADDR(addr) ((addr) << 20)
32 #define AD5791_ADDR_NOOP 0
33 #define AD5791_ADDR_DAC0 1
34 #define AD5791_ADDR_CTRL 2
35 #define AD5791_ADDR_CLRCODE 3
36 #define AD5791_ADDR_SW_CTRL 4
38 /* Control Register */
39 #define AD5791_CTRL_RBUF BIT(1)
40 #define AD5791_CTRL_OPGND BIT(2)
41 #define AD5791_CTRL_DACTRI BIT(3)
42 #define AD5791_CTRL_BIN2SC BIT(4)
43 #define AD5791_CTRL_SDODIS BIT(5)
44 #define AD5761_CTRL_LINCOMP(x) ((x) << 6)
46 #define AD5791_LINCOMP_0_10 0
47 #define AD5791_LINCOMP_10_12 1
48 #define AD5791_LINCOMP_12_16 2
49 #define AD5791_LINCOMP_16_19 3
50 #define AD5791_LINCOMP_19_20 12
52 #define AD5780_LINCOMP_0_10 0
53 #define AD5780_LINCOMP_10_20 12
55 /* Software Control Register */
56 #define AD5791_SWCTRL_LDAC BIT(0)
57 #define AD5791_SWCTRL_CLR BIT(1)
58 #define AD5791_SWCTRL_RESET BIT(2)
60 #define AD5791_DAC_PWRDN_6K 0
61 #define AD5791_DAC_PWRDN_3STATE 1
64 * struct ad5791_chip_info - chip specific information
65 * @name: name of the dac chip
66 * @channel: channel specification
67 * @get_lin_comp: function pointer to the device specific function
69 struct ad5791_chip_info {
71 const struct iio_chan_spec channel;
72 int (*get_lin_comp)(unsigned int span);
76 * struct ad5791_state - driver instance specific data
78 * @reg_vdd: positive supply regulator
79 * @reg_vss: negative supply regulator
80 * @gpio_reset: reset gpio
81 * @gpio_clear: clear gpio
82 * @gpio_ldac: load dac gpio
83 * @chip_info: chip model specific constants
84 * @vref_mv: actual reference voltage used
85 * @vref_neg_mv: voltage of the negative supply
86 * @ctrl: control register cache
87 * @pwr_down_mode: current power down mode
88 * @pwr_down: true if device is powered down
89 * @data: spi transfer buffers
92 struct spi_device *spi;
93 struct regulator *reg_vdd;
94 struct regulator *reg_vss;
95 struct gpio_desc *gpio_reset;
96 struct gpio_desc *gpio_clear;
97 struct gpio_desc *gpio_ldac;
98 const struct ad5791_chip_info *chip_info;
99 unsigned short vref_mv;
100 unsigned int vref_neg_mv;
102 unsigned pwr_down_mode;
108 } data[3] __aligned(IIO_DMA_MINALIGN);
111 static int ad5791_spi_write(struct ad5791_state *st, u8 addr, u32 val)
113 st->data[0].d32 = cpu_to_be32(AD5791_CMD_WRITE |
115 (val & AD5791_DAC_MASK));
117 return spi_write(st->spi, &st->data[0].d8[1], 3);
120 static int ad5791_spi_read(struct ad5791_state *st, u8 addr, u32 *val)
123 struct spi_transfer xfers[] = {
125 .tx_buf = &st->data[0].d8[1],
130 .tx_buf = &st->data[1].d8[1],
131 .rx_buf = &st->data[2].d8[1],
137 st->data[0].d32 = cpu_to_be32(AD5791_CMD_READ |
139 st->data[1].d32 = cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP));
141 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
143 *val = be32_to_cpu(st->data[2].d32);
148 static const char * const ad5791_powerdown_modes[] = {
153 static int ad5791_get_powerdown_mode(struct iio_dev *indio_dev,
154 const struct iio_chan_spec *chan)
156 struct ad5791_state *st = iio_priv(indio_dev);
158 return st->pwr_down_mode;
161 static int ad5791_set_powerdown_mode(struct iio_dev *indio_dev,
162 const struct iio_chan_spec *chan, unsigned int mode)
164 struct ad5791_state *st = iio_priv(indio_dev);
166 st->pwr_down_mode = mode;
171 static const struct iio_enum ad5791_powerdown_mode_enum = {
172 .items = ad5791_powerdown_modes,
173 .num_items = ARRAY_SIZE(ad5791_powerdown_modes),
174 .get = ad5791_get_powerdown_mode,
175 .set = ad5791_set_powerdown_mode,
178 static ssize_t ad5791_read_dac_powerdown(struct iio_dev *indio_dev,
179 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
181 struct ad5791_state *st = iio_priv(indio_dev);
183 return sysfs_emit(buf, "%d\n", st->pwr_down);
186 static ssize_t ad5791_write_dac_powerdown(struct iio_dev *indio_dev,
187 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
192 struct ad5791_state *st = iio_priv(indio_dev);
194 ret = kstrtobool(buf, &pwr_down);
199 st->ctrl &= ~(AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
201 if (st->pwr_down_mode == AD5791_DAC_PWRDN_6K)
202 st->ctrl |= AD5791_CTRL_OPGND;
203 else if (st->pwr_down_mode == AD5791_DAC_PWRDN_3STATE)
204 st->ctrl |= AD5791_CTRL_DACTRI;
206 st->pwr_down = pwr_down;
208 ret = ad5791_spi_write(st, AD5791_ADDR_CTRL, st->ctrl);
210 return ret ? ret : len;
213 static int ad5791_get_lin_comp(unsigned int span)
216 return AD5791_LINCOMP_0_10;
217 else if (span <= 12000)
218 return AD5791_LINCOMP_10_12;
219 else if (span <= 16000)
220 return AD5791_LINCOMP_12_16;
221 else if (span <= 19000)
222 return AD5791_LINCOMP_16_19;
224 return AD5791_LINCOMP_19_20;
227 static int ad5780_get_lin_comp(unsigned int span)
230 return AD5780_LINCOMP_0_10;
232 return AD5780_LINCOMP_10_20;
235 static int ad5791_read_raw(struct iio_dev *indio_dev,
236 struct iio_chan_spec const *chan,
241 struct ad5791_state *st = iio_priv(indio_dev);
246 case IIO_CHAN_INFO_RAW:
247 ret = ad5791_spi_read(st, chan->address, val);
250 *val &= AD5791_DAC_MASK;
251 *val >>= chan->scan_type.shift;
253 case IIO_CHAN_INFO_SCALE:
255 *val2 = (1 << chan->scan_type.realbits) - 1;
256 return IIO_VAL_FRACTIONAL;
257 case IIO_CHAN_INFO_OFFSET:
258 val64 = (((u64)st->vref_neg_mv) << chan->scan_type.realbits);
259 do_div(val64, st->vref_mv);
268 static const struct iio_chan_spec_ext_info ad5791_ext_info[] = {
271 .shared = IIO_SHARED_BY_TYPE,
272 .read = ad5791_read_dac_powerdown,
273 .write = ad5791_write_dac_powerdown,
275 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
276 &ad5791_powerdown_mode_enum),
277 IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE, &ad5791_powerdown_mode_enum),
281 #define AD5791_DEFINE_CHIP_INFO(_name, bits, _shift, _lin_comp) \
282 static const struct ad5791_chip_info _name##_chip_info = { \
284 .get_lin_comp = &(_lin_comp), \
286 .type = IIO_VOLTAGE, \
289 .address = AD5791_ADDR_DAC0, \
291 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
292 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
293 BIT(IIO_CHAN_INFO_OFFSET), \
296 .realbits = (bits), \
300 .ext_info = ad5791_ext_info, \
304 AD5791_DEFINE_CHIP_INFO(ad5760, 16, 4, ad5780_get_lin_comp);
305 AD5791_DEFINE_CHIP_INFO(ad5780, 18, 2, ad5780_get_lin_comp);
306 AD5791_DEFINE_CHIP_INFO(ad5781, 18, 2, ad5791_get_lin_comp);
307 AD5791_DEFINE_CHIP_INFO(ad5790, 20, 0, ad5791_get_lin_comp);
308 AD5791_DEFINE_CHIP_INFO(ad5791, 20, 0, ad5791_get_lin_comp);
310 static int ad5791_write_raw(struct iio_dev *indio_dev,
311 struct iio_chan_spec const *chan,
316 struct ad5791_state *st = iio_priv(indio_dev);
319 case IIO_CHAN_INFO_RAW:
320 val &= GENMASK(chan->scan_type.realbits - 1, 0);
321 val <<= chan->scan_type.shift;
323 return ad5791_spi_write(st, chan->address, val);
330 static const struct iio_info ad5791_info = {
331 .read_raw = &ad5791_read_raw,
332 .write_raw = &ad5791_write_raw,
335 static int ad5791_probe(struct spi_device *spi)
337 const struct ad5791_platform_data *pdata = dev_get_platdata(&spi->dev);
338 struct iio_dev *indio_dev;
339 struct ad5791_state *st;
340 int ret, pos_voltage_uv = 0, neg_voltage_uv = 0;
343 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
346 st = iio_priv(indio_dev);
348 st->gpio_reset = devm_gpiod_get_optional(&spi->dev, "reset",
350 if (IS_ERR(st->gpio_reset))
351 return PTR_ERR(st->gpio_reset);
353 st->gpio_clear = devm_gpiod_get_optional(&spi->dev, "clear",
355 if (IS_ERR(st->gpio_clear))
356 return PTR_ERR(st->gpio_clear);
358 st->gpio_ldac = devm_gpiod_get_optional(&spi->dev, "ldac",
360 if (IS_ERR(st->gpio_ldac))
361 return PTR_ERR(st->gpio_ldac);
367 use_rbuf_gain2 = pdata->use_rbuf_gain2;
369 use_rbuf_gain2 = device_property_read_bool(&spi->dev,
370 "adi,rbuf-gain2-en");
372 pos_voltage_uv = devm_regulator_get_enable_read_voltage(&spi->dev, "vdd");
373 if (pos_voltage_uv < 0 && pos_voltage_uv != -ENODEV)
374 return dev_err_probe(&spi->dev, pos_voltage_uv,
375 "failed to get vdd voltage\n");
377 neg_voltage_uv = devm_regulator_get_enable_read_voltage(&spi->dev, "vss");
378 if (neg_voltage_uv < 0 && neg_voltage_uv != -ENODEV)
379 return dev_err_probe(&spi->dev, neg_voltage_uv,
380 "failed to get vss voltage\n");
382 if (neg_voltage_uv >= 0 && pos_voltage_uv >= 0) {
383 st->vref_mv = (pos_voltage_uv + neg_voltage_uv) / 1000;
384 st->vref_neg_mv = neg_voltage_uv / 1000;
386 st->vref_mv = pdata->vref_pos_mv + pdata->vref_neg_mv;
387 st->vref_neg_mv = pdata->vref_neg_mv;
389 dev_warn(&spi->dev, "reference voltage unspecified\n");
392 if (st->gpio_reset) {
394 gpiod_set_value_cansleep(st->gpio_reset, 0);
396 ret = ad5791_spi_write(st, AD5791_ADDR_SW_CTRL, AD5791_SWCTRL_RESET);
398 return dev_err_probe(&spi->dev, ret, "fail to reset\n");
401 st->chip_info = spi_get_device_match_data(spi);
403 return dev_err_probe(&spi->dev, -EINVAL, "no chip info\n");
405 st->ctrl = AD5761_CTRL_LINCOMP(st->chip_info->get_lin_comp(st->vref_mv))
406 | (use_rbuf_gain2 ? 0 : AD5791_CTRL_RBUF) |
409 ret = ad5791_spi_write(st, AD5791_ADDR_CTRL, st->ctrl |
410 AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
412 return dev_err_probe(&spi->dev, ret, "fail to write ctrl register\n");
414 indio_dev->info = &ad5791_info;
415 indio_dev->modes = INDIO_DIRECT_MODE;
416 indio_dev->channels = &st->chip_info->channel;
417 indio_dev->num_channels = 1;
418 indio_dev->name = st->chip_info->name;
419 return devm_iio_device_register(&spi->dev, indio_dev);
422 static const struct of_device_id ad5791_of_match[] = {
423 { .compatible = "adi,ad5760", .data = &ad5760_chip_info },
424 { .compatible = "adi,ad5780", .data = &ad5780_chip_info },
425 { .compatible = "adi,ad5781", .data = &ad5781_chip_info },
426 { .compatible = "adi,ad5790", .data = &ad5790_chip_info },
427 { .compatible = "adi,ad5791", .data = &ad5791_chip_info },
430 MODULE_DEVICE_TABLE(of, ad5791_of_match);
432 static const struct spi_device_id ad5791_id[] = {
433 { "ad5760", (kernel_ulong_t)&ad5760_chip_info },
434 { "ad5780", (kernel_ulong_t)&ad5780_chip_info },
435 { "ad5781", (kernel_ulong_t)&ad5781_chip_info },
436 { "ad5790", (kernel_ulong_t)&ad5790_chip_info },
437 { "ad5791", (kernel_ulong_t)&ad5791_chip_info },
440 MODULE_DEVICE_TABLE(spi, ad5791_id);
442 static struct spi_driver ad5791_driver = {
445 .of_match_table = ad5791_of_match,
447 .probe = ad5791_probe,
448 .id_table = ad5791_id,
450 module_spi_driver(ad5791_driver);
453 MODULE_DESCRIPTION("Analog Devices AD5760/AD5780/AD5781/AD5790/AD5791 DAC");
454 MODULE_LICENSE("GPL v2");