1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD5024, AD5025, AD5044, AD5045, AD5064, AD5064-1, AD5065, AD5625, AD5625R,
4 * AD5627, AD5627R, AD5628, AD5629R, AD5645R, AD5647R, AD5648, AD5665, AD5665R,
5 * AD5666, AD5667, AD5667R, AD5668, AD5669R, LTC2606, LTC2607, LTC2609, LTC2616,
6 * LTC2617, LTC2619, LTC2626, LTC2627, LTC2629, LTC2631, LTC2633, LTC2635
7 * Digital to analog converters driver
9 * Copyright 2011 Analog Devices Inc.
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/sysfs.h>
20 #include <linux/regulator/consumer.h>
21 #include <asm/unaligned.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
26 #define AD5064_MAX_DAC_CHANNELS 8
27 #define AD5064_MAX_VREFS 4
29 #define AD5064_ADDR(x) ((x) << 20)
30 #define AD5064_CMD(x) ((x) << 24)
32 #define AD5064_ADDR_ALL_DAC 0xF
34 #define AD5064_CMD_WRITE_INPUT_N 0x0
35 #define AD5064_CMD_UPDATE_DAC_N 0x1
36 #define AD5064_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
37 #define AD5064_CMD_WRITE_INPUT_N_UPDATE_N 0x3
38 #define AD5064_CMD_POWERDOWN_DAC 0x4
39 #define AD5064_CMD_CLEAR 0x5
40 #define AD5064_CMD_LDAC_MASK 0x6
41 #define AD5064_CMD_RESET 0x7
42 #define AD5064_CMD_CONFIG 0x8
44 #define AD5064_CMD_RESET_V2 0x5
45 #define AD5064_CMD_CONFIG_V2 0x7
47 #define AD5064_CONFIG_DAISY_CHAIN_ENABLE BIT(1)
48 #define AD5064_CONFIG_INT_VREF_ENABLE BIT(0)
50 #define AD5064_LDAC_PWRDN_NONE 0x0
51 #define AD5064_LDAC_PWRDN_1K 0x1
52 #define AD5064_LDAC_PWRDN_100K 0x2
53 #define AD5064_LDAC_PWRDN_3STATE 0x3
56 * enum ad5064_regmap_type - Register layout variant
57 * @AD5064_REGMAP_ADI: Old Analog Devices register map layout
58 * @AD5064_REGMAP_ADI2: New Analog Devices register map layout
59 * @AD5064_REGMAP_LTC: LTC register map layout
61 enum ad5064_regmap_type
{
68 * struct ad5064_chip_info - chip specific information
69 * @shared_vref: whether the vref supply is shared between channels
70 * @internal_vref: internal reference voltage. 0 if the chip has no
72 * @channel: channel specification
73 * @num_channels: number of channels
74 * @regmap_type: register map layout variant
77 struct ad5064_chip_info
{
79 unsigned long internal_vref
;
80 const struct iio_chan_spec
*channels
;
81 unsigned int num_channels
;
82 enum ad5064_regmap_type regmap_type
;
87 typedef int (*ad5064_write_func
)(struct ad5064_state
*st
, unsigned int cmd
,
88 unsigned int addr
, unsigned int val
);
91 * struct ad5064_state - driver instance specific data
92 * @dev: the device for this driver instance
93 * @chip_info: chip model specific constants, available modes etc
94 * @vref_reg: vref supply regulators
95 * @pwr_down: whether channel is powered down
96 * @pwr_down_mode: channel's current power down mode
97 * @dac_cache: current DAC raw value (chip does not support readback)
98 * @use_internal_vref: set to true if the internal reference voltage should be
100 * @write: register write callback
101 * @data: i2c/spi transfer buffers
104 struct ad5064_state
{
106 const struct ad5064_chip_info
*chip_info
;
107 struct regulator_bulk_data vref_reg
[AD5064_MAX_VREFS
];
108 bool pwr_down
[AD5064_MAX_DAC_CHANNELS
];
109 u8 pwr_down_mode
[AD5064_MAX_DAC_CHANNELS
];
110 unsigned int dac_cache
[AD5064_MAX_DAC_CHANNELS
];
111 bool use_internal_vref
;
113 ad5064_write_func write
;
114 /* Lock used to maintain consistency between cached and dev state */
118 * DMA (thus cache coherency maintenance) requires the
119 * transfer buffers to live in their own cache lines.
124 } data ____cacheline_aligned
;
192 static int ad5064_write(struct ad5064_state
*st
, unsigned int cmd
,
193 unsigned int addr
, unsigned int val
, unsigned int shift
)
197 return st
->write(st
, cmd
, addr
, val
);
200 static int ad5064_sync_powerdown_mode(struct ad5064_state
*st
,
201 const struct iio_chan_spec
*chan
)
203 unsigned int val
, address
;
207 if (st
->chip_info
->regmap_type
== AD5064_REGMAP_LTC
) {
209 address
= chan
->address
;
211 if (st
->chip_info
->regmap_type
== AD5064_REGMAP_ADI2
)
216 val
= (0x1 << chan
->address
);
219 if (st
->pwr_down
[chan
->channel
])
220 val
|= st
->pwr_down_mode
[chan
->channel
] << shift
;
223 ret
= ad5064_write(st
, AD5064_CMD_POWERDOWN_DAC
, address
, val
, 0);
228 static const char * const ad5064_powerdown_modes
[] = {
234 static const char * const ltc2617_powerdown_modes
[] = {
238 static int ad5064_get_powerdown_mode(struct iio_dev
*indio_dev
,
239 const struct iio_chan_spec
*chan
)
241 struct ad5064_state
*st
= iio_priv(indio_dev
);
243 return st
->pwr_down_mode
[chan
->channel
] - 1;
246 static int ad5064_set_powerdown_mode(struct iio_dev
*indio_dev
,
247 const struct iio_chan_spec
*chan
, unsigned int mode
)
249 struct ad5064_state
*st
= iio_priv(indio_dev
);
252 mutex_lock(&st
->lock
);
253 st
->pwr_down_mode
[chan
->channel
] = mode
+ 1;
255 ret
= ad5064_sync_powerdown_mode(st
, chan
);
256 mutex_unlock(&st
->lock
);
261 static const struct iio_enum ad5064_powerdown_mode_enum
= {
262 .items
= ad5064_powerdown_modes
,
263 .num_items
= ARRAY_SIZE(ad5064_powerdown_modes
),
264 .get
= ad5064_get_powerdown_mode
,
265 .set
= ad5064_set_powerdown_mode
,
268 static const struct iio_enum ltc2617_powerdown_mode_enum
= {
269 .items
= ltc2617_powerdown_modes
,
270 .num_items
= ARRAY_SIZE(ltc2617_powerdown_modes
),
271 .get
= ad5064_get_powerdown_mode
,
272 .set
= ad5064_set_powerdown_mode
,
275 static ssize_t
ad5064_read_dac_powerdown(struct iio_dev
*indio_dev
,
276 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
278 struct ad5064_state
*st
= iio_priv(indio_dev
);
280 return sprintf(buf
, "%d\n", st
->pwr_down
[chan
->channel
]);
283 static ssize_t
ad5064_write_dac_powerdown(struct iio_dev
*indio_dev
,
284 uintptr_t private, const struct iio_chan_spec
*chan
, const char *buf
,
287 struct ad5064_state
*st
= iio_priv(indio_dev
);
291 ret
= strtobool(buf
, &pwr_down
);
295 mutex_lock(&st
->lock
);
296 st
->pwr_down
[chan
->channel
] = pwr_down
;
298 ret
= ad5064_sync_powerdown_mode(st
, chan
);
299 mutex_unlock(&st
->lock
);
300 return ret
? ret
: len
;
303 static int ad5064_get_vref(struct ad5064_state
*st
,
304 struct iio_chan_spec
const *chan
)
308 if (st
->use_internal_vref
)
309 return st
->chip_info
->internal_vref
;
311 i
= st
->chip_info
->shared_vref
? 0 : chan
->channel
;
312 return regulator_get_voltage(st
->vref_reg
[i
].consumer
);
315 static int ad5064_read_raw(struct iio_dev
*indio_dev
,
316 struct iio_chan_spec
const *chan
,
321 struct ad5064_state
*st
= iio_priv(indio_dev
);
325 case IIO_CHAN_INFO_RAW
:
326 *val
= st
->dac_cache
[chan
->channel
];
328 case IIO_CHAN_INFO_SCALE
:
329 scale_uv
= ad5064_get_vref(st
, chan
);
333 *val
= scale_uv
/ 1000;
334 *val2
= chan
->scan_type
.realbits
;
335 return IIO_VAL_FRACTIONAL_LOG2
;
342 static int ad5064_write_raw(struct iio_dev
*indio_dev
,
343 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
345 struct ad5064_state
*st
= iio_priv(indio_dev
);
349 case IIO_CHAN_INFO_RAW
:
350 if (val
>= (1 << chan
->scan_type
.realbits
) || val
< 0)
353 mutex_lock(&st
->lock
);
354 ret
= ad5064_write(st
, AD5064_CMD_WRITE_INPUT_N_UPDATE_N
,
355 chan
->address
, val
, chan
->scan_type
.shift
);
357 st
->dac_cache
[chan
->channel
] = val
;
358 mutex_unlock(&st
->lock
);
367 static const struct iio_info ad5064_info
= {
368 .read_raw
= ad5064_read_raw
,
369 .write_raw
= ad5064_write_raw
,
372 static const struct iio_chan_spec_ext_info ad5064_ext_info
[] = {
375 .read
= ad5064_read_dac_powerdown
,
376 .write
= ad5064_write_dac_powerdown
,
377 .shared
= IIO_SEPARATE
,
379 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, &ad5064_powerdown_mode_enum
),
380 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5064_powerdown_mode_enum
),
384 static const struct iio_chan_spec_ext_info ltc2617_ext_info
[] = {
387 .read
= ad5064_read_dac_powerdown
,
388 .write
= ad5064_write_dac_powerdown
,
389 .shared
= IIO_SEPARATE
,
391 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, <c2617_powerdown_mode_enum
),
392 IIO_ENUM_AVAILABLE("powerdown_mode", <c2617_powerdown_mode_enum
),
396 #define AD5064_CHANNEL(chan, addr, bits, _shift, _ext_info) { \
397 .type = IIO_VOLTAGE, \
401 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
402 BIT(IIO_CHAN_INFO_SCALE), \
406 .realbits = (bits), \
410 .ext_info = (_ext_info), \
413 #define DECLARE_AD5064_CHANNELS(name, bits, shift, ext_info) \
414 const struct iio_chan_spec name[] = { \
415 AD5064_CHANNEL(0, 0, bits, shift, ext_info), \
416 AD5064_CHANNEL(1, 1, bits, shift, ext_info), \
417 AD5064_CHANNEL(2, 2, bits, shift, ext_info), \
418 AD5064_CHANNEL(3, 3, bits, shift, ext_info), \
419 AD5064_CHANNEL(4, 4, bits, shift, ext_info), \
420 AD5064_CHANNEL(5, 5, bits, shift, ext_info), \
421 AD5064_CHANNEL(6, 6, bits, shift, ext_info), \
422 AD5064_CHANNEL(7, 7, bits, shift, ext_info), \
425 #define DECLARE_AD5065_CHANNELS(name, bits, shift, ext_info) \
426 const struct iio_chan_spec name[] = { \
427 AD5064_CHANNEL(0, 0, bits, shift, ext_info), \
428 AD5064_CHANNEL(1, 3, bits, shift, ext_info), \
431 static DECLARE_AD5064_CHANNELS(ad5024_channels
, 12, 8, ad5064_ext_info
);
432 static DECLARE_AD5064_CHANNELS(ad5044_channels
, 14, 6, ad5064_ext_info
);
433 static DECLARE_AD5064_CHANNELS(ad5064_channels
, 16, 4, ad5064_ext_info
);
435 static DECLARE_AD5065_CHANNELS(ad5025_channels
, 12, 8, ad5064_ext_info
);
436 static DECLARE_AD5065_CHANNELS(ad5045_channels
, 14, 6, ad5064_ext_info
);
437 static DECLARE_AD5065_CHANNELS(ad5065_channels
, 16, 4, ad5064_ext_info
);
439 static DECLARE_AD5064_CHANNELS(ad5629_channels
, 12, 4, ad5064_ext_info
);
440 static DECLARE_AD5064_CHANNELS(ad5645_channels
, 14, 2, ad5064_ext_info
);
441 static DECLARE_AD5064_CHANNELS(ad5669_channels
, 16, 0, ad5064_ext_info
);
443 static DECLARE_AD5064_CHANNELS(ltc2607_channels
, 16, 0, ltc2617_ext_info
);
444 static DECLARE_AD5064_CHANNELS(ltc2617_channels
, 14, 2, ltc2617_ext_info
);
445 static DECLARE_AD5064_CHANNELS(ltc2627_channels
, 12, 4, ltc2617_ext_info
);
446 #define ltc2631_12_channels ltc2627_channels
447 static DECLARE_AD5064_CHANNELS(ltc2631_10_channels
, 10, 6, ltc2617_ext_info
);
448 static DECLARE_AD5064_CHANNELS(ltc2631_8_channels
, 8, 8, ltc2617_ext_info
);
450 #define LTC2631_INFO(vref, pchannels, nchannels) \
452 .shared_vref = true, \
453 .internal_vref = vref, \
454 .channels = pchannels, \
455 .num_channels = nchannels, \
456 .regmap_type = AD5064_REGMAP_LTC, \
460 static const struct ad5064_chip_info ad5064_chip_info_tbl
[] = {
462 .shared_vref
= false,
463 .channels
= ad5024_channels
,
465 .regmap_type
= AD5064_REGMAP_ADI
,
468 .shared_vref
= false,
469 .channels
= ad5025_channels
,
471 .regmap_type
= AD5064_REGMAP_ADI
,
474 .shared_vref
= false,
475 .channels
= ad5044_channels
,
477 .regmap_type
= AD5064_REGMAP_ADI
,
480 .shared_vref
= false,
481 .channels
= ad5045_channels
,
483 .regmap_type
= AD5064_REGMAP_ADI
,
486 .shared_vref
= false,
487 .channels
= ad5064_channels
,
489 .regmap_type
= AD5064_REGMAP_ADI
,
493 .channels
= ad5064_channels
,
495 .regmap_type
= AD5064_REGMAP_ADI
,
498 .shared_vref
= false,
499 .channels
= ad5065_channels
,
501 .regmap_type
= AD5064_REGMAP_ADI
,
505 .channels
= ad5629_channels
,
507 .regmap_type
= AD5064_REGMAP_ADI2
509 [ID_AD5625R_1V25
] = {
511 .internal_vref
= 1250000,
512 .channels
= ad5629_channels
,
514 .regmap_type
= AD5064_REGMAP_ADI2
518 .internal_vref
= 2500000,
519 .channels
= ad5629_channels
,
521 .regmap_type
= AD5064_REGMAP_ADI2
525 .channels
= ad5629_channels
,
527 .regmap_type
= AD5064_REGMAP_ADI2
529 [ID_AD5627R_1V25
] = {
531 .internal_vref
= 1250000,
532 .channels
= ad5629_channels
,
534 .regmap_type
= AD5064_REGMAP_ADI2
538 .internal_vref
= 2500000,
539 .channels
= ad5629_channels
,
541 .regmap_type
= AD5064_REGMAP_ADI2
545 .internal_vref
= 2500000,
546 .channels
= ad5024_channels
,
548 .regmap_type
= AD5064_REGMAP_ADI
,
552 .internal_vref
= 5000000,
553 .channels
= ad5024_channels
,
555 .regmap_type
= AD5064_REGMAP_ADI
,
559 .internal_vref
= 2500000,
560 .channels
= ad5629_channels
,
562 .regmap_type
= AD5064_REGMAP_ADI
,
566 .internal_vref
= 5000000,
567 .channels
= ad5629_channels
,
569 .regmap_type
= AD5064_REGMAP_ADI
,
571 [ID_AD5645R_1V25
] = {
573 .internal_vref
= 1250000,
574 .channels
= ad5645_channels
,
576 .regmap_type
= AD5064_REGMAP_ADI2
580 .internal_vref
= 2500000,
581 .channels
= ad5645_channels
,
583 .regmap_type
= AD5064_REGMAP_ADI2
585 [ID_AD5647R_1V25
] = {
587 .internal_vref
= 1250000,
588 .channels
= ad5645_channels
,
590 .regmap_type
= AD5064_REGMAP_ADI2
594 .internal_vref
= 2500000,
595 .channels
= ad5645_channels
,
597 .regmap_type
= AD5064_REGMAP_ADI2
601 .internal_vref
= 2500000,
602 .channels
= ad5044_channels
,
604 .regmap_type
= AD5064_REGMAP_ADI
,
608 .internal_vref
= 5000000,
609 .channels
= ad5044_channels
,
611 .regmap_type
= AD5064_REGMAP_ADI
,
615 .channels
= ad5669_channels
,
617 .regmap_type
= AD5064_REGMAP_ADI2
619 [ID_AD5665R_1V25
] = {
621 .internal_vref
= 1250000,
622 .channels
= ad5669_channels
,
624 .regmap_type
= AD5064_REGMAP_ADI2
628 .internal_vref
= 2500000,
629 .channels
= ad5669_channels
,
631 .regmap_type
= AD5064_REGMAP_ADI2
635 .internal_vref
= 2500000,
636 .channels
= ad5064_channels
,
638 .regmap_type
= AD5064_REGMAP_ADI
,
642 .internal_vref
= 5000000,
643 .channels
= ad5064_channels
,
645 .regmap_type
= AD5064_REGMAP_ADI
,
649 .channels
= ad5669_channels
,
651 .regmap_type
= AD5064_REGMAP_ADI2
653 [ID_AD5667R_1V25
] = {
655 .internal_vref
= 1250000,
656 .channels
= ad5669_channels
,
658 .regmap_type
= AD5064_REGMAP_ADI2
662 .internal_vref
= 2500000,
663 .channels
= ad5669_channels
,
665 .regmap_type
= AD5064_REGMAP_ADI2
669 .internal_vref
= 2500000,
670 .channels
= ad5064_channels
,
672 .regmap_type
= AD5064_REGMAP_ADI
,
676 .internal_vref
= 5000000,
677 .channels
= ad5064_channels
,
679 .regmap_type
= AD5064_REGMAP_ADI
,
683 .internal_vref
= 2500000,
684 .channels
= ad5669_channels
,
686 .regmap_type
= AD5064_REGMAP_ADI
,
690 .internal_vref
= 5000000,
691 .channels
= ad5669_channels
,
693 .regmap_type
= AD5064_REGMAP_ADI
,
698 .channels
= ltc2607_channels
,
700 .regmap_type
= AD5064_REGMAP_LTC
,
705 .channels
= ltc2607_channels
,
707 .regmap_type
= AD5064_REGMAP_LTC
,
710 .shared_vref
= false,
712 .channels
= ltc2607_channels
,
714 .regmap_type
= AD5064_REGMAP_LTC
,
719 .channels
= ltc2617_channels
,
721 .regmap_type
= AD5064_REGMAP_LTC
,
726 .channels
= ltc2617_channels
,
728 .regmap_type
= AD5064_REGMAP_LTC
,
731 .shared_vref
= false,
733 .channels
= ltc2617_channels
,
735 .regmap_type
= AD5064_REGMAP_LTC
,
740 .channels
= ltc2627_channels
,
742 .regmap_type
= AD5064_REGMAP_LTC
,
747 .channels
= ltc2627_channels
,
749 .regmap_type
= AD5064_REGMAP_LTC
,
752 .shared_vref
= false,
754 .channels
= ltc2627_channels
,
756 .regmap_type
= AD5064_REGMAP_LTC
,
758 [ID_LTC2631_L12
] = LTC2631_INFO(2500000, ltc2631_12_channels
, 1),
759 [ID_LTC2631_H12
] = LTC2631_INFO(4096000, ltc2631_12_channels
, 1),
760 [ID_LTC2631_L10
] = LTC2631_INFO(2500000, ltc2631_10_channels
, 1),
761 [ID_LTC2631_H10
] = LTC2631_INFO(4096000, ltc2631_10_channels
, 1),
762 [ID_LTC2631_L8
] = LTC2631_INFO(2500000, ltc2631_8_channels
, 1),
763 [ID_LTC2631_H8
] = LTC2631_INFO(4096000, ltc2631_8_channels
, 1),
764 [ID_LTC2633_L12
] = LTC2631_INFO(2500000, ltc2631_12_channels
, 2),
765 [ID_LTC2633_H12
] = LTC2631_INFO(4096000, ltc2631_12_channels
, 2),
766 [ID_LTC2633_L10
] = LTC2631_INFO(2500000, ltc2631_10_channels
, 2),
767 [ID_LTC2633_H10
] = LTC2631_INFO(4096000, ltc2631_10_channels
, 2),
768 [ID_LTC2633_L8
] = LTC2631_INFO(2500000, ltc2631_8_channels
, 2),
769 [ID_LTC2633_H8
] = LTC2631_INFO(4096000, ltc2631_8_channels
, 2),
770 [ID_LTC2635_L12
] = LTC2631_INFO(2500000, ltc2631_12_channels
, 4),
771 [ID_LTC2635_H12
] = LTC2631_INFO(4096000, ltc2631_12_channels
, 4),
772 [ID_LTC2635_L10
] = LTC2631_INFO(2500000, ltc2631_10_channels
, 4),
773 [ID_LTC2635_H10
] = LTC2631_INFO(4096000, ltc2631_10_channels
, 4),
774 [ID_LTC2635_L8
] = LTC2631_INFO(2500000, ltc2631_8_channels
, 4),
775 [ID_LTC2635_H8
] = LTC2631_INFO(4096000, ltc2631_8_channels
, 4),
778 static inline unsigned int ad5064_num_vref(struct ad5064_state
*st
)
780 return st
->chip_info
->shared_vref
? 1 : st
->chip_info
->num_channels
;
783 static const char * const ad5064_vref_names
[] = {
790 static const char * const ad5064_vref_name(struct ad5064_state
*st
,
793 return st
->chip_info
->shared_vref
? "vref" : ad5064_vref_names
[vref
];
796 static int ad5064_set_config(struct ad5064_state
*st
, unsigned int val
)
800 switch (st
->chip_info
->regmap_type
) {
801 case AD5064_REGMAP_ADI2
:
802 cmd
= AD5064_CMD_CONFIG_V2
;
805 cmd
= AD5064_CMD_CONFIG
;
809 return ad5064_write(st
, cmd
, 0, val
, 0);
812 static int ad5064_request_vref(struct ad5064_state
*st
, struct device
*dev
)
817 for (i
= 0; i
< ad5064_num_vref(st
); ++i
)
818 st
->vref_reg
[i
].supply
= ad5064_vref_name(st
, i
);
820 if (!st
->chip_info
->internal_vref
)
821 return devm_regulator_bulk_get(dev
, ad5064_num_vref(st
),
825 * This assumes that when the regulator has an internal VREF
826 * there is only one external VREF connection, which is
827 * currently the case for all supported devices.
829 st
->vref_reg
[0].consumer
= devm_regulator_get_optional(dev
, "vref");
830 if (!IS_ERR(st
->vref_reg
[0].consumer
))
833 ret
= PTR_ERR(st
->vref_reg
[0].consumer
);
837 /* If no external regulator was supplied use the internal VREF */
838 st
->use_internal_vref
= true;
839 ret
= ad5064_set_config(st
, AD5064_CONFIG_INT_VREF_ENABLE
);
841 dev_err(dev
, "Failed to enable internal vref: %d\n", ret
);
846 static int ad5064_probe(struct device
*dev
, enum ad5064_type type
,
847 const char *name
, ad5064_write_func write
)
849 struct iio_dev
*indio_dev
;
850 struct ad5064_state
*st
;
851 unsigned int midscale
;
855 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
856 if (indio_dev
== NULL
)
859 st
= iio_priv(indio_dev
);
860 mutex_init(&st
->lock
);
861 dev_set_drvdata(dev
, indio_dev
);
863 st
->chip_info
= &ad5064_chip_info_tbl
[type
];
867 ret
= ad5064_request_vref(st
, dev
);
871 if (!st
->use_internal_vref
) {
872 ret
= regulator_bulk_enable(ad5064_num_vref(st
), st
->vref_reg
);
877 indio_dev
->dev
.parent
= dev
;
878 indio_dev
->name
= name
;
879 indio_dev
->info
= &ad5064_info
;
880 indio_dev
->modes
= INDIO_DIRECT_MODE
;
881 indio_dev
->channels
= st
->chip_info
->channels
;
882 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
884 midscale
= (1 << indio_dev
->channels
[0].scan_type
.realbits
) / 2;
886 for (i
= 0; i
< st
->chip_info
->num_channels
; ++i
) {
887 st
->pwr_down_mode
[i
] = AD5064_LDAC_PWRDN_1K
;
888 st
->dac_cache
[i
] = midscale
;
891 ret
= iio_device_register(indio_dev
);
893 goto error_disable_reg
;
898 if (!st
->use_internal_vref
)
899 regulator_bulk_disable(ad5064_num_vref(st
), st
->vref_reg
);
904 static int ad5064_remove(struct device
*dev
)
906 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
907 struct ad5064_state
*st
= iio_priv(indio_dev
);
909 iio_device_unregister(indio_dev
);
911 if (!st
->use_internal_vref
)
912 regulator_bulk_disable(ad5064_num_vref(st
), st
->vref_reg
);
917 #if IS_ENABLED(CONFIG_SPI_MASTER)
919 static int ad5064_spi_write(struct ad5064_state
*st
, unsigned int cmd
,
920 unsigned int addr
, unsigned int val
)
922 struct spi_device
*spi
= to_spi_device(st
->dev
);
924 st
->data
.spi
= cpu_to_be32(AD5064_CMD(cmd
) | AD5064_ADDR(addr
) | val
);
925 return spi_write(spi
, &st
->data
.spi
, sizeof(st
->data
.spi
));
928 static int ad5064_spi_probe(struct spi_device
*spi
)
930 const struct spi_device_id
*id
= spi_get_device_id(spi
);
932 return ad5064_probe(&spi
->dev
, id
->driver_data
, id
->name
,
936 static int ad5064_spi_remove(struct spi_device
*spi
)
938 return ad5064_remove(&spi
->dev
);
941 static const struct spi_device_id ad5064_spi_ids
[] = {
942 {"ad5024", ID_AD5024
},
943 {"ad5025", ID_AD5025
},
944 {"ad5044", ID_AD5044
},
945 {"ad5045", ID_AD5045
},
946 {"ad5064", ID_AD5064
},
947 {"ad5064-1", ID_AD5064_1
},
948 {"ad5065", ID_AD5065
},
949 {"ad5628-1", ID_AD5628_1
},
950 {"ad5628-2", ID_AD5628_2
},
951 {"ad5648-1", ID_AD5648_1
},
952 {"ad5648-2", ID_AD5648_2
},
953 {"ad5666-1", ID_AD5666_1
},
954 {"ad5666-2", ID_AD5666_2
},
955 {"ad5668-1", ID_AD5668_1
},
956 {"ad5668-2", ID_AD5668_2
},
957 {"ad5668-3", ID_AD5668_2
}, /* similar enough to ad5668-2 */
960 MODULE_DEVICE_TABLE(spi
, ad5064_spi_ids
);
962 static struct spi_driver ad5064_spi_driver
= {
966 .probe
= ad5064_spi_probe
,
967 .remove
= ad5064_spi_remove
,
968 .id_table
= ad5064_spi_ids
,
971 static int __init
ad5064_spi_register_driver(void)
973 return spi_register_driver(&ad5064_spi_driver
);
976 static void ad5064_spi_unregister_driver(void)
978 spi_unregister_driver(&ad5064_spi_driver
);
983 static inline int ad5064_spi_register_driver(void) { return 0; }
984 static inline void ad5064_spi_unregister_driver(void) { }
988 #if IS_ENABLED(CONFIG_I2C)
990 static int ad5064_i2c_write(struct ad5064_state
*st
, unsigned int cmd
,
991 unsigned int addr
, unsigned int val
)
993 struct i2c_client
*i2c
= to_i2c_client(st
->dev
);
994 unsigned int cmd_shift
;
997 switch (st
->chip_info
->regmap_type
) {
998 case AD5064_REGMAP_ADI2
:
1006 st
->data
.i2c
[0] = (cmd
<< cmd_shift
) | addr
;
1007 put_unaligned_be16(val
, &st
->data
.i2c
[1]);
1009 ret
= i2c_master_send(i2c
, st
->data
.i2c
, 3);
1016 static int ad5064_i2c_probe(struct i2c_client
*i2c
,
1017 const struct i2c_device_id
*id
)
1019 return ad5064_probe(&i2c
->dev
, id
->driver_data
, id
->name
,
1023 static int ad5064_i2c_remove(struct i2c_client
*i2c
)
1025 return ad5064_remove(&i2c
->dev
);
1028 static const struct i2c_device_id ad5064_i2c_ids
[] = {
1029 {"ad5625", ID_AD5625
},
1030 {"ad5625r-1v25", ID_AD5625R_1V25
},
1031 {"ad5625r-2v5", ID_AD5625R_2V5
},
1032 {"ad5627", ID_AD5627
},
1033 {"ad5627r-1v25", ID_AD5627R_1V25
},
1034 {"ad5627r-2v5", ID_AD5627R_2V5
},
1035 {"ad5629-1", ID_AD5629_1
},
1036 {"ad5629-2", ID_AD5629_2
},
1037 {"ad5629-3", ID_AD5629_2
}, /* similar enough to ad5629-2 */
1038 {"ad5645r-1v25", ID_AD5645R_1V25
},
1039 {"ad5645r-2v5", ID_AD5645R_2V5
},
1040 {"ad5665", ID_AD5665
},
1041 {"ad5665r-1v25", ID_AD5665R_1V25
},
1042 {"ad5665r-2v5", ID_AD5665R_2V5
},
1043 {"ad5667", ID_AD5667
},
1044 {"ad5667r-1v25", ID_AD5667R_1V25
},
1045 {"ad5667r-2v5", ID_AD5667R_2V5
},
1046 {"ad5669-1", ID_AD5669_1
},
1047 {"ad5669-2", ID_AD5669_2
},
1048 {"ad5669-3", ID_AD5669_2
}, /* similar enough to ad5669-2 */
1049 {"ltc2606", ID_LTC2606
},
1050 {"ltc2607", ID_LTC2607
},
1051 {"ltc2609", ID_LTC2609
},
1052 {"ltc2616", ID_LTC2616
},
1053 {"ltc2617", ID_LTC2617
},
1054 {"ltc2619", ID_LTC2619
},
1055 {"ltc2626", ID_LTC2626
},
1056 {"ltc2627", ID_LTC2627
},
1057 {"ltc2629", ID_LTC2629
},
1058 {"ltc2631-l12", ID_LTC2631_L12
},
1059 {"ltc2631-h12", ID_LTC2631_H12
},
1060 {"ltc2631-l10", ID_LTC2631_L10
},
1061 {"ltc2631-h10", ID_LTC2631_H10
},
1062 {"ltc2631-l8", ID_LTC2631_L8
},
1063 {"ltc2631-h8", ID_LTC2631_H8
},
1064 {"ltc2633-l12", ID_LTC2633_L12
},
1065 {"ltc2633-h12", ID_LTC2633_H12
},
1066 {"ltc2633-l10", ID_LTC2633_L10
},
1067 {"ltc2633-h10", ID_LTC2633_H10
},
1068 {"ltc2633-l8", ID_LTC2633_L8
},
1069 {"ltc2633-h8", ID_LTC2633_H8
},
1070 {"ltc2635-l12", ID_LTC2635_L12
},
1071 {"ltc2635-h12", ID_LTC2635_H12
},
1072 {"ltc2635-l10", ID_LTC2635_L10
},
1073 {"ltc2635-h10", ID_LTC2635_H10
},
1074 {"ltc2635-l8", ID_LTC2635_L8
},
1075 {"ltc2635-h8", ID_LTC2635_H8
},
1078 MODULE_DEVICE_TABLE(i2c
, ad5064_i2c_ids
);
1080 static struct i2c_driver ad5064_i2c_driver
= {
1084 .probe
= ad5064_i2c_probe
,
1085 .remove
= ad5064_i2c_remove
,
1086 .id_table
= ad5064_i2c_ids
,
1089 static int __init
ad5064_i2c_register_driver(void)
1091 return i2c_add_driver(&ad5064_i2c_driver
);
1094 static void __exit
ad5064_i2c_unregister_driver(void)
1096 i2c_del_driver(&ad5064_i2c_driver
);
1101 static inline int ad5064_i2c_register_driver(void) { return 0; }
1102 static inline void ad5064_i2c_unregister_driver(void) { }
1106 static int __init
ad5064_init(void)
1110 ret
= ad5064_spi_register_driver();
1114 ret
= ad5064_i2c_register_driver();
1116 ad5064_spi_unregister_driver();
1122 module_init(ad5064_init
);
1124 static void __exit
ad5064_exit(void)
1126 ad5064_i2c_unregister_driver();
1127 ad5064_spi_unregister_driver();
1129 module_exit(ad5064_exit
);
1131 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1132 MODULE_DESCRIPTION("Analog Devices AD5024 and similar multi-channel DACs");
1133 MODULE_LICENSE("GPL v2");