10 #include <linux/device.h>
12 #include <linux/i2c.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/slab.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)
82 static ssize_t ad5380_read_dac_powerdown(
struct iio_dev *indio_dev,
90 static ssize_t ad5380_write_dac_powerdown(
struct iio_dev *indio_dev,
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,
137 static int ad5380_set_powerdown_mode(
struct iio_dev *indio_dev,
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,
174 static int ad5380_write_raw(
struct iio_dev *indio_dev,
177 const unsigned int max_val = (1 << chan->
scan_type.realbits);
183 if (val >= max_val || val < 0)
187 ad5380_info_to_reg(chan, info),
190 val += (1 << chan->
scan_type.realbits) / 2;
191 if (val >= max_val || val < 0)
203 static int ad5380_read_raw(
struct iio_dev *indio_dev,
204 struct iio_chan_spec const *chan,
int *val,
int *val2,
long info)
207 unsigned long scale_uv;
225 val -= (1 << chan->
scan_type.realbits) / 2;
228 scale_uv = ((2 * st->
vref) >> chan->
scan_type.realbits) * 100;
229 *val = scale_uv / 100000;
230 *val2 = (scale_uv % 100000) * 10;
239 static const struct iio_info ad5380_info = {
240 .read_raw = ad5380_read_raw,
241 .write_raw = ad5380_write_raw,
248 .read = ad5380_read_dac_powerdown,
249 .write = ad5380_write_dac_powerdown,
251 IIO_ENUM(
"powerdown_mode",
true, &ad5380_powerdown_mode_enum),
256 #define AD5380_CHANNEL(_bits) { \
257 .type = IIO_VOLTAGE, \
260 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
261 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
262 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
263 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT, \
264 .scan_type = IIO_ST('u', (_bits), 16, 14 - (_bits)), \
265 .ext_info = ad5380_ext_info, \
347 channels = kcalloc(st->
chip_info->num_channels,
353 for (i = 0; i < st->
chip_info->num_channels; ++
i) {
354 channels[
i] = st->
chip_info->channel_template;
369 unsigned int ctrl = 0;
373 if (indio_dev ==
NULL) {
374 dev_err(dev,
"Failed to allocate iio device\n");
379 st = iio_priv(indio_dev);
385 indio_dev->
dev.parent =
dev;
387 indio_dev->
info = &ad5380_info;
391 ret = ad5380_alloc_channels(indio_dev);
393 dev_err(dev,
"Failed to allocate channel spec: %d\n", ret);
404 dev_err(dev,
"Failed to enable vref regulators: %d\n",
417 dev_err(dev,
"Failed to write to device: %d\n", ret);
418 goto error_disable_reg;
423 dev_err(dev,
"Failed to register iio device: %d\n", ret);
424 goto error_disable_reg;
463 static bool ad5380_reg_false(
struct device *dev,
unsigned int reg)
468 static const struct regmap_config ad5380_regmap_config = {
475 .volatile_reg = ad5380_reg_false,
476 .readable_reg = ad5380_reg_false,
479 #if IS_ENABLED(CONFIG_SPI_MASTER)
484 struct regmap *regmap;
489 return PTR_ERR(regmap);
491 return ad5380_probe(&spi->
dev, regmap, id->driver_data, id->
name);
496 return ad5380_remove(&spi->
dev);
520 static struct spi_driver ad5380_spi_driver = {
525 .probe = ad5380_spi_probe,
527 .id_table = ad5380_spi_ids,
530 static inline int ad5380_spi_register_driver(
void)
535 static inline void ad5380_spi_unregister_driver(
void)
537 spi_unregister_driver(&ad5380_spi_driver);
542 static inline int ad5380_spi_register_driver(
void)
547 static inline void ad5380_spi_unregister_driver(
void)
553 #if IS_ENABLED(CONFIG_I2C)
558 struct regmap *regmap;
563 return PTR_ERR(regmap);
565 return ad5380_probe(&i2c->
dev, regmap, id->driver_data, id->
name);
570 return ad5380_remove(&i2c->
dev);
594 static struct i2c_driver ad5380_i2c_driver = {
599 .probe = ad5380_i2c_probe,
601 .id_table = ad5380_i2c_ids,
604 static inline int ad5380_i2c_register_driver(
void)
606 return i2c_add_driver(&ad5380_i2c_driver);
609 static inline void ad5380_i2c_unregister_driver(
void)
616 static inline int ad5380_i2c_register_driver(
void)
621 static inline void ad5380_i2c_unregister_driver(
void)
627 static int __init ad5380_spi_init(
void)
631 ret = ad5380_spi_register_driver();
635 ret = ad5380_i2c_register_driver();
637 ad5380_spi_unregister_driver();
645 static void __exit ad5380_spi_exit(
void)
647 ad5380_i2c_unregister_driver();
648 ad5380_spi_unregister_driver();