2 * Analog devices AD5380, AD5381, AD5382, AD5383, AD5390, AD5391, AD5392
3 * multi-channel Digital to Analog Converters driver
5 * Copyright 2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 #define AD5380_REG_DATA(x) (((x) << 2) | 3)
25 #define AD5380_REG_OFFSET(x) (((x) << 2) | 2)
26 #define AD5380_REG_GAIN(x) (((x) << 2) | 1)
27 #define AD5380_REG_SF_PWR_DOWN (8 << 2)
28 #define AD5380_REG_SF_PWR_UP (9 << 2)
29 #define AD5380_REG_SF_CTRL (12 << 2)
31 #define AD5380_CTRL_PWR_DOWN_MODE_OFFSET 13
32 #define AD5380_CTRL_INT_VREF_2V5 BIT(12)
33 #define AD5380_CTRL_INT_VREF_EN BIT(10)
36 * struct ad5380_chip_info - chip specific information
37 * @channel_template: channel specification template
38 * @num_channels: number of channels
39 * @int_vref: internal vref in uV
42 struct ad5380_chip_info
{
43 struct iio_chan_spec channel_template
;
44 unsigned int num_channels
;
45 unsigned int int_vref
;
49 * struct ad5380_state - driver instance specific data
50 * @regmap: regmap instance used by the device
51 * @chip_info: chip model specific constants, available modes etc
52 * @vref_reg: vref supply regulator
53 * @vref: actual reference voltage used in uA
54 * @pwr_down: whether the chip is currently in power down mode
58 struct regmap
*regmap
;
59 const struct ad5380_chip_info
*chip_info
;
60 struct regulator
*vref_reg
;
82 static ssize_t
ad5380_read_dac_powerdown(struct iio_dev
*indio_dev
,
83 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
85 struct ad5380_state
*st
= iio_priv(indio_dev
);
87 return sprintf(buf
, "%d\n", st
->pwr_down
);
90 static ssize_t
ad5380_write_dac_powerdown(struct iio_dev
*indio_dev
,
91 uintptr_t private, const struct iio_chan_spec
*chan
, const char *buf
,
94 struct ad5380_state
*st
= iio_priv(indio_dev
);
98 ret
= strtobool(buf
, &pwr_down
);
102 mutex_lock(&indio_dev
->mlock
);
105 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_PWR_DOWN
, 0);
107 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_PWR_UP
, 0);
109 st
->pwr_down
= pwr_down
;
111 mutex_unlock(&indio_dev
->mlock
);
113 return ret
? ret
: len
;
116 static const char * const ad5380_powerdown_modes
[] = {
121 static int ad5380_get_powerdown_mode(struct iio_dev
*indio_dev
,
122 const struct iio_chan_spec
*chan
)
124 struct ad5380_state
*st
= iio_priv(indio_dev
);
128 ret
= regmap_read(st
->regmap
, AD5380_REG_SF_CTRL
, &mode
);
132 mode
= (mode
>> AD5380_CTRL_PWR_DOWN_MODE_OFFSET
) & 1;
137 static int ad5380_set_powerdown_mode(struct iio_dev
*indio_dev
,
138 const struct iio_chan_spec
*chan
, unsigned int mode
)
140 struct ad5380_state
*st
= iio_priv(indio_dev
);
143 ret
= regmap_update_bits(st
->regmap
, AD5380_REG_SF_CTRL
,
144 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET
,
145 mode
<< AD5380_CTRL_PWR_DOWN_MODE_OFFSET
);
150 static const struct iio_enum ad5380_powerdown_mode_enum
= {
151 .items
= ad5380_powerdown_modes
,
152 .num_items
= ARRAY_SIZE(ad5380_powerdown_modes
),
153 .get
= ad5380_get_powerdown_mode
,
154 .set
= ad5380_set_powerdown_mode
,
157 static unsigned int ad5380_info_to_reg(struct iio_chan_spec
const *chan
,
162 return AD5380_REG_DATA(chan
->address
);
163 case IIO_CHAN_INFO_CALIBBIAS
:
164 return AD5380_REG_OFFSET(chan
->address
);
165 case IIO_CHAN_INFO_CALIBSCALE
:
166 return AD5380_REG_GAIN(chan
->address
);
174 static int ad5380_write_raw(struct iio_dev
*indio_dev
,
175 struct iio_chan_spec
const *chan
, int val
, int val2
, long info
)
177 const unsigned int max_val
= (1 << chan
->scan_type
.realbits
);
178 struct ad5380_state
*st
= iio_priv(indio_dev
);
181 case IIO_CHAN_INFO_RAW
:
182 case IIO_CHAN_INFO_CALIBSCALE
:
183 if (val
>= max_val
|| val
< 0)
186 return regmap_write(st
->regmap
,
187 ad5380_info_to_reg(chan
, info
),
188 val
<< chan
->scan_type
.shift
);
189 case IIO_CHAN_INFO_CALIBBIAS
:
190 val
+= (1 << chan
->scan_type
.realbits
) / 2;
191 if (val
>= max_val
|| val
< 0)
194 return regmap_write(st
->regmap
,
195 AD5380_REG_OFFSET(chan
->address
),
196 val
<< chan
->scan_type
.shift
);
203 static int ad5380_read_raw(struct iio_dev
*indio_dev
,
204 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
206 struct ad5380_state
*st
= iio_priv(indio_dev
);
210 case IIO_CHAN_INFO_RAW
:
211 case IIO_CHAN_INFO_CALIBSCALE
:
212 ret
= regmap_read(st
->regmap
, ad5380_info_to_reg(chan
, info
),
216 *val
>>= chan
->scan_type
.shift
;
218 case IIO_CHAN_INFO_CALIBBIAS
:
219 ret
= regmap_read(st
->regmap
, AD5380_REG_OFFSET(chan
->address
),
223 *val
>>= chan
->scan_type
.shift
;
224 val
-= (1 << chan
->scan_type
.realbits
) / 2;
226 case IIO_CHAN_INFO_SCALE
:
228 *val2
= chan
->scan_type
.realbits
;
229 return IIO_VAL_FRACTIONAL_LOG2
;
237 static const struct iio_info ad5380_info
= {
238 .read_raw
= ad5380_read_raw
,
239 .write_raw
= ad5380_write_raw
,
240 .driver_module
= THIS_MODULE
,
243 static struct iio_chan_spec_ext_info ad5380_ext_info
[] = {
246 .read
= ad5380_read_dac_powerdown
,
247 .write
= ad5380_write_dac_powerdown
,
248 .shared
= IIO_SEPARATE
,
250 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE
,
251 &ad5380_powerdown_mode_enum
),
252 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5380_powerdown_mode_enum
),
256 #define AD5380_CHANNEL(_bits) { \
257 .type = IIO_VOLTAGE, \
260 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
261 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
262 BIT(IIO_CHAN_INFO_CALIBBIAS), \
263 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
266 .realbits = (_bits), \
268 .shift = 14 - (_bits), \
270 .ext_info = ad5380_ext_info, \
273 static const struct ad5380_chip_info ad5380_chip_info_tbl
[] = {
275 .channel_template
= AD5380_CHANNEL(14),
280 .channel_template
= AD5380_CHANNEL(14),
285 .channel_template
= AD5380_CHANNEL(12),
290 .channel_template
= AD5380_CHANNEL(12),
295 .channel_template
= AD5380_CHANNEL(14),
300 .channel_template
= AD5380_CHANNEL(14),
305 .channel_template
= AD5380_CHANNEL(12),
310 .channel_template
= AD5380_CHANNEL(12),
315 .channel_template
= AD5380_CHANNEL(14),
320 .channel_template
= AD5380_CHANNEL(14),
325 .channel_template
= AD5380_CHANNEL(12),
330 .channel_template
= AD5380_CHANNEL(12),
335 .channel_template
= AD5380_CHANNEL(14),
340 .channel_template
= AD5380_CHANNEL(14),
346 static int ad5380_alloc_channels(struct iio_dev
*indio_dev
)
348 struct ad5380_state
*st
= iio_priv(indio_dev
);
349 struct iio_chan_spec
*channels
;
352 channels
= kcalloc(st
->chip_info
->num_channels
,
353 sizeof(struct iio_chan_spec
), GFP_KERNEL
);
358 for (i
= 0; i
< st
->chip_info
->num_channels
; ++i
) {
359 channels
[i
] = st
->chip_info
->channel_template
;
360 channels
[i
].channel
= i
;
361 channels
[i
].address
= i
;
364 indio_dev
->channels
= channels
;
369 static int ad5380_probe(struct device
*dev
, struct regmap
*regmap
,
370 enum ad5380_type type
, const char *name
)
372 struct iio_dev
*indio_dev
;
373 struct ad5380_state
*st
;
374 unsigned int ctrl
= 0;
377 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
378 if (indio_dev
== NULL
) {
379 dev_err(dev
, "Failed to allocate iio device\n");
383 st
= iio_priv(indio_dev
);
384 dev_set_drvdata(dev
, indio_dev
);
386 st
->chip_info
= &ad5380_chip_info_tbl
[type
];
389 indio_dev
->dev
.parent
= dev
;
390 indio_dev
->name
= name
;
391 indio_dev
->info
= &ad5380_info
;
392 indio_dev
->modes
= INDIO_DIRECT_MODE
;
393 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
395 ret
= ad5380_alloc_channels(indio_dev
);
397 dev_err(dev
, "Failed to allocate channel spec: %d\n", ret
);
401 if (st
->chip_info
->int_vref
== 2500)
402 ctrl
|= AD5380_CTRL_INT_VREF_2V5
;
404 st
->vref_reg
= devm_regulator_get(dev
, "vref");
405 if (!IS_ERR(st
->vref_reg
)) {
406 ret
= regulator_enable(st
->vref_reg
);
408 dev_err(dev
, "Failed to enable vref regulators: %d\n",
413 ret
= regulator_get_voltage(st
->vref_reg
);
415 goto error_disable_reg
;
417 st
->vref
= ret
/ 1000;
419 st
->vref
= st
->chip_info
->int_vref
;
420 ctrl
|= AD5380_CTRL_INT_VREF_EN
;
423 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_CTRL
, ctrl
);
425 dev_err(dev
, "Failed to write to device: %d\n", ret
);
426 goto error_disable_reg
;
429 ret
= iio_device_register(indio_dev
);
431 dev_err(dev
, "Failed to register iio device: %d\n", ret
);
432 goto error_disable_reg
;
438 if (!IS_ERR(st
->vref_reg
))
439 regulator_disable(st
->vref_reg
);
441 kfree(indio_dev
->channels
);
446 static int ad5380_remove(struct device
*dev
)
448 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
449 struct ad5380_state
*st
= iio_priv(indio_dev
);
451 iio_device_unregister(indio_dev
);
453 kfree(indio_dev
->channels
);
455 if (!IS_ERR(st
->vref_reg
)) {
456 regulator_disable(st
->vref_reg
);
462 static bool ad5380_reg_false(struct device
*dev
, unsigned int reg
)
467 static const struct regmap_config ad5380_regmap_config
= {
471 .max_register
= AD5380_REG_DATA(40),
472 .cache_type
= REGCACHE_RBTREE
,
474 .volatile_reg
= ad5380_reg_false
,
475 .readable_reg
= ad5380_reg_false
,
478 #if IS_ENABLED(CONFIG_SPI_MASTER)
480 static int ad5380_spi_probe(struct spi_device
*spi
)
482 const struct spi_device_id
*id
= spi_get_device_id(spi
);
483 struct regmap
*regmap
;
485 regmap
= devm_regmap_init_spi(spi
, &ad5380_regmap_config
);
488 return PTR_ERR(regmap
);
490 return ad5380_probe(&spi
->dev
, regmap
, id
->driver_data
, id
->name
);
493 static int ad5380_spi_remove(struct spi_device
*spi
)
495 return ad5380_remove(&spi
->dev
);
498 static const struct spi_device_id ad5380_spi_ids
[] = {
499 { "ad5380-3", ID_AD5380_3
},
500 { "ad5380-5", ID_AD5380_5
},
501 { "ad5381-3", ID_AD5381_3
},
502 { "ad5381-5", ID_AD5381_5
},
503 { "ad5382-3", ID_AD5382_3
},
504 { "ad5382-5", ID_AD5382_5
},
505 { "ad5383-3", ID_AD5383_3
},
506 { "ad5383-5", ID_AD5383_5
},
507 { "ad5384-3", ID_AD5380_3
},
508 { "ad5384-5", ID_AD5380_5
},
509 { "ad5390-3", ID_AD5390_3
},
510 { "ad5390-5", ID_AD5390_5
},
511 { "ad5391-3", ID_AD5391_3
},
512 { "ad5391-5", ID_AD5391_5
},
513 { "ad5392-3", ID_AD5392_3
},
514 { "ad5392-5", ID_AD5392_5
},
517 MODULE_DEVICE_TABLE(spi
, ad5380_spi_ids
);
519 static struct spi_driver ad5380_spi_driver
= {
522 .owner
= THIS_MODULE
,
524 .probe
= ad5380_spi_probe
,
525 .remove
= ad5380_spi_remove
,
526 .id_table
= ad5380_spi_ids
,
529 static inline int ad5380_spi_register_driver(void)
531 return spi_register_driver(&ad5380_spi_driver
);
534 static inline void ad5380_spi_unregister_driver(void)
536 spi_unregister_driver(&ad5380_spi_driver
);
541 static inline int ad5380_spi_register_driver(void)
546 static inline void ad5380_spi_unregister_driver(void)
552 #if IS_ENABLED(CONFIG_I2C)
554 static int ad5380_i2c_probe(struct i2c_client
*i2c
,
555 const struct i2c_device_id
*id
)
557 struct regmap
*regmap
;
559 regmap
= devm_regmap_init_i2c(i2c
, &ad5380_regmap_config
);
562 return PTR_ERR(regmap
);
564 return ad5380_probe(&i2c
->dev
, regmap
, id
->driver_data
, id
->name
);
567 static int ad5380_i2c_remove(struct i2c_client
*i2c
)
569 return ad5380_remove(&i2c
->dev
);
572 static const struct i2c_device_id ad5380_i2c_ids
[] = {
573 { "ad5380-3", ID_AD5380_3
},
574 { "ad5380-5", ID_AD5380_5
},
575 { "ad5381-3", ID_AD5381_3
},
576 { "ad5381-5", ID_AD5381_5
},
577 { "ad5382-3", ID_AD5382_3
},
578 { "ad5382-5", ID_AD5382_5
},
579 { "ad5383-3", ID_AD5383_3
},
580 { "ad5383-5", ID_AD5383_5
},
581 { "ad5384-3", ID_AD5380_3
},
582 { "ad5384-5", ID_AD5380_5
},
583 { "ad5390-3", ID_AD5390_3
},
584 { "ad5390-5", ID_AD5390_5
},
585 { "ad5391-3", ID_AD5391_3
},
586 { "ad5391-5", ID_AD5391_5
},
587 { "ad5392-3", ID_AD5392_3
},
588 { "ad5392-5", ID_AD5392_5
},
591 MODULE_DEVICE_TABLE(i2c
, ad5380_i2c_ids
);
593 static struct i2c_driver ad5380_i2c_driver
= {
596 .owner
= THIS_MODULE
,
598 .probe
= ad5380_i2c_probe
,
599 .remove
= ad5380_i2c_remove
,
600 .id_table
= ad5380_i2c_ids
,
603 static inline int ad5380_i2c_register_driver(void)
605 return i2c_add_driver(&ad5380_i2c_driver
);
608 static inline void ad5380_i2c_unregister_driver(void)
610 i2c_del_driver(&ad5380_i2c_driver
);
615 static inline int ad5380_i2c_register_driver(void)
620 static inline void ad5380_i2c_unregister_driver(void)
626 static int __init
ad5380_spi_init(void)
630 ret
= ad5380_spi_register_driver();
634 ret
= ad5380_i2c_register_driver();
636 ad5380_spi_unregister_driver();
642 module_init(ad5380_spi_init
);
644 static void __exit
ad5380_spi_exit(void)
646 ad5380_i2c_unregister_driver();
647 ad5380_spi_unregister_driver();
650 module_exit(ad5380_spi_exit
);
652 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
653 MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
654 MODULE_LICENSE("GPL v2");