WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / dac / ad5380.c
blob37ef653564b0a29d62e0f05b1beba1ee0ba3070b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Analog devices AD5380, AD5381, AD5382, AD5383, AD5390, AD5391, AD5392
4 * multi-channel Digital to Analog Converters driver
6 * Copyright 2011 Analog Devices Inc.
7 */
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)
34 /**
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;
47 /**
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
54 * @lock: lock to protect the data buffer during regmap ops
57 struct ad5380_state {
58 struct regmap *regmap;
59 const struct ad5380_chip_info *chip_info;
60 struct regulator *vref_reg;
61 int vref;
62 bool pwr_down;
63 struct mutex lock;
66 enum ad5380_type {
67 ID_AD5380_3,
68 ID_AD5380_5,
69 ID_AD5381_3,
70 ID_AD5381_5,
71 ID_AD5382_3,
72 ID_AD5382_5,
73 ID_AD5383_3,
74 ID_AD5383_5,
75 ID_AD5390_3,
76 ID_AD5390_5,
77 ID_AD5391_3,
78 ID_AD5391_5,
79 ID_AD5392_3,
80 ID_AD5392_5,
83 static ssize_t ad5380_read_dac_powerdown(struct iio_dev *indio_dev,
84 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
86 struct ad5380_state *st = iio_priv(indio_dev);
88 return sprintf(buf, "%d\n", st->pwr_down);
91 static ssize_t ad5380_write_dac_powerdown(struct iio_dev *indio_dev,
92 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
93 size_t len)
95 struct ad5380_state *st = iio_priv(indio_dev);
96 bool pwr_down;
97 int ret;
99 ret = strtobool(buf, &pwr_down);
100 if (ret)
101 return ret;
103 mutex_lock(&st->lock);
105 if (pwr_down)
106 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_DOWN, 0);
107 else
108 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_UP, 0);
110 st->pwr_down = pwr_down;
112 mutex_unlock(&st->lock);
114 return ret ? ret : len;
117 static const char * const ad5380_powerdown_modes[] = {
118 "100kohm_to_gnd",
119 "three_state",
122 static int ad5380_get_powerdown_mode(struct iio_dev *indio_dev,
123 const struct iio_chan_spec *chan)
125 struct ad5380_state *st = iio_priv(indio_dev);
126 unsigned int mode;
127 int ret;
129 ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
130 if (ret)
131 return ret;
133 mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1;
135 return mode;
138 static int ad5380_set_powerdown_mode(struct iio_dev *indio_dev,
139 const struct iio_chan_spec *chan, unsigned int mode)
141 struct ad5380_state *st = iio_priv(indio_dev);
142 int ret;
144 ret = regmap_update_bits(st->regmap, AD5380_REG_SF_CTRL,
145 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET,
146 mode << AD5380_CTRL_PWR_DOWN_MODE_OFFSET);
148 return ret;
151 static const struct iio_enum ad5380_powerdown_mode_enum = {
152 .items = ad5380_powerdown_modes,
153 .num_items = ARRAY_SIZE(ad5380_powerdown_modes),
154 .get = ad5380_get_powerdown_mode,
155 .set = ad5380_set_powerdown_mode,
158 static unsigned int ad5380_info_to_reg(struct iio_chan_spec const *chan,
159 long info)
161 switch (info) {
162 case IIO_CHAN_INFO_RAW:
163 return AD5380_REG_DATA(chan->address);
164 case IIO_CHAN_INFO_CALIBBIAS:
165 return AD5380_REG_OFFSET(chan->address);
166 case IIO_CHAN_INFO_CALIBSCALE:
167 return AD5380_REG_GAIN(chan->address);
168 default:
169 break;
172 return 0;
175 static int ad5380_write_raw(struct iio_dev *indio_dev,
176 struct iio_chan_spec const *chan, int val, int val2, long info)
178 const unsigned int max_val = (1 << chan->scan_type.realbits);
179 struct ad5380_state *st = iio_priv(indio_dev);
181 switch (info) {
182 case IIO_CHAN_INFO_RAW:
183 case IIO_CHAN_INFO_CALIBSCALE:
184 if (val >= max_val || val < 0)
185 return -EINVAL;
187 return regmap_write(st->regmap,
188 ad5380_info_to_reg(chan, info),
189 val << chan->scan_type.shift);
190 case IIO_CHAN_INFO_CALIBBIAS:
191 val += (1 << chan->scan_type.realbits) / 2;
192 if (val >= max_val || val < 0)
193 return -EINVAL;
195 return regmap_write(st->regmap,
196 AD5380_REG_OFFSET(chan->address),
197 val << chan->scan_type.shift);
198 default:
199 break;
201 return -EINVAL;
204 static int ad5380_read_raw(struct iio_dev *indio_dev,
205 struct iio_chan_spec const *chan, int *val, int *val2, long info)
207 struct ad5380_state *st = iio_priv(indio_dev);
208 int ret;
210 switch (info) {
211 case IIO_CHAN_INFO_RAW:
212 case IIO_CHAN_INFO_CALIBSCALE:
213 ret = regmap_read(st->regmap, ad5380_info_to_reg(chan, info),
214 val);
215 if (ret)
216 return ret;
217 *val >>= chan->scan_type.shift;
218 return IIO_VAL_INT;
219 case IIO_CHAN_INFO_CALIBBIAS:
220 ret = regmap_read(st->regmap, AD5380_REG_OFFSET(chan->address),
221 val);
222 if (ret)
223 return ret;
224 *val >>= chan->scan_type.shift;
225 *val -= (1 << chan->scan_type.realbits) / 2;
226 return IIO_VAL_INT;
227 case IIO_CHAN_INFO_SCALE:
228 *val = 2 * st->vref;
229 *val2 = chan->scan_type.realbits;
230 return IIO_VAL_FRACTIONAL_LOG2;
231 default:
232 break;
235 return -EINVAL;
238 static const struct iio_info ad5380_info = {
239 .read_raw = ad5380_read_raw,
240 .write_raw = ad5380_write_raw,
243 static const struct iio_chan_spec_ext_info ad5380_ext_info[] = {
245 .name = "powerdown",
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),
253 { },
256 #define AD5380_CHANNEL(_bits) { \
257 .type = IIO_VOLTAGE, \
258 .indexed = 1, \
259 .output = 1, \
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), \
264 .scan_type = { \
265 .sign = 'u', \
266 .realbits = (_bits), \
267 .storagebits = 16, \
268 .shift = 14 - (_bits), \
269 }, \
270 .ext_info = ad5380_ext_info, \
273 static const struct ad5380_chip_info ad5380_chip_info_tbl[] = {
274 [ID_AD5380_3] = {
275 .channel_template = AD5380_CHANNEL(14),
276 .num_channels = 40,
277 .int_vref = 1250,
279 [ID_AD5380_5] = {
280 .channel_template = AD5380_CHANNEL(14),
281 .num_channels = 40,
282 .int_vref = 2500,
284 [ID_AD5381_3] = {
285 .channel_template = AD5380_CHANNEL(12),
286 .num_channels = 16,
287 .int_vref = 1250,
289 [ID_AD5381_5] = {
290 .channel_template = AD5380_CHANNEL(12),
291 .num_channels = 16,
292 .int_vref = 2500,
294 [ID_AD5382_3] = {
295 .channel_template = AD5380_CHANNEL(14),
296 .num_channels = 32,
297 .int_vref = 1250,
299 [ID_AD5382_5] = {
300 .channel_template = AD5380_CHANNEL(14),
301 .num_channels = 32,
302 .int_vref = 2500,
304 [ID_AD5383_3] = {
305 .channel_template = AD5380_CHANNEL(12),
306 .num_channels = 32,
307 .int_vref = 1250,
309 [ID_AD5383_5] = {
310 .channel_template = AD5380_CHANNEL(12),
311 .num_channels = 32,
312 .int_vref = 2500,
314 [ID_AD5390_3] = {
315 .channel_template = AD5380_CHANNEL(14),
316 .num_channels = 16,
317 .int_vref = 1250,
319 [ID_AD5390_5] = {
320 .channel_template = AD5380_CHANNEL(14),
321 .num_channels = 16,
322 .int_vref = 2500,
324 [ID_AD5391_3] = {
325 .channel_template = AD5380_CHANNEL(12),
326 .num_channels = 16,
327 .int_vref = 1250,
329 [ID_AD5391_5] = {
330 .channel_template = AD5380_CHANNEL(12),
331 .num_channels = 16,
332 .int_vref = 2500,
334 [ID_AD5392_3] = {
335 .channel_template = AD5380_CHANNEL(14),
336 .num_channels = 8,
337 .int_vref = 1250,
339 [ID_AD5392_5] = {
340 .channel_template = AD5380_CHANNEL(14),
341 .num_channels = 8,
342 .int_vref = 2500,
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;
350 unsigned int i;
352 channels = kcalloc(st->chip_info->num_channels,
353 sizeof(struct iio_chan_spec), GFP_KERNEL);
355 if (!channels)
356 return -ENOMEM;
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;
366 return 0;
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;
375 int ret;
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");
380 return -ENOMEM;
383 st = iio_priv(indio_dev);
384 dev_set_drvdata(dev, indio_dev);
386 st->chip_info = &ad5380_chip_info_tbl[type];
387 st->regmap = regmap;
389 indio_dev->name = name;
390 indio_dev->info = &ad5380_info;
391 indio_dev->modes = INDIO_DIRECT_MODE;
392 indio_dev->num_channels = st->chip_info->num_channels;
394 mutex_init(&st->lock);
396 ret = ad5380_alloc_channels(indio_dev);
397 if (ret) {
398 dev_err(dev, "Failed to allocate channel spec: %d\n", ret);
399 return ret;
402 if (st->chip_info->int_vref == 2500)
403 ctrl |= AD5380_CTRL_INT_VREF_2V5;
405 st->vref_reg = devm_regulator_get(dev, "vref");
406 if (!IS_ERR(st->vref_reg)) {
407 ret = regulator_enable(st->vref_reg);
408 if (ret) {
409 dev_err(dev, "Failed to enable vref regulators: %d\n",
410 ret);
411 goto error_free_reg;
414 ret = regulator_get_voltage(st->vref_reg);
415 if (ret < 0)
416 goto error_disable_reg;
418 st->vref = ret / 1000;
419 } else {
420 st->vref = st->chip_info->int_vref;
421 ctrl |= AD5380_CTRL_INT_VREF_EN;
424 ret = regmap_write(st->regmap, AD5380_REG_SF_CTRL, ctrl);
425 if (ret) {
426 dev_err(dev, "Failed to write to device: %d\n", ret);
427 goto error_disable_reg;
430 ret = iio_device_register(indio_dev);
431 if (ret) {
432 dev_err(dev, "Failed to register iio device: %d\n", ret);
433 goto error_disable_reg;
436 return 0;
438 error_disable_reg:
439 if (!IS_ERR(st->vref_reg))
440 regulator_disable(st->vref_reg);
441 error_free_reg:
442 kfree(indio_dev->channels);
444 return ret;
447 static int ad5380_remove(struct device *dev)
449 struct iio_dev *indio_dev = dev_get_drvdata(dev);
450 struct ad5380_state *st = iio_priv(indio_dev);
452 iio_device_unregister(indio_dev);
454 kfree(indio_dev->channels);
456 if (!IS_ERR(st->vref_reg)) {
457 regulator_disable(st->vref_reg);
460 return 0;
463 static bool ad5380_reg_false(struct device *dev, unsigned int reg)
465 return false;
468 static const struct regmap_config ad5380_regmap_config = {
469 .reg_bits = 10,
470 .val_bits = 14,
472 .max_register = AD5380_REG_DATA(40),
473 .cache_type = REGCACHE_RBTREE,
475 .volatile_reg = ad5380_reg_false,
476 .readable_reg = ad5380_reg_false,
479 #if IS_ENABLED(CONFIG_SPI_MASTER)
481 static int ad5380_spi_probe(struct spi_device *spi)
483 const struct spi_device_id *id = spi_get_device_id(spi);
484 struct regmap *regmap;
486 regmap = devm_regmap_init_spi(spi, &ad5380_regmap_config);
488 if (IS_ERR(regmap))
489 return PTR_ERR(regmap);
491 return ad5380_probe(&spi->dev, regmap, id->driver_data, id->name);
494 static int ad5380_spi_remove(struct spi_device *spi)
496 return ad5380_remove(&spi->dev);
499 static const struct spi_device_id ad5380_spi_ids[] = {
500 { "ad5380-3", ID_AD5380_3 },
501 { "ad5380-5", ID_AD5380_5 },
502 { "ad5381-3", ID_AD5381_3 },
503 { "ad5381-5", ID_AD5381_5 },
504 { "ad5382-3", ID_AD5382_3 },
505 { "ad5382-5", ID_AD5382_5 },
506 { "ad5383-3", ID_AD5383_3 },
507 { "ad5383-5", ID_AD5383_5 },
508 { "ad5384-3", ID_AD5380_3 },
509 { "ad5384-5", ID_AD5380_5 },
510 { "ad5390-3", ID_AD5390_3 },
511 { "ad5390-5", ID_AD5390_5 },
512 { "ad5391-3", ID_AD5391_3 },
513 { "ad5391-5", ID_AD5391_5 },
514 { "ad5392-3", ID_AD5392_3 },
515 { "ad5392-5", ID_AD5392_5 },
518 MODULE_DEVICE_TABLE(spi, ad5380_spi_ids);
520 static struct spi_driver ad5380_spi_driver = {
521 .driver = {
522 .name = "ad5380",
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);
539 #else
541 static inline int ad5380_spi_register_driver(void)
543 return 0;
546 static inline void ad5380_spi_unregister_driver(void)
550 #endif
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);
561 if (IS_ERR(regmap))
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 = {
594 .driver = {
595 .name = "ad5380",
597 .probe = ad5380_i2c_probe,
598 .remove = ad5380_i2c_remove,
599 .id_table = ad5380_i2c_ids,
602 static inline int ad5380_i2c_register_driver(void)
604 return i2c_add_driver(&ad5380_i2c_driver);
607 static inline void ad5380_i2c_unregister_driver(void)
609 i2c_del_driver(&ad5380_i2c_driver);
612 #else
614 static inline int ad5380_i2c_register_driver(void)
616 return 0;
619 static inline void ad5380_i2c_unregister_driver(void)
623 #endif
625 static int __init ad5380_spi_init(void)
627 int ret;
629 ret = ad5380_spi_register_driver();
630 if (ret)
631 return ret;
633 ret = ad5380_i2c_register_driver();
634 if (ret) {
635 ad5380_spi_unregister_driver();
636 return ret;
639 return 0;
641 module_init(ad5380_spi_init);
643 static void __exit ad5380_spi_exit(void)
645 ad5380_i2c_unregister_driver();
646 ad5380_spi_unregister_driver();
649 module_exit(ad5380_spi_exit);
651 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
652 MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
653 MODULE_LICENSE("GPL v2");