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
);
129 case IIO_CHAN_INFO_SCALE
:
131 *val2
= chan
->scan_type
.realbits
;
132 return IIO_VAL_FRACTIONAL_LOG2
;
137 static int ad5686_write_raw(struct iio_dev
*indio_dev
,
138 struct iio_chan_spec
const *chan
,
143 struct ad5686_state
*st
= iio_priv(indio_dev
);
147 case IIO_CHAN_INFO_RAW
:
148 if (val
> (1 << chan
->scan_type
.realbits
) || val
< 0)
151 mutex_lock(&indio_dev
->mlock
);
153 AD5686_CMD_WRITE_INPUT_N_UPDATE_N
,
155 val
<< chan
->scan_type
.shift
);
156 mutex_unlock(&indio_dev
->mlock
);
165 static const struct iio_info ad5686_info
= {
166 .read_raw
= ad5686_read_raw
,
167 .write_raw
= ad5686_write_raw
,
170 static const struct iio_chan_spec_ext_info ad5686_ext_info
[] = {
173 .read
= ad5686_read_dac_powerdown
,
174 .write
= ad5686_write_dac_powerdown
,
175 .shared
= IIO_SEPARATE
,
177 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, &ad5686_powerdown_mode_enum
),
178 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5686_powerdown_mode_enum
),
182 #define AD5868_CHANNEL(chan, addr, bits, _shift) { \
183 .type = IIO_VOLTAGE, \
187 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
188 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
192 .realbits = (bits), \
196 .ext_info = ad5686_ext_info, \
199 #define DECLARE_AD5693_CHANNELS(name, bits, _shift) \
200 static struct iio_chan_spec name[] = { \
201 AD5868_CHANNEL(0, 0, bits, _shift), \
204 #define DECLARE_AD5686_CHANNELS(name, bits, _shift) \
205 static struct iio_chan_spec name[] = { \
206 AD5868_CHANNEL(0, 1, bits, _shift), \
207 AD5868_CHANNEL(1, 2, bits, _shift), \
208 AD5868_CHANNEL(2, 4, bits, _shift), \
209 AD5868_CHANNEL(3, 8, bits, _shift), \
212 #define DECLARE_AD5676_CHANNELS(name, bits, _shift) \
213 static struct iio_chan_spec name[] = { \
214 AD5868_CHANNEL(0, 0, bits, _shift), \
215 AD5868_CHANNEL(1, 1, bits, _shift), \
216 AD5868_CHANNEL(2, 2, bits, _shift), \
217 AD5868_CHANNEL(3, 3, bits, _shift), \
218 AD5868_CHANNEL(4, 4, bits, _shift), \
219 AD5868_CHANNEL(5, 5, bits, _shift), \
220 AD5868_CHANNEL(6, 6, bits, _shift), \
221 AD5868_CHANNEL(7, 7, bits, _shift), \
224 DECLARE_AD5693_CHANNELS(ad5311r_channels
, 10, 6);
225 DECLARE_AD5676_CHANNELS(ad5672_channels
, 12, 4);
226 DECLARE_AD5676_CHANNELS(ad5676_channels
, 16, 0);
227 DECLARE_AD5686_CHANNELS(ad5684_channels
, 12, 4);
228 DECLARE_AD5686_CHANNELS(ad5685r_channels
, 14, 2);
229 DECLARE_AD5686_CHANNELS(ad5686_channels
, 16, 0);
230 DECLARE_AD5693_CHANNELS(ad5693_channels
, 16, 0);
231 DECLARE_AD5693_CHANNELS(ad5692r_channels
, 14, 2);
232 DECLARE_AD5693_CHANNELS(ad5691r_channels
, 12, 4);
234 static const struct ad5686_chip_info ad5686_chip_info_tbl
[] = {
236 .channels
= ad5311r_channels
,
239 .regmap_type
= AD5693_REGMAP
,
242 .channels
= ad5672_channels
,
245 .regmap_type
= AD5686_REGMAP
,
248 .channels
= ad5672_channels
,
251 .regmap_type
= AD5686_REGMAP
,
254 .channels
= ad5676_channels
,
257 .regmap_type
= AD5686_REGMAP
,
260 .channels
= ad5676_channels
,
262 .regmap_type
= AD5686_REGMAP
,
265 .channels
= ad5676_channels
,
268 .regmap_type
= AD5686_REGMAP
,
271 .channels
= ad5691r_channels
,
274 .regmap_type
= AD5683_REGMAP
,
277 .channels
= ad5692r_channels
,
280 .regmap_type
= AD5683_REGMAP
,
283 .channels
= ad5693_channels
,
285 .regmap_type
= AD5683_REGMAP
,
288 .channels
= ad5693_channels
,
291 .regmap_type
= AD5683_REGMAP
,
294 .channels
= ad5684_channels
,
296 .regmap_type
= AD5686_REGMAP
,
299 .channels
= ad5684_channels
,
302 .regmap_type
= AD5686_REGMAP
,
305 .channels
= ad5685r_channels
,
308 .regmap_type
= AD5686_REGMAP
,
311 .channels
= ad5686_channels
,
313 .regmap_type
= AD5686_REGMAP
,
316 .channels
= ad5686_channels
,
319 .regmap_type
= AD5686_REGMAP
,
322 .channels
= ad5691r_channels
,
325 .regmap_type
= AD5693_REGMAP
,
328 .channels
= ad5692r_channels
,
331 .regmap_type
= AD5693_REGMAP
,
334 .channels
= ad5693_channels
,
336 .regmap_type
= AD5693_REGMAP
,
339 .channels
= ad5693_channels
,
342 .regmap_type
= AD5693_REGMAP
,
345 .channels
= ad5684_channels
,
347 .regmap_type
= AD5686_REGMAP
,
350 .channels
= ad5684_channels
,
353 .regmap_type
= AD5686_REGMAP
,
356 .channels
= ad5686_channels
,
358 .regmap_type
= AD5686_REGMAP
,
361 .channels
= ad5686_channels
,
364 .regmap_type
= AD5686_REGMAP
,
368 int ad5686_probe(struct device
*dev
,
369 enum ad5686_supported_device_ids chip_type
,
370 const char *name
, ad5686_write_func write
,
371 ad5686_read_func read
)
373 struct ad5686_state
*st
;
374 struct iio_dev
*indio_dev
;
375 unsigned int val
, ref_bit_msk
;
377 int ret
, i
, voltage_uv
= 0;
379 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
380 if (indio_dev
== NULL
)
383 st
= iio_priv(indio_dev
);
384 dev_set_drvdata(dev
, indio_dev
);
390 st
->reg
= devm_regulator_get_optional(dev
, "vcc");
391 if (!IS_ERR(st
->reg
)) {
392 ret
= regulator_enable(st
->reg
);
396 ret
= regulator_get_voltage(st
->reg
);
398 goto error_disable_reg
;
403 st
->chip_info
= &ad5686_chip_info_tbl
[chip_type
];
406 st
->vref_mv
= voltage_uv
/ 1000;
408 st
->vref_mv
= st
->chip_info
->int_vref_mv
;
410 /* Set all the power down mode for all channels to 1K pulldown */
411 for (i
= 0; i
< st
->chip_info
->num_channels
; i
++)
412 st
->pwr_down_mode
|= (0x01 << (i
* 2));
414 indio_dev
->dev
.parent
= dev
;
415 indio_dev
->name
= name
;
416 indio_dev
->info
= &ad5686_info
;
417 indio_dev
->modes
= INDIO_DIRECT_MODE
;
418 indio_dev
->channels
= st
->chip_info
->channels
;
419 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
421 switch (st
->chip_info
->regmap_type
) {
423 cmd
= AD5686_CMD_CONTROL_REG
;
424 ref_bit_msk
= AD5683_REF_BIT_MSK
;
425 st
->use_internal_vref
= !voltage_uv
;
428 cmd
= AD5686_CMD_INTERNAL_REFER_SETUP
;
432 cmd
= AD5686_CMD_CONTROL_REG
;
433 ref_bit_msk
= AD5693_REF_BIT_MSK
;
434 st
->use_internal_vref
= !voltage_uv
;
438 goto error_disable_reg
;
441 val
= (voltage_uv
| ref_bit_msk
);
443 ret
= st
->write(st
, cmd
, 0, !!val
);
445 goto error_disable_reg
;
447 ret
= iio_device_register(indio_dev
);
449 goto error_disable_reg
;
454 if (!IS_ERR(st
->reg
))
455 regulator_disable(st
->reg
);
458 EXPORT_SYMBOL_GPL(ad5686_probe
);
460 int ad5686_remove(struct device
*dev
)
462 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
463 struct ad5686_state
*st
= iio_priv(indio_dev
);
465 iio_device_unregister(indio_dev
);
466 if (!IS_ERR(st
->reg
))
467 regulator_disable(st
->reg
);
471 EXPORT_SYMBOL_GPL(ad5686_remove
);
473 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
474 MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
475 MODULE_LICENSE("GPL v2");