1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog devices AD5380, AD5381, AD5382, AD5383, AD5390, AD5391, AD5392
4 * multi-channel Digital to Analog Converters driver
6 * Copyright 2011 Analog Devices Inc.
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
23 #define AD5380_REG_DATA(x) (((x) << 2) | 3)
24 #define AD5380_REG_OFFSET(x) (((x) << 2) | 2)
25 #define AD5380_REG_GAIN(x) (((x) << 2) | 1)
26 #define AD5380_REG_SF_PWR_DOWN (8 << 2)
27 #define AD5380_REG_SF_PWR_UP (9 << 2)
28 #define AD5380_REG_SF_CTRL (12 << 2)
30 #define AD5380_CTRL_PWR_DOWN_MODE_OFFSET 13
31 #define AD5380_CTRL_INT_VREF_2V5 BIT(12)
32 #define AD5380_CTRL_INT_VREF_EN BIT(10)
35 * struct ad5380_chip_info - chip specific information
36 * @channel_template: channel specification template
37 * @num_channels: number of channels
38 * @int_vref: internal vref in uV
41 struct ad5380_chip_info
{
42 struct iio_chan_spec channel_template
;
43 unsigned int num_channels
;
44 unsigned int int_vref
;
48 * struct ad5380_state - driver instance specific data
49 * @regmap: regmap instance used by the device
50 * @chip_info: chip model specific constants, available modes etc
51 * @vref_reg: vref supply regulator
52 * @vref: actual reference voltage used in uA
53 * @pwr_down: whether the chip is currently in power down mode
57 struct regmap
*regmap
;
58 const struct ad5380_chip_info
*chip_info
;
59 struct regulator
*vref_reg
;
81 static ssize_t
ad5380_read_dac_powerdown(struct iio_dev
*indio_dev
,
82 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
84 struct ad5380_state
*st
= iio_priv(indio_dev
);
86 return sprintf(buf
, "%d\n", st
->pwr_down
);
89 static ssize_t
ad5380_write_dac_powerdown(struct iio_dev
*indio_dev
,
90 uintptr_t private, const struct iio_chan_spec
*chan
, const char *buf
,
93 struct ad5380_state
*st
= iio_priv(indio_dev
);
97 ret
= strtobool(buf
, &pwr_down
);
101 mutex_lock(&indio_dev
->mlock
);
104 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_PWR_DOWN
, 0);
106 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_PWR_UP
, 0);
108 st
->pwr_down
= pwr_down
;
110 mutex_unlock(&indio_dev
->mlock
);
112 return ret
? ret
: len
;
115 static const char * const ad5380_powerdown_modes
[] = {
120 static int ad5380_get_powerdown_mode(struct iio_dev
*indio_dev
,
121 const struct iio_chan_spec
*chan
)
123 struct ad5380_state
*st
= iio_priv(indio_dev
);
127 ret
= regmap_read(st
->regmap
, AD5380_REG_SF_CTRL
, &mode
);
131 mode
= (mode
>> AD5380_CTRL_PWR_DOWN_MODE_OFFSET
) & 1;
136 static int ad5380_set_powerdown_mode(struct iio_dev
*indio_dev
,
137 const struct iio_chan_spec
*chan
, unsigned int mode
)
139 struct ad5380_state
*st
= iio_priv(indio_dev
);
142 ret
= regmap_update_bits(st
->regmap
, AD5380_REG_SF_CTRL
,
143 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET
,
144 mode
<< AD5380_CTRL_PWR_DOWN_MODE_OFFSET
);
149 static const struct iio_enum ad5380_powerdown_mode_enum
= {
150 .items
= ad5380_powerdown_modes
,
151 .num_items
= ARRAY_SIZE(ad5380_powerdown_modes
),
152 .get
= ad5380_get_powerdown_mode
,
153 .set
= ad5380_set_powerdown_mode
,
156 static unsigned int ad5380_info_to_reg(struct iio_chan_spec
const *chan
,
160 case IIO_CHAN_INFO_RAW
:
161 return AD5380_REG_DATA(chan
->address
);
162 case IIO_CHAN_INFO_CALIBBIAS
:
163 return AD5380_REG_OFFSET(chan
->address
);
164 case IIO_CHAN_INFO_CALIBSCALE
:
165 return AD5380_REG_GAIN(chan
->address
);
173 static int ad5380_write_raw(struct iio_dev
*indio_dev
,
174 struct iio_chan_spec
const *chan
, int val
, int val2
, long info
)
176 const unsigned int max_val
= (1 << chan
->scan_type
.realbits
);
177 struct ad5380_state
*st
= iio_priv(indio_dev
);
180 case IIO_CHAN_INFO_RAW
:
181 case IIO_CHAN_INFO_CALIBSCALE
:
182 if (val
>= max_val
|| val
< 0)
185 return regmap_write(st
->regmap
,
186 ad5380_info_to_reg(chan
, info
),
187 val
<< chan
->scan_type
.shift
);
188 case IIO_CHAN_INFO_CALIBBIAS
:
189 val
+= (1 << chan
->scan_type
.realbits
) / 2;
190 if (val
>= max_val
|| val
< 0)
193 return regmap_write(st
->regmap
,
194 AD5380_REG_OFFSET(chan
->address
),
195 val
<< chan
->scan_type
.shift
);
202 static int ad5380_read_raw(struct iio_dev
*indio_dev
,
203 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
205 struct ad5380_state
*st
= iio_priv(indio_dev
);
209 case IIO_CHAN_INFO_RAW
:
210 case IIO_CHAN_INFO_CALIBSCALE
:
211 ret
= regmap_read(st
->regmap
, ad5380_info_to_reg(chan
, info
),
215 *val
>>= chan
->scan_type
.shift
;
217 case IIO_CHAN_INFO_CALIBBIAS
:
218 ret
= regmap_read(st
->regmap
, AD5380_REG_OFFSET(chan
->address
),
222 *val
>>= chan
->scan_type
.shift
;
223 *val
-= (1 << chan
->scan_type
.realbits
) / 2;
225 case IIO_CHAN_INFO_SCALE
:
227 *val2
= chan
->scan_type
.realbits
;
228 return IIO_VAL_FRACTIONAL_LOG2
;
236 static const struct iio_info ad5380_info
= {
237 .read_raw
= ad5380_read_raw
,
238 .write_raw
= ad5380_write_raw
,
241 static struct iio_chan_spec_ext_info ad5380_ext_info
[] = {
244 .read
= ad5380_read_dac_powerdown
,
245 .write
= ad5380_write_dac_powerdown
,
246 .shared
= IIO_SEPARATE
,
248 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE
,
249 &ad5380_powerdown_mode_enum
),
250 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5380_powerdown_mode_enum
),
254 #define AD5380_CHANNEL(_bits) { \
255 .type = IIO_VOLTAGE, \
258 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
259 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
260 BIT(IIO_CHAN_INFO_CALIBBIAS), \
261 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
264 .realbits = (_bits), \
266 .shift = 14 - (_bits), \
268 .ext_info = ad5380_ext_info, \
271 static const struct ad5380_chip_info ad5380_chip_info_tbl
[] = {
273 .channel_template
= AD5380_CHANNEL(14),
278 .channel_template
= AD5380_CHANNEL(14),
283 .channel_template
= AD5380_CHANNEL(12),
288 .channel_template
= AD5380_CHANNEL(12),
293 .channel_template
= AD5380_CHANNEL(14),
298 .channel_template
= AD5380_CHANNEL(14),
303 .channel_template
= AD5380_CHANNEL(12),
308 .channel_template
= AD5380_CHANNEL(12),
313 .channel_template
= AD5380_CHANNEL(14),
318 .channel_template
= AD5380_CHANNEL(14),
323 .channel_template
= AD5380_CHANNEL(12),
328 .channel_template
= AD5380_CHANNEL(12),
333 .channel_template
= AD5380_CHANNEL(14),
338 .channel_template
= AD5380_CHANNEL(14),
344 static int ad5380_alloc_channels(struct iio_dev
*indio_dev
)
346 struct ad5380_state
*st
= iio_priv(indio_dev
);
347 struct iio_chan_spec
*channels
;
350 channels
= kcalloc(st
->chip_info
->num_channels
,
351 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
356 for (i
= 0; i
< st
->chip_info
->num_channels
; ++i
) {
357 channels
[i
] = st
->chip_info
->channel_template
;
358 channels
[i
].channel
= i
;
359 channels
[i
].address
= i
;
362 indio_dev
->channels
= channels
;
367 static int ad5380_probe(struct device
*dev
, struct regmap
*regmap
,
368 enum ad5380_type type
, const char *name
)
370 struct iio_dev
*indio_dev
;
371 struct ad5380_state
*st
;
372 unsigned int ctrl
= 0;
375 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
376 if (indio_dev
== NULL
) {
377 dev_err(dev
, "Failed to allocate iio device\n");
381 st
= iio_priv(indio_dev
);
382 dev_set_drvdata(dev
, indio_dev
);
384 st
->chip_info
= &ad5380_chip_info_tbl
[type
];
387 indio_dev
->dev
.parent
= dev
;
388 indio_dev
->name
= name
;
389 indio_dev
->info
= &ad5380_info
;
390 indio_dev
->modes
= INDIO_DIRECT_MODE
;
391 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
393 ret
= ad5380_alloc_channels(indio_dev
);
395 dev_err(dev
, "Failed to allocate channel spec: %d\n", ret
);
399 if (st
->chip_info
->int_vref
== 2500)
400 ctrl
|= AD5380_CTRL_INT_VREF_2V5
;
402 st
->vref_reg
= devm_regulator_get(dev
, "vref");
403 if (!IS_ERR(st
->vref_reg
)) {
404 ret
= regulator_enable(st
->vref_reg
);
406 dev_err(dev
, "Failed to enable vref regulators: %d\n",
411 ret
= regulator_get_voltage(st
->vref_reg
);
413 goto error_disable_reg
;
415 st
->vref
= ret
/ 1000;
417 st
->vref
= st
->chip_info
->int_vref
;
418 ctrl
|= AD5380_CTRL_INT_VREF_EN
;
421 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_CTRL
, ctrl
);
423 dev_err(dev
, "Failed to write to device: %d\n", ret
);
424 goto error_disable_reg
;
427 ret
= iio_device_register(indio_dev
);
429 dev_err(dev
, "Failed to register iio device: %d\n", ret
);
430 goto error_disable_reg
;
436 if (!IS_ERR(st
->vref_reg
))
437 regulator_disable(st
->vref_reg
);
439 kfree(indio_dev
->channels
);
444 static int ad5380_remove(struct device
*dev
)
446 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
447 struct ad5380_state
*st
= iio_priv(indio_dev
);
449 iio_device_unregister(indio_dev
);
451 kfree(indio_dev
->channels
);
453 if (!IS_ERR(st
->vref_reg
)) {
454 regulator_disable(st
->vref_reg
);
460 static bool ad5380_reg_false(struct device
*dev
, unsigned int reg
)
465 static const struct regmap_config ad5380_regmap_config
= {
469 .max_register
= AD5380_REG_DATA(40),
470 .cache_type
= REGCACHE_RBTREE
,
472 .volatile_reg
= ad5380_reg_false
,
473 .readable_reg
= ad5380_reg_false
,
476 #if IS_ENABLED(CONFIG_SPI_MASTER)
478 static int ad5380_spi_probe(struct spi_device
*spi
)
480 const struct spi_device_id
*id
= spi_get_device_id(spi
);
481 struct regmap
*regmap
;
483 regmap
= devm_regmap_init_spi(spi
, &ad5380_regmap_config
);
486 return PTR_ERR(regmap
);
488 return ad5380_probe(&spi
->dev
, regmap
, id
->driver_data
, id
->name
);
491 static int ad5380_spi_remove(struct spi_device
*spi
)
493 return ad5380_remove(&spi
->dev
);
496 static const struct spi_device_id ad5380_spi_ids
[] = {
497 { "ad5380-3", ID_AD5380_3
},
498 { "ad5380-5", ID_AD5380_5
},
499 { "ad5381-3", ID_AD5381_3
},
500 { "ad5381-5", ID_AD5381_5
},
501 { "ad5382-3", ID_AD5382_3
},
502 { "ad5382-5", ID_AD5382_5
},
503 { "ad5383-3", ID_AD5383_3
},
504 { "ad5383-5", ID_AD5383_5
},
505 { "ad5384-3", ID_AD5380_3
},
506 { "ad5384-5", ID_AD5380_5
},
507 { "ad5390-3", ID_AD5390_3
},
508 { "ad5390-5", ID_AD5390_5
},
509 { "ad5391-3", ID_AD5391_3
},
510 { "ad5391-5", ID_AD5391_5
},
511 { "ad5392-3", ID_AD5392_3
},
512 { "ad5392-5", ID_AD5392_5
},
515 MODULE_DEVICE_TABLE(spi
, ad5380_spi_ids
);
517 static struct spi_driver ad5380_spi_driver
= {
521 .probe
= ad5380_spi_probe
,
522 .remove
= ad5380_spi_remove
,
523 .id_table
= ad5380_spi_ids
,
526 static inline int ad5380_spi_register_driver(void)
528 return spi_register_driver(&ad5380_spi_driver
);
531 static inline void ad5380_spi_unregister_driver(void)
533 spi_unregister_driver(&ad5380_spi_driver
);
538 static inline int ad5380_spi_register_driver(void)
543 static inline void ad5380_spi_unregister_driver(void)
549 #if IS_ENABLED(CONFIG_I2C)
551 static int ad5380_i2c_probe(struct i2c_client
*i2c
,
552 const struct i2c_device_id
*id
)
554 struct regmap
*regmap
;
556 regmap
= devm_regmap_init_i2c(i2c
, &ad5380_regmap_config
);
559 return PTR_ERR(regmap
);
561 return ad5380_probe(&i2c
->dev
, regmap
, id
->driver_data
, id
->name
);
564 static int ad5380_i2c_remove(struct i2c_client
*i2c
)
566 return ad5380_remove(&i2c
->dev
);
569 static const struct i2c_device_id ad5380_i2c_ids
[] = {
570 { "ad5380-3", ID_AD5380_3
},
571 { "ad5380-5", ID_AD5380_5
},
572 { "ad5381-3", ID_AD5381_3
},
573 { "ad5381-5", ID_AD5381_5
},
574 { "ad5382-3", ID_AD5382_3
},
575 { "ad5382-5", ID_AD5382_5
},
576 { "ad5383-3", ID_AD5383_3
},
577 { "ad5383-5", ID_AD5383_5
},
578 { "ad5384-3", ID_AD5380_3
},
579 { "ad5384-5", ID_AD5380_5
},
580 { "ad5390-3", ID_AD5390_3
},
581 { "ad5390-5", ID_AD5390_5
},
582 { "ad5391-3", ID_AD5391_3
},
583 { "ad5391-5", ID_AD5391_5
},
584 { "ad5392-3", ID_AD5392_3
},
585 { "ad5392-5", ID_AD5392_5
},
588 MODULE_DEVICE_TABLE(i2c
, ad5380_i2c_ids
);
590 static struct i2c_driver ad5380_i2c_driver
= {
594 .probe
= ad5380_i2c_probe
,
595 .remove
= ad5380_i2c_remove
,
596 .id_table
= ad5380_i2c_ids
,
599 static inline int ad5380_i2c_register_driver(void)
601 return i2c_add_driver(&ad5380_i2c_driver
);
604 static inline void ad5380_i2c_unregister_driver(void)
606 i2c_del_driver(&ad5380_i2c_driver
);
611 static inline int ad5380_i2c_register_driver(void)
616 static inline void ad5380_i2c_unregister_driver(void)
622 static int __init
ad5380_spi_init(void)
626 ret
= ad5380_spi_register_driver();
630 ret
= ad5380_i2c_register_driver();
632 ad5380_spi_unregister_driver();
638 module_init(ad5380_spi_init
);
640 static void __exit
ad5380_spi_exit(void)
642 ad5380_i2c_unregister_driver();
643 ad5380_spi_unregister_driver();
646 module_exit(ad5380_spi_exit
);
648 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
649 MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
650 MODULE_LICENSE("GPL v2");