10 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
15 #include <linux/i2c.h>
16 #include <linux/slab.h>
19 #include <asm/unaligned.h>
24 #define AD5064_MAX_DAC_CHANNELS 8
25 #define AD5064_MAX_VREFS 4
27 #define AD5064_ADDR(x) ((x) << 20)
28 #define AD5064_CMD(x) ((x) << 24)
30 #define AD5064_ADDR_DAC(chan) (chan)
31 #define AD5064_ADDR_ALL_DAC 0xF
33 #define AD5064_CMD_WRITE_INPUT_N 0x0
34 #define AD5064_CMD_UPDATE_DAC_N 0x1
35 #define AD5064_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
36 #define AD5064_CMD_WRITE_INPUT_N_UPDATE_N 0x3
37 #define AD5064_CMD_POWERDOWN_DAC 0x4
38 #define AD5064_CMD_CLEAR 0x5
39 #define AD5064_CMD_LDAC_MASK 0x6
40 #define AD5064_CMD_RESET 0x7
41 #define AD5064_CMD_CONFIG 0x8
43 #define AD5064_CONFIG_DAISY_CHAIN_ENABLE BIT(1)
44 #define AD5064_CONFIG_INT_VREF_ENABLE BIT(0)
46 #define AD5064_LDAC_PWRDN_NONE 0x0
47 #define AD5064_LDAC_PWRDN_1K 0x1
48 #define AD5064_LDAC_PWRDN_100K 0x2
49 #define AD5064_LDAC_PWRDN_3STATE 0x3
70 unsigned int addr,
unsigned int val);
126 unsigned int addr,
unsigned int val,
unsigned int shift)
130 return st->
write(st, cmd, addr, val);
133 static int ad5064_sync_powerdown_mode(
struct ad5064_state *st,
149 static const char *
const ad5064_powerdown_modes[] = {
155 static int ad5064_get_powerdown_mode(
struct iio_dev *indio_dev,
163 static int ad5064_set_powerdown_mode(
struct iio_dev *indio_dev,
172 ret = ad5064_sync_powerdown_mode(st, chan->
channel);
178 static const struct iio_enum ad5064_powerdown_mode_enum = {
179 .items = ad5064_powerdown_modes,
180 .num_items =
ARRAY_SIZE(ad5064_powerdown_modes),
181 .get = ad5064_get_powerdown_mode,
182 .set = ad5064_set_powerdown_mode,
185 static ssize_t ad5064_read_dac_powerdown(
struct iio_dev *indio_dev,
193 static ssize_t ad5064_write_dac_powerdown(
struct iio_dev *indio_dev,
208 ret = ad5064_sync_powerdown_mode(st, chan->
channel);
210 return ret ? ret : len;
225 static int ad5064_read_raw(
struct iio_dev *indio_dev,
239 scale_uv = ad5064_get_vref(st, chan);
243 scale_uv = (scale_uv * 100) >> chan->
scan_type.realbits;
244 *val = scale_uv / 100000;
245 *val2 = (scale_uv % 100000) * 10;
253 static int ad5064_write_raw(
struct iio_dev *indio_dev,
261 if (val > (1 << chan->
scan_type.realbits) || val < 0)
278 static const struct iio_info ad5064_info = {
279 .read_raw = ad5064_read_raw,
280 .write_raw = ad5064_write_raw,
287 .read = ad5064_read_dac_powerdown,
288 .write = ad5064_write_dac_powerdown,
290 IIO_ENUM(
"powerdown_mode",
false, &ad5064_powerdown_mode_enum),
295 #define AD5064_CHANNEL(chan, bits) { \
296 .type = IIO_VOLTAGE, \
300 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
301 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
302 .address = AD5064_ADDR_DAC(chan), \
303 .scan_type = IIO_ST('u', (bits), 16, 20 - (bits)), \
304 .ext_info = ad5064_ext_info, \
307 #define DECLARE_AD5064_CHANNELS(name, bits) \
308 const struct iio_chan_spec name[] = { \
309 AD5064_CHANNEL(0, bits), \
310 AD5064_CHANNEL(1, bits), \
311 AD5064_CHANNEL(2, bits), \
312 AD5064_CHANNEL(3, bits), \
313 AD5064_CHANNEL(4, bits), \
314 AD5064_CHANNEL(5, bits), \
315 AD5064_CHANNEL(6, bits), \
316 AD5064_CHANNEL(7, bits), \
325 .shared_vref =
false,
326 .channels = ad5024_channels,
330 .shared_vref =
false,
331 .channels = ad5024_channels,
335 .shared_vref =
false,
336 .channels = ad5044_channels,
340 .shared_vref =
false,
341 .channels = ad5044_channels,
345 .shared_vref =
false,
346 .channels = ad5064_channels,
351 .channels = ad5064_channels,
355 .shared_vref =
false,
356 .channels = ad5064_channels,
361 .internal_vref = 2500000,
362 .channels = ad5024_channels,
367 .internal_vref = 5000000,
368 .channels = ad5024_channels,
373 .internal_vref = 2500000,
374 .channels = ad5044_channels,
379 .internal_vref = 5000000,
380 .channels = ad5044_channels,
385 .internal_vref = 2500000,
386 .channels = ad5064_channels,
391 .internal_vref = 5000000,
392 .channels = ad5064_channels,
397 .internal_vref = 2500000,
398 .channels = ad5064_channels,
403 .internal_vref = 5000000,
404 .channels = ad5064_channels,
409 static inline unsigned int ad5064_num_vref(
struct ad5064_state *st)
414 static const char *
const ad5064_vref_names[] = {
421 static const char *
const ad5064_vref_name(
struct ad5064_state *st,
424 return st->
chip_info->shared_vref ?
"vref" : ad5064_vref_names[vref];
436 if (indio_dev ==
NULL)
439 st = iio_priv(indio_dev);
446 for (i = 0; i < ad5064_num_vref(st); ++
i)
447 st->
vref_reg[i].supply = ad5064_vref_name(st, i);
458 dev_err(dev,
"Failed to enable internal vref: %d\n",
468 for (i = 0; i < st->
chip_info->num_channels; ++
i) {
473 indio_dev->
dev.parent =
dev;
475 indio_dev->
info = &ad5064_info;
482 goto error_disable_reg;
515 #if IS_ENABLED(CONFIG_SPI_MASTER)
518 unsigned int addr,
unsigned int val)
523 return spi_write(spi, &st->data.
spi,
sizeof(st->data.
spi));
530 return ad5064_probe(&spi->
dev, id->driver_data, id->
name,
536 return ad5064_remove(&spi->
dev);
560 static struct spi_driver ad5064_spi_driver = {
565 .probe = ad5064_spi_probe,
567 .id_table = ad5064_spi_ids,
570 static int __init ad5064_spi_register_driver(
void)
575 static void ad5064_spi_unregister_driver(
void)
577 spi_unregister_driver(&ad5064_spi_driver);
582 static inline int ad5064_spi_register_driver(
void) {
return 0; }
583 static inline void ad5064_spi_unregister_driver(
void) { }
587 #if IS_ENABLED(CONFIG_I2C)
589 static int ad5064_i2c_write(
struct ad5064_state *st,
unsigned int cmd,
590 unsigned int addr,
unsigned int val)
594 st->data.
i2c[0] = (cmd << 4) | addr;
595 put_unaligned_be16(val, &st->data.
i2c[1]);
602 return ad5064_probe(&i2c->
dev, id->driver_data, id->
name,
608 return ad5064_remove(&i2c->
dev);
622 static struct i2c_driver ad5064_i2c_driver = {
627 .probe = ad5064_i2c_probe,
629 .id_table = ad5064_i2c_ids,
632 static int __init ad5064_i2c_register_driver(
void)
634 return i2c_add_driver(&ad5064_i2c_driver);
637 static void __exit ad5064_i2c_unregister_driver(
void)
644 static inline int ad5064_i2c_register_driver(
void) {
return 0; }
645 static inline void ad5064_i2c_unregister_driver(
void) { }
649 static int __init ad5064_init(
void)
653 ret = ad5064_spi_register_driver();
657 ret = ad5064_i2c_register_driver();
659 ad5064_spi_unregister_driver();
667 static void __exit ad5064_exit(
void)
669 ad5064_i2c_unregister_driver();
670 ad5064_spi_unregister_driver();