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
40 struct ad5380_chip_info
{
41 struct iio_chan_spec channel_template
;
42 unsigned int num_channels
;
43 unsigned int int_vref
;
47 * struct ad5380_state - driver instance specific data
48 * @regmap: regmap instance used by the device
49 * @chip_info: chip model specific constants, available modes etc
50 * @vref: actual reference voltage used in uA
51 * @pwr_down: whether the chip is currently in power down mode
52 * @lock: lock to protect the data buffer during regmap ops
55 struct regmap
*regmap
;
56 const struct ad5380_chip_info
*chip_info
;
79 static ssize_t
ad5380_read_dac_powerdown(struct iio_dev
*indio_dev
,
80 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
82 struct ad5380_state
*st
= iio_priv(indio_dev
);
84 return sysfs_emit(buf
, "%d\n", st
->pwr_down
);
87 static ssize_t
ad5380_write_dac_powerdown(struct iio_dev
*indio_dev
,
88 uintptr_t private, const struct iio_chan_spec
*chan
, const char *buf
,
91 struct ad5380_state
*st
= iio_priv(indio_dev
);
95 ret
= kstrtobool(buf
, &pwr_down
);
99 mutex_lock(&st
->lock
);
102 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_PWR_DOWN
, 0);
104 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_PWR_UP
, 0);
106 st
->pwr_down
= pwr_down
;
108 mutex_unlock(&st
->lock
);
110 return ret
? ret
: len
;
113 static const char * const ad5380_powerdown_modes
[] = {
118 static int ad5380_get_powerdown_mode(struct iio_dev
*indio_dev
,
119 const struct iio_chan_spec
*chan
)
121 struct ad5380_state
*st
= iio_priv(indio_dev
);
125 ret
= regmap_read(st
->regmap
, AD5380_REG_SF_CTRL
, &mode
);
129 mode
= (mode
>> AD5380_CTRL_PWR_DOWN_MODE_OFFSET
) & 1;
134 static int ad5380_set_powerdown_mode(struct iio_dev
*indio_dev
,
135 const struct iio_chan_spec
*chan
, unsigned int mode
)
137 struct ad5380_state
*st
= iio_priv(indio_dev
);
140 ret
= regmap_update_bits(st
->regmap
, AD5380_REG_SF_CTRL
,
141 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET
,
142 mode
<< AD5380_CTRL_PWR_DOWN_MODE_OFFSET
);
147 static const struct iio_enum ad5380_powerdown_mode_enum
= {
148 .items
= ad5380_powerdown_modes
,
149 .num_items
= ARRAY_SIZE(ad5380_powerdown_modes
),
150 .get
= ad5380_get_powerdown_mode
,
151 .set
= ad5380_set_powerdown_mode
,
154 static unsigned int ad5380_info_to_reg(struct iio_chan_spec
const *chan
,
158 case IIO_CHAN_INFO_RAW
:
159 return AD5380_REG_DATA(chan
->address
);
160 case IIO_CHAN_INFO_CALIBBIAS
:
161 return AD5380_REG_OFFSET(chan
->address
);
162 case IIO_CHAN_INFO_CALIBSCALE
:
163 return AD5380_REG_GAIN(chan
->address
);
171 static int ad5380_write_raw(struct iio_dev
*indio_dev
,
172 struct iio_chan_spec
const *chan
, int val
, int val2
, long info
)
174 const unsigned int max_val
= (1 << chan
->scan_type
.realbits
);
175 struct ad5380_state
*st
= iio_priv(indio_dev
);
178 case IIO_CHAN_INFO_RAW
:
179 case IIO_CHAN_INFO_CALIBSCALE
:
180 if (val
>= max_val
|| val
< 0)
183 return regmap_write(st
->regmap
,
184 ad5380_info_to_reg(chan
, info
),
185 val
<< chan
->scan_type
.shift
);
186 case IIO_CHAN_INFO_CALIBBIAS
:
187 val
+= (1 << chan
->scan_type
.realbits
) / 2;
188 if (val
>= max_val
|| val
< 0)
191 return regmap_write(st
->regmap
,
192 AD5380_REG_OFFSET(chan
->address
),
193 val
<< chan
->scan_type
.shift
);
200 static int ad5380_read_raw(struct iio_dev
*indio_dev
,
201 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
203 struct ad5380_state
*st
= iio_priv(indio_dev
);
207 case IIO_CHAN_INFO_RAW
:
208 case IIO_CHAN_INFO_CALIBSCALE
:
209 ret
= regmap_read(st
->regmap
, ad5380_info_to_reg(chan
, info
),
213 *val
>>= chan
->scan_type
.shift
;
215 case IIO_CHAN_INFO_CALIBBIAS
:
216 ret
= regmap_read(st
->regmap
, AD5380_REG_OFFSET(chan
->address
),
220 *val
>>= chan
->scan_type
.shift
;
221 *val
-= (1 << chan
->scan_type
.realbits
) / 2;
223 case IIO_CHAN_INFO_SCALE
:
225 *val2
= chan
->scan_type
.realbits
;
226 return IIO_VAL_FRACTIONAL_LOG2
;
234 static const struct iio_info ad5380_info
= {
235 .read_raw
= ad5380_read_raw
,
236 .write_raw
= ad5380_write_raw
,
239 static const struct iio_chan_spec_ext_info ad5380_ext_info
[] = {
242 .read
= ad5380_read_dac_powerdown
,
243 .write
= ad5380_write_dac_powerdown
,
244 .shared
= IIO_SEPARATE
,
246 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE
,
247 &ad5380_powerdown_mode_enum
),
248 IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE
, &ad5380_powerdown_mode_enum
),
252 #define AD5380_CHANNEL(_bits) { \
253 .type = IIO_VOLTAGE, \
256 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
257 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
258 BIT(IIO_CHAN_INFO_CALIBBIAS), \
259 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
262 .realbits = (_bits), \
264 .shift = 14 - (_bits), \
266 .ext_info = ad5380_ext_info, \
269 static const struct ad5380_chip_info ad5380_chip_info_tbl
[] = {
271 .channel_template
= AD5380_CHANNEL(14),
276 .channel_template
= AD5380_CHANNEL(14),
281 .channel_template
= AD5380_CHANNEL(12),
286 .channel_template
= AD5380_CHANNEL(12),
291 .channel_template
= AD5380_CHANNEL(14),
296 .channel_template
= AD5380_CHANNEL(14),
301 .channel_template
= AD5380_CHANNEL(12),
306 .channel_template
= AD5380_CHANNEL(12),
311 .channel_template
= AD5380_CHANNEL(14),
316 .channel_template
= AD5380_CHANNEL(14),
321 .channel_template
= AD5380_CHANNEL(12),
326 .channel_template
= AD5380_CHANNEL(12),
331 .channel_template
= AD5380_CHANNEL(14),
336 .channel_template
= AD5380_CHANNEL(14),
342 static int ad5380_alloc_channels(struct device
*dev
, struct iio_dev
*indio_dev
)
344 struct ad5380_state
*st
= iio_priv(indio_dev
);
345 struct iio_chan_spec
*channels
;
348 channels
= devm_kcalloc(dev
, st
->chip_info
->num_channels
,
349 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
354 for (i
= 0; i
< st
->chip_info
->num_channels
; ++i
) {
355 channels
[i
] = st
->chip_info
->channel_template
;
356 channels
[i
].channel
= i
;
357 channels
[i
].address
= i
;
360 indio_dev
->channels
= channels
;
365 static int ad5380_probe(struct device
*dev
, struct regmap
*regmap
,
366 enum ad5380_type type
, const char *name
)
368 struct iio_dev
*indio_dev
;
369 struct ad5380_state
*st
;
370 unsigned int ctrl
= 0;
373 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
374 if (indio_dev
== NULL
) {
375 dev_err(dev
, "Failed to allocate iio device\n");
379 st
= iio_priv(indio_dev
);
381 st
->chip_info
= &ad5380_chip_info_tbl
[type
];
384 indio_dev
->name
= name
;
385 indio_dev
->info
= &ad5380_info
;
386 indio_dev
->modes
= INDIO_DIRECT_MODE
;
387 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
389 mutex_init(&st
->lock
);
391 ret
= ad5380_alloc_channels(dev
, indio_dev
);
393 return dev_err_probe(dev
, ret
, "Failed to allocate channel spec\n");
395 if (st
->chip_info
->int_vref
== 2500)
396 ctrl
|= AD5380_CTRL_INT_VREF_2V5
;
398 ret
= devm_regulator_get_enable_read_voltage(dev
, "vref");
399 if (ret
< 0 && ret
!= -ENODEV
)
400 return dev_err_probe(dev
, ret
, "Failed to get vref voltage\n");
401 if (ret
== -ENODEV
) {
402 st
->vref
= st
->chip_info
->int_vref
;
403 ctrl
|= AD5380_CTRL_INT_VREF_EN
;
405 st
->vref
= ret
/ 1000;
408 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_CTRL
, ctrl
);
410 return dev_err_probe(dev
, ret
, "Failed to write to device\n");
412 ret
= devm_iio_device_register(dev
, indio_dev
);
414 return dev_err_probe(dev
, ret
, "Failed to register iio device\n");
419 static bool ad5380_reg_false(struct device
*dev
, unsigned int reg
)
424 static const struct regmap_config ad5380_regmap_config
= {
428 .max_register
= AD5380_REG_DATA(40),
429 .cache_type
= REGCACHE_RBTREE
,
431 .volatile_reg
= ad5380_reg_false
,
432 .readable_reg
= ad5380_reg_false
,
435 #if IS_ENABLED(CONFIG_SPI_MASTER)
437 static int ad5380_spi_probe(struct spi_device
*spi
)
439 const struct spi_device_id
*id
= spi_get_device_id(spi
);
440 struct regmap
*regmap
;
442 regmap
= devm_regmap_init_spi(spi
, &ad5380_regmap_config
);
445 return PTR_ERR(regmap
);
447 return ad5380_probe(&spi
->dev
, regmap
, id
->driver_data
, id
->name
);
450 static const struct spi_device_id ad5380_spi_ids
[] = {
451 { "ad5380-3", ID_AD5380_3
},
452 { "ad5380-5", ID_AD5380_5
},
453 { "ad5381-3", ID_AD5381_3
},
454 { "ad5381-5", ID_AD5381_5
},
455 { "ad5382-3", ID_AD5382_3
},
456 { "ad5382-5", ID_AD5382_5
},
457 { "ad5383-3", ID_AD5383_3
},
458 { "ad5383-5", ID_AD5383_5
},
459 { "ad5384-3", ID_AD5380_3
},
460 { "ad5384-5", ID_AD5380_5
},
461 { "ad5390-3", ID_AD5390_3
},
462 { "ad5390-5", ID_AD5390_5
},
463 { "ad5391-3", ID_AD5391_3
},
464 { "ad5391-5", ID_AD5391_5
},
465 { "ad5392-3", ID_AD5392_3
},
466 { "ad5392-5", ID_AD5392_5
},
469 MODULE_DEVICE_TABLE(spi
, ad5380_spi_ids
);
471 static struct spi_driver ad5380_spi_driver
= {
475 .probe
= ad5380_spi_probe
,
476 .id_table
= ad5380_spi_ids
,
479 static inline int ad5380_spi_register_driver(void)
481 return spi_register_driver(&ad5380_spi_driver
);
484 static inline void ad5380_spi_unregister_driver(void)
486 spi_unregister_driver(&ad5380_spi_driver
);
491 static inline int ad5380_spi_register_driver(void)
496 static inline void ad5380_spi_unregister_driver(void)
502 #if IS_ENABLED(CONFIG_I2C)
504 static int ad5380_i2c_probe(struct i2c_client
*i2c
)
506 const struct i2c_device_id
*id
= i2c_client_get_device_id(i2c
);
507 struct regmap
*regmap
;
509 regmap
= devm_regmap_init_i2c(i2c
, &ad5380_regmap_config
);
512 return PTR_ERR(regmap
);
514 return ad5380_probe(&i2c
->dev
, regmap
, id
->driver_data
, id
->name
);
517 static const struct i2c_device_id ad5380_i2c_ids
[] = {
518 { "ad5380-3", ID_AD5380_3
},
519 { "ad5380-5", ID_AD5380_5
},
520 { "ad5381-3", ID_AD5381_3
},
521 { "ad5381-5", ID_AD5381_5
},
522 { "ad5382-3", ID_AD5382_3
},
523 { "ad5382-5", ID_AD5382_5
},
524 { "ad5383-3", ID_AD5383_3
},
525 { "ad5383-5", ID_AD5383_5
},
526 { "ad5384-3", ID_AD5380_3
},
527 { "ad5384-5", ID_AD5380_5
},
528 { "ad5390-3", ID_AD5390_3
},
529 { "ad5390-5", ID_AD5390_5
},
530 { "ad5391-3", ID_AD5391_3
},
531 { "ad5391-5", ID_AD5391_5
},
532 { "ad5392-3", ID_AD5392_3
},
533 { "ad5392-5", ID_AD5392_5
},
536 MODULE_DEVICE_TABLE(i2c
, ad5380_i2c_ids
);
538 static struct i2c_driver ad5380_i2c_driver
= {
542 .probe
= ad5380_i2c_probe
,
543 .id_table
= ad5380_i2c_ids
,
546 static inline int ad5380_i2c_register_driver(void)
548 return i2c_add_driver(&ad5380_i2c_driver
);
551 static inline void ad5380_i2c_unregister_driver(void)
553 i2c_del_driver(&ad5380_i2c_driver
);
558 static inline int ad5380_i2c_register_driver(void)
563 static inline void ad5380_i2c_unregister_driver(void)
569 static int __init
ad5380_spi_init(void)
573 ret
= ad5380_spi_register_driver();
577 ret
= ad5380_i2c_register_driver();
579 ad5380_spi_unregister_driver();
585 module_init(ad5380_spi_init
);
587 static void __exit
ad5380_spi_exit(void)
589 ad5380_i2c_unregister_driver();
590 ad5380_spi_unregister_driver();
593 module_exit(ad5380_spi_exit
);
595 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
596 MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
597 MODULE_LICENSE("GPL v2");