1 // SPDX-License-Identifier: GPL-2.0+
3 * AD5686R, AD5685R, AD5684R Digital to analog converters driver
5 * Copyright 2011 Analog Devices Inc.
8 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/regulator/consumer.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
22 static const char * const ad5686_powerdown_modes[] = {
28 static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev,
29 const struct iio_chan_spec *chan)
31 struct ad5686_state *st = iio_priv(indio_dev);
33 return ((st->pwr_down_mode >> (chan->channel * 2)) & 0x3) - 1;
36 static int ad5686_set_powerdown_mode(struct iio_dev *indio_dev,
37 const struct iio_chan_spec *chan,
40 struct ad5686_state *st = iio_priv(indio_dev);
42 st->pwr_down_mode &= ~(0x3 << (chan->channel * 2));
43 st->pwr_down_mode |= ((mode + 1) << (chan->channel * 2));
48 static const struct iio_enum ad5686_powerdown_mode_enum = {
49 .items = ad5686_powerdown_modes,
50 .num_items = ARRAY_SIZE(ad5686_powerdown_modes),
51 .get = ad5686_get_powerdown_mode,
52 .set = ad5686_set_powerdown_mode,
55 static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev,
56 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
58 struct ad5686_state *st = iio_priv(indio_dev);
60 return sprintf(buf, "%d\n", !!(st->pwr_down_mask &
61 (0x3 << (chan->channel * 2))));
64 static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
66 const struct iio_chan_spec *chan,
72 struct ad5686_state *st = iio_priv(indio_dev);
73 unsigned int val, ref_bit_msk;
76 ret = strtobool(buf, &readin);
81 st->pwr_down_mask |= (0x3 << (chan->channel * 2));
83 st->pwr_down_mask &= ~(0x3 << (chan->channel * 2));
85 switch (st->chip_info->regmap_type) {
88 ref_bit_msk = AD5683_REF_BIT_MSK;
96 ref_bit_msk = AD5693_REF_BIT_MSK;
102 val = ((st->pwr_down_mask & st->pwr_down_mode) << shift);
103 if (!st->use_internal_vref)
106 ret = st->write(st, AD5686_CMD_POWERDOWN_DAC, 0, val);
108 return ret ? ret : len;
111 static int ad5686_read_raw(struct iio_dev *indio_dev,
112 struct iio_chan_spec const *chan,
117 struct ad5686_state *st = iio_priv(indio_dev);
121 case IIO_CHAN_INFO_RAW:
122 mutex_lock(&indio_dev->mlock);
123 ret = st->read(st, chan->address);
124 mutex_unlock(&indio_dev->mlock);
127 *val = (ret >> chan->scan_type.shift) &
128 GENMASK(chan->scan_type.realbits - 1, 0);
130 case IIO_CHAN_INFO_SCALE:
132 *val2 = chan->scan_type.realbits;
133 return IIO_VAL_FRACTIONAL_LOG2;
138 static int ad5686_write_raw(struct iio_dev *indio_dev,
139 struct iio_chan_spec const *chan,
144 struct ad5686_state *st = iio_priv(indio_dev);
148 case IIO_CHAN_INFO_RAW:
149 if (val > (1 << chan->scan_type.realbits) || val < 0)
152 mutex_lock(&indio_dev->mlock);
154 AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
156 val << chan->scan_type.shift);
157 mutex_unlock(&indio_dev->mlock);
166 static const struct iio_info ad5686_info = {
167 .read_raw = ad5686_read_raw,
168 .write_raw = ad5686_write_raw,
171 static const struct iio_chan_spec_ext_info ad5686_ext_info[] = {
174 .read = ad5686_read_dac_powerdown,
175 .write = ad5686_write_dac_powerdown,
176 .shared = IIO_SEPARATE,
178 IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5686_powerdown_mode_enum),
179 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5686_powerdown_mode_enum),
183 #define AD5868_CHANNEL(chan, addr, bits, _shift) { \
184 .type = IIO_VOLTAGE, \
188 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
189 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
193 .realbits = (bits), \
197 .ext_info = ad5686_ext_info, \
200 #define DECLARE_AD5693_CHANNELS(name, bits, _shift) \
201 static struct iio_chan_spec name[] = { \
202 AD5868_CHANNEL(0, 0, bits, _shift), \
205 #define DECLARE_AD5686_CHANNELS(name, bits, _shift) \
206 static struct iio_chan_spec name[] = { \
207 AD5868_CHANNEL(0, 1, bits, _shift), \
208 AD5868_CHANNEL(1, 2, bits, _shift), \
209 AD5868_CHANNEL(2, 4, bits, _shift), \
210 AD5868_CHANNEL(3, 8, bits, _shift), \
213 #define DECLARE_AD5676_CHANNELS(name, bits, _shift) \
214 static struct iio_chan_spec name[] = { \
215 AD5868_CHANNEL(0, 0, bits, _shift), \
216 AD5868_CHANNEL(1, 1, bits, _shift), \
217 AD5868_CHANNEL(2, 2, bits, _shift), \
218 AD5868_CHANNEL(3, 3, bits, _shift), \
219 AD5868_CHANNEL(4, 4, bits, _shift), \
220 AD5868_CHANNEL(5, 5, bits, _shift), \
221 AD5868_CHANNEL(6, 6, bits, _shift), \
222 AD5868_CHANNEL(7, 7, bits, _shift), \
225 DECLARE_AD5693_CHANNELS(ad5311r_channels, 10, 6);
226 DECLARE_AD5676_CHANNELS(ad5672_channels, 12, 4);
227 DECLARE_AD5676_CHANNELS(ad5676_channels, 16, 0);
228 DECLARE_AD5686_CHANNELS(ad5684_channels, 12, 4);
229 DECLARE_AD5686_CHANNELS(ad5685r_channels, 14, 2);
230 DECLARE_AD5686_CHANNELS(ad5686_channels, 16, 0);
231 DECLARE_AD5693_CHANNELS(ad5693_channels, 16, 0);
232 DECLARE_AD5693_CHANNELS(ad5692r_channels, 14, 2);
233 DECLARE_AD5693_CHANNELS(ad5691r_channels, 12, 4);
235 static const struct ad5686_chip_info ad5686_chip_info_tbl[] = {
237 .channels = ad5311r_channels,
240 .regmap_type = AD5693_REGMAP,
243 .channels = ad5672_channels,
246 .regmap_type = AD5686_REGMAP,
249 .channels = ad5672_channels,
252 .regmap_type = AD5686_REGMAP,
255 .channels = ad5676_channels,
258 .regmap_type = AD5686_REGMAP,
261 .channels = ad5676_channels,
263 .regmap_type = AD5686_REGMAP,
266 .channels = ad5676_channels,
269 .regmap_type = AD5686_REGMAP,
272 .channels = ad5691r_channels,
275 .regmap_type = AD5683_REGMAP,
278 .channels = ad5692r_channels,
281 .regmap_type = AD5683_REGMAP,
284 .channels = ad5693_channels,
286 .regmap_type = AD5683_REGMAP,
289 .channels = ad5693_channels,
292 .regmap_type = AD5683_REGMAP,
295 .channels = ad5684_channels,
297 .regmap_type = AD5686_REGMAP,
300 .channels = ad5684_channels,
303 .regmap_type = AD5686_REGMAP,
306 .channels = ad5685r_channels,
309 .regmap_type = AD5686_REGMAP,
312 .channels = ad5686_channels,
314 .regmap_type = AD5686_REGMAP,
317 .channels = ad5686_channels,
320 .regmap_type = AD5686_REGMAP,
323 .channels = ad5691r_channels,
326 .regmap_type = AD5693_REGMAP,
329 .channels = ad5692r_channels,
332 .regmap_type = AD5693_REGMAP,
335 .channels = ad5693_channels,
337 .regmap_type = AD5693_REGMAP,
340 .channels = ad5693_channels,
343 .regmap_type = AD5693_REGMAP,
346 .channels = ad5684_channels,
348 .regmap_type = AD5686_REGMAP,
351 .channels = ad5684_channels,
354 .regmap_type = AD5686_REGMAP,
357 .channels = ad5686_channels,
359 .regmap_type = AD5686_REGMAP,
362 .channels = ad5686_channels,
365 .regmap_type = AD5686_REGMAP,
369 int ad5686_probe(struct device *dev,
370 enum ad5686_supported_device_ids chip_type,
371 const char *name, ad5686_write_func write,
372 ad5686_read_func read)
374 struct ad5686_state *st;
375 struct iio_dev *indio_dev;
376 unsigned int val, ref_bit_msk;
378 int ret, i, voltage_uv = 0;
380 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
381 if (indio_dev == NULL)
384 st = iio_priv(indio_dev);
385 dev_set_drvdata(dev, indio_dev);
391 st->reg = devm_regulator_get_optional(dev, "vcc");
392 if (!IS_ERR(st->reg)) {
393 ret = regulator_enable(st->reg);
397 ret = regulator_get_voltage(st->reg);
399 goto error_disable_reg;
404 st->chip_info = &ad5686_chip_info_tbl[chip_type];
407 st->vref_mv = voltage_uv / 1000;
409 st->vref_mv = st->chip_info->int_vref_mv;
411 /* Set all the power down mode for all channels to 1K pulldown */
412 for (i = 0; i < st->chip_info->num_channels; i++)
413 st->pwr_down_mode |= (0x01 << (i * 2));
415 indio_dev->dev.parent = dev;
416 indio_dev->name = name;
417 indio_dev->info = &ad5686_info;
418 indio_dev->modes = INDIO_DIRECT_MODE;
419 indio_dev->channels = st->chip_info->channels;
420 indio_dev->num_channels = st->chip_info->num_channels;
422 switch (st->chip_info->regmap_type) {
424 cmd = AD5686_CMD_CONTROL_REG;
425 ref_bit_msk = AD5683_REF_BIT_MSK;
426 st->use_internal_vref = !voltage_uv;
429 cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
433 cmd = AD5686_CMD_CONTROL_REG;
434 ref_bit_msk = AD5693_REF_BIT_MSK;
435 st->use_internal_vref = !voltage_uv;
439 goto error_disable_reg;
442 val = (voltage_uv | ref_bit_msk);
444 ret = st->write(st, cmd, 0, !!val);
446 goto error_disable_reg;
448 ret = iio_device_register(indio_dev);
450 goto error_disable_reg;
455 if (!IS_ERR(st->reg))
456 regulator_disable(st->reg);
459 EXPORT_SYMBOL_GPL(ad5686_probe);
461 int ad5686_remove(struct device *dev)
463 struct iio_dev *indio_dev = dev_get_drvdata(dev);
464 struct ad5686_state *st = iio_priv(indio_dev);
466 iio_device_unregister(indio_dev);
467 if (!IS_ERR(st->reg))
468 regulator_disable(st->reg);
472 EXPORT_SYMBOL_GPL(ad5686_remove);
474 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
475 MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
476 MODULE_LICENSE("GPL v2");