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>
26 #define AD5380_REG_DATA(x) (((x) << 2) | 3)
27 #define AD5380_REG_OFFSET(x) (((x) << 2) | 2)
28 #define AD5380_REG_GAIN(x) (((x) << 2) | 1)
29 #define AD5380_REG_SF_PWR_DOWN (8 << 2)
30 #define AD5380_REG_SF_PWR_UP (9 << 2)
31 #define AD5380_REG_SF_CTRL (12 << 2)
33 #define AD5380_CTRL_PWR_DOWN_MODE_OFFSET 13
34 #define AD5380_CTRL_INT_VREF_2V5 BIT(12)
35 #define AD5380_CTRL_INT_VREF_EN BIT(10)
38 * struct ad5380_chip_info - chip specific information
39 * @channel_template: channel specification template
40 * @num_channels: number of channels
41 * @int_vref: internal vref in uV
44 struct ad5380_chip_info
{
45 struct iio_chan_spec channel_template
;
46 unsigned int num_channels
;
47 unsigned int int_vref
;
51 * struct ad5380_state - driver instance specific data
52 * @regmap: regmap instance used by the device
53 * @chip_info: chip model specific constants, available modes etc
54 * @vref_reg: vref supply regulator
55 * @vref: actual reference voltage used in uA
56 * @pwr_down: whether the chip is currently in power down mode
60 struct regmap
*regmap
;
61 const struct ad5380_chip_info
*chip_info
;
62 struct regulator
*vref_reg
;
84 #define AD5380_CHANNEL(_bits) { \
85 .type = IIO_VOLTAGE, \
88 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT | \
89 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
90 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT, \
91 .scan_type = IIO_ST('u', (_bits), 16, 14 - (_bits)) \
94 static const struct ad5380_chip_info ad5380_chip_info_tbl
[] = {
96 .channel_template
= AD5380_CHANNEL(14),
101 .channel_template
= AD5380_CHANNEL(14),
106 .channel_template
= AD5380_CHANNEL(12),
111 .channel_template
= AD5380_CHANNEL(12),
116 .channel_template
= AD5380_CHANNEL(14),
121 .channel_template
= AD5380_CHANNEL(14),
126 .channel_template
= AD5380_CHANNEL(12),
131 .channel_template
= AD5380_CHANNEL(12),
136 .channel_template
= AD5380_CHANNEL(14),
141 .channel_template
= AD5380_CHANNEL(14),
146 .channel_template
= AD5380_CHANNEL(12),
151 .channel_template
= AD5380_CHANNEL(12),
156 .channel_template
= AD5380_CHANNEL(14),
161 .channel_template
= AD5380_CHANNEL(14),
167 static ssize_t
ad5380_read_dac_powerdown(struct device
*dev
,
168 struct device_attribute
*attr
, char *buf
)
170 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
171 struct ad5380_state
*st
= iio_priv(indio_dev
);
173 return sprintf(buf
, "%d\n", st
->pwr_down
);
176 static ssize_t
ad5380_write_dac_powerdown(struct device
*dev
,
177 struct device_attribute
*attr
, const char *buf
, size_t len
)
179 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
180 struct ad5380_state
*st
= iio_priv(indio_dev
);
184 ret
= strtobool(buf
, &pwr_down
);
188 mutex_lock(&indio_dev
->mlock
);
191 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_PWR_DOWN
, 0);
193 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_PWR_UP
, 0);
195 st
->pwr_down
= pwr_down
;
197 mutex_unlock(&indio_dev
->mlock
);
199 return ret
? ret
: len
;
202 static IIO_DEVICE_ATTR(out_voltage_powerdown
,
204 ad5380_read_dac_powerdown
,
205 ad5380_write_dac_powerdown
, 0);
207 static const char ad5380_powerdown_modes
[][15] = {
208 [0] = "100kohm_to_gnd",
212 static ssize_t
ad5380_read_powerdown_mode(struct device
*dev
,
213 struct device_attribute
*attr
, char *buf
)
215 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
216 struct ad5380_state
*st
= iio_priv(indio_dev
);
220 ret
= regmap_read(st
->regmap
, AD5380_REG_SF_CTRL
, &mode
);
224 mode
= (mode
>> AD5380_CTRL_PWR_DOWN_MODE_OFFSET
) & 1;
226 return sprintf(buf
, "%s\n", ad5380_powerdown_modes
[mode
]);
229 static ssize_t
ad5380_write_powerdown_mode(struct device
*dev
,
230 struct device_attribute
*attr
, const char *buf
, size_t len
)
232 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
233 struct ad5380_state
*st
= iio_priv(indio_dev
);
237 for (i
= 0; i
< ARRAY_SIZE(ad5380_powerdown_modes
); ++i
) {
238 if (sysfs_streq(buf
, ad5380_powerdown_modes
[i
]))
242 if (i
== ARRAY_SIZE(ad5380_powerdown_modes
))
245 ret
= regmap_update_bits(st
->regmap
, AD5380_REG_SF_CTRL
,
246 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET
,
247 i
<< AD5380_CTRL_PWR_DOWN_MODE_OFFSET
);
249 return ret
? ret
: len
;
252 static IIO_DEVICE_ATTR(out_voltage_powerdown_mode
,
254 ad5380_read_powerdown_mode
,
255 ad5380_write_powerdown_mode
, 0);
257 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available
,
258 "100kohm_to_gnd three_state");
260 static struct attribute
*ad5380_attributes
[] = {
261 &iio_dev_attr_out_voltage_powerdown
.dev_attr
.attr
,
262 &iio_dev_attr_out_voltage_powerdown_mode
.dev_attr
.attr
,
263 &iio_const_attr_out_voltage_powerdown_mode_available
.dev_attr
.attr
,
267 static const struct attribute_group ad5380_attribute_group
= {
268 .attrs
= ad5380_attributes
,
271 static unsigned int ad5380_info_to_reg(struct iio_chan_spec
const *chan
,
276 return AD5380_REG_DATA(chan
->address
);
277 case IIO_CHAN_INFO_CALIBBIAS
:
278 return AD5380_REG_OFFSET(chan
->address
);
279 case IIO_CHAN_INFO_CALIBSCALE
:
280 return AD5380_REG_GAIN(chan
->address
);
288 static int ad5380_write_raw(struct iio_dev
*indio_dev
,
289 struct iio_chan_spec
const *chan
, int val
, int val2
, long info
)
291 const unsigned int max_val
= (1 << chan
->scan_type
.realbits
);
292 struct ad5380_state
*st
= iio_priv(indio_dev
);
296 case IIO_CHAN_INFO_CALIBSCALE
:
297 if (val
>= max_val
|| val
< 0)
300 return regmap_write(st
->regmap
,
301 ad5380_info_to_reg(chan
, info
),
302 val
<< chan
->scan_type
.shift
);
303 case IIO_CHAN_INFO_CALIBBIAS
:
304 val
+= (1 << chan
->scan_type
.realbits
) / 2;
305 if (val
>= max_val
|| val
< 0)
308 return regmap_write(st
->regmap
,
309 AD5380_REG_OFFSET(chan
->address
),
310 val
<< chan
->scan_type
.shift
);
317 static int ad5380_read_raw(struct iio_dev
*indio_dev
,
318 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
320 struct ad5380_state
*st
= iio_priv(indio_dev
);
321 unsigned long scale_uv
;
326 case IIO_CHAN_INFO_CALIBSCALE
:
327 ret
= regmap_read(st
->regmap
, ad5380_info_to_reg(chan
, info
),
331 *val
>>= chan
->scan_type
.shift
;
333 case IIO_CHAN_INFO_CALIBBIAS
:
334 ret
= regmap_read(st
->regmap
, AD5380_REG_OFFSET(chan
->address
),
338 *val
>>= chan
->scan_type
.shift
;
339 val
-= (1 << chan
->scan_type
.realbits
) / 2;
341 case IIO_CHAN_INFO_SCALE
:
342 scale_uv
= ((2 * st
->vref
) >> chan
->scan_type
.realbits
) * 100;
343 *val
= scale_uv
/ 100000;
344 *val2
= (scale_uv
% 100000) * 10;
345 return IIO_VAL_INT_PLUS_MICRO
;
353 static const struct iio_info ad5380_info
= {
354 .read_raw
= ad5380_read_raw
,
355 .write_raw
= ad5380_write_raw
,
356 .attrs
= &ad5380_attribute_group
,
357 .driver_module
= THIS_MODULE
,
360 static int __devinit
ad5380_alloc_channels(struct iio_dev
*indio_dev
)
362 struct ad5380_state
*st
= iio_priv(indio_dev
);
363 struct iio_chan_spec
*channels
;
366 channels
= kcalloc(sizeof(struct iio_chan_spec
),
367 st
->chip_info
->num_channels
, GFP_KERNEL
);
372 for (i
= 0; i
< st
->chip_info
->num_channels
; ++i
) {
373 channels
[i
] = st
->chip_info
->channel_template
;
374 channels
[i
].channel
= i
;
375 channels
[i
].address
= i
;
378 indio_dev
->channels
= channels
;
383 static int __devinit
ad5380_probe(struct device
*dev
, struct regmap
*regmap
,
384 enum ad5380_type type
, const char *name
)
386 struct iio_dev
*indio_dev
;
387 struct ad5380_state
*st
;
388 unsigned int ctrl
= 0;
391 indio_dev
= iio_allocate_device(sizeof(*st
));
392 if (indio_dev
== NULL
) {
393 dev_err(dev
, "Failed to allocate iio device\n");
395 goto error_regmap_exit
;
398 st
= iio_priv(indio_dev
);
399 dev_set_drvdata(dev
, indio_dev
);
401 st
->chip_info
= &ad5380_chip_info_tbl
[type
];
404 indio_dev
->dev
.parent
= dev
;
405 indio_dev
->name
= name
;
406 indio_dev
->info
= &ad5380_info
;
407 indio_dev
->modes
= INDIO_DIRECT_MODE
;
408 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
410 ret
= ad5380_alloc_channels(indio_dev
);
412 dev_err(dev
, "Failed to allocate channel spec: %d\n", ret
);
416 if (st
->chip_info
->int_vref
== 2500000)
417 ctrl
|= AD5380_CTRL_INT_VREF_2V5
;
419 st
->vref_reg
= regulator_get(dev
, "vref");
420 if (!IS_ERR(st
->vref_reg
)) {
421 ret
= regulator_enable(st
->vref_reg
);
423 dev_err(dev
, "Failed to enable vref regulators: %d\n",
428 st
->vref
= regulator_get_voltage(st
->vref_reg
);
430 st
->vref
= st
->chip_info
->int_vref
;
431 ctrl
|= AD5380_CTRL_INT_VREF_EN
;
434 ret
= regmap_write(st
->regmap
, AD5380_REG_SF_CTRL
, ctrl
);
436 dev_err(dev
, "Failed to write to device: %d\n", ret
);
437 goto error_disable_reg
;
440 ret
= iio_device_register(indio_dev
);
442 dev_err(dev
, "Failed to register iio device: %d\n", ret
);
443 goto error_disable_reg
;
449 if (!IS_ERR(st
->vref_reg
))
450 regulator_disable(st
->vref_reg
);
452 if (!IS_ERR(st
->vref_reg
))
453 regulator_put(st
->vref_reg
);
455 kfree(indio_dev
->channels
);
457 iio_free_device(indio_dev
);
464 static int __devexit
ad5380_remove(struct device
*dev
)
466 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
467 struct ad5380_state
*st
= iio_priv(indio_dev
);
469 iio_device_unregister(indio_dev
);
471 kfree(indio_dev
->channels
);
473 if (!IS_ERR(st
->vref_reg
)) {
474 regulator_disable(st
->vref_reg
);
475 regulator_put(st
->vref_reg
);
478 regmap_exit(st
->regmap
);
479 iio_free_device(indio_dev
);
484 static bool ad5380_reg_false(struct device
*dev
, unsigned int reg
)
489 static const struct regmap_config ad5380_regmap_config
= {
493 .max_register
= AD5380_REG_DATA(40),
494 .cache_type
= REGCACHE_RBTREE
,
496 .volatile_reg
= ad5380_reg_false
,
497 .readable_reg
= ad5380_reg_false
,
500 #if IS_ENABLED(CONFIG_SPI_MASTER)
502 static int __devinit
ad5380_spi_probe(struct spi_device
*spi
)
504 const struct spi_device_id
*id
= spi_get_device_id(spi
);
505 struct regmap
*regmap
;
507 regmap
= regmap_init_spi(spi
, &ad5380_regmap_config
);
510 return PTR_ERR(regmap
);
512 return ad5380_probe(&spi
->dev
, regmap
, id
->driver_data
, id
->name
);
515 static int __devexit
ad5380_spi_remove(struct spi_device
*spi
)
517 return ad5380_remove(&spi
->dev
);
520 static const struct spi_device_id ad5380_spi_ids
[] = {
521 { "ad5380-3", ID_AD5380_3
},
522 { "ad5380-5", ID_AD5380_5
},
523 { "ad5381-3", ID_AD5381_3
},
524 { "ad5381-5", ID_AD5381_5
},
525 { "ad5382-3", ID_AD5382_3
},
526 { "ad5382-5", ID_AD5382_5
},
527 { "ad5383-3", ID_AD5383_3
},
528 { "ad5383-5", ID_AD5383_5
},
529 { "ad5384-3", ID_AD5380_3
},
530 { "ad5384-5", ID_AD5380_5
},
531 { "ad5390-3", ID_AD5390_3
},
532 { "ad5390-5", ID_AD5390_5
},
533 { "ad5391-3", ID_AD5391_3
},
534 { "ad5391-5", ID_AD5391_5
},
535 { "ad5392-3", ID_AD5392_3
},
536 { "ad5392-5", ID_AD5392_5
},
539 MODULE_DEVICE_TABLE(spi
, ad5380_spi_ids
);
541 static struct spi_driver ad5380_spi_driver
= {
544 .owner
= THIS_MODULE
,
546 .probe
= ad5380_spi_probe
,
547 .remove
= __devexit_p(ad5380_spi_remove
),
548 .id_table
= ad5380_spi_ids
,
551 static inline int ad5380_spi_register_driver(void)
553 return spi_register_driver(&ad5380_spi_driver
);
556 static inline void ad5380_spi_unregister_driver(void)
558 spi_unregister_driver(&ad5380_spi_driver
);
563 static inline int ad5380_spi_register_driver(void)
568 static inline void ad5380_spi_unregister_driver(void)
574 #if IS_ENABLED(CONFIG_I2C)
576 static int __devinit
ad5380_i2c_probe(struct i2c_client
*i2c
,
577 const struct i2c_device_id
*id
)
579 struct regmap
*regmap
;
581 regmap
= regmap_init_i2c(i2c
, &ad5380_regmap_config
);
584 return PTR_ERR(regmap
);
586 return ad5380_probe(&i2c
->dev
, regmap
, id
->driver_data
, id
->name
);
589 static int __devexit
ad5380_i2c_remove(struct i2c_client
*i2c
)
591 return ad5380_remove(&i2c
->dev
);
594 static const struct i2c_device_id ad5380_i2c_ids
[] = {
595 { "ad5380-3", ID_AD5380_3
},
596 { "ad5380-5", ID_AD5380_5
},
597 { "ad5381-3", ID_AD5381_3
},
598 { "ad5381-5", ID_AD5381_5
},
599 { "ad5382-3", ID_AD5382_3
},
600 { "ad5382-5", ID_AD5382_5
},
601 { "ad5383-3", ID_AD5383_3
},
602 { "ad5383-5", ID_AD5383_5
},
603 { "ad5384-3", ID_AD5380_3
},
604 { "ad5384-5", ID_AD5380_5
},
605 { "ad5390-3", ID_AD5390_3
},
606 { "ad5390-5", ID_AD5390_5
},
607 { "ad5391-3", ID_AD5391_3
},
608 { "ad5391-5", ID_AD5391_5
},
609 { "ad5392-3", ID_AD5392_3
},
610 { "ad5392-5", ID_AD5392_5
},
613 MODULE_DEVICE_TABLE(i2c
, ad5380_i2c_ids
);
615 static struct i2c_driver ad5380_i2c_driver
= {
618 .owner
= THIS_MODULE
,
620 .probe
= ad5380_i2c_probe
,
621 .remove
= __devexit_p(ad5380_i2c_remove
),
622 .id_table
= ad5380_i2c_ids
,
625 static inline int ad5380_i2c_register_driver(void)
627 return i2c_add_driver(&ad5380_i2c_driver
);
630 static inline void ad5380_i2c_unregister_driver(void)
632 i2c_del_driver(&ad5380_i2c_driver
);
637 static inline int ad5380_i2c_register_driver(void)
642 static inline void ad5380_i2c_unregister_driver(void)
648 static int __init
ad5380_spi_init(void)
652 ret
= ad5380_spi_register_driver();
656 ret
= ad5380_i2c_register_driver();
658 ad5380_spi_unregister_driver();
664 module_init(ad5380_spi_init
);
666 static void __exit
ad5380_spi_exit(void)
668 ad5380_i2c_unregister_driver();
669 ad5380_spi_unregister_driver();
672 module_exit(ad5380_spi_exit
);
674 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
675 MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
676 MODULE_LICENSE("GPL v2");