Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / iio / dac / ad5380.c
blob392a1c7aee03b515e9e068a1fc9bb4e594bef267
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
40 struct ad5380_chip_info {
41 struct iio_chan_spec channel_template;
42 unsigned int num_channels;
43 unsigned int int_vref;
46 /**
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
54 struct ad5380_state {
55 struct regmap *regmap;
56 const struct ad5380_chip_info *chip_info;
57 int vref;
58 bool pwr_down;
59 struct mutex lock;
62 enum ad5380_type {
63 ID_AD5380_3,
64 ID_AD5380_5,
65 ID_AD5381_3,
66 ID_AD5381_5,
67 ID_AD5382_3,
68 ID_AD5382_5,
69 ID_AD5383_3,
70 ID_AD5383_5,
71 ID_AD5390_3,
72 ID_AD5390_5,
73 ID_AD5391_3,
74 ID_AD5391_5,
75 ID_AD5392_3,
76 ID_AD5392_5,
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,
89 size_t len)
91 struct ad5380_state *st = iio_priv(indio_dev);
92 bool pwr_down;
93 int ret;
95 ret = kstrtobool(buf, &pwr_down);
96 if (ret)
97 return ret;
99 mutex_lock(&st->lock);
101 if (pwr_down)
102 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_DOWN, 0);
103 else
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[] = {
114 "100kohm_to_gnd",
115 "three_state",
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);
122 unsigned int mode;
123 int ret;
125 ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
126 if (ret)
127 return ret;
129 mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1;
131 return mode;
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);
138 int ret;
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);
144 return ret;
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,
155 long info)
157 switch (info) {
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);
164 default:
165 break;
168 return 0;
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);
177 switch (info) {
178 case IIO_CHAN_INFO_RAW:
179 case IIO_CHAN_INFO_CALIBSCALE:
180 if (val >= max_val || val < 0)
181 return -EINVAL;
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)
189 return -EINVAL;
191 return regmap_write(st->regmap,
192 AD5380_REG_OFFSET(chan->address),
193 val << chan->scan_type.shift);
194 default:
195 break;
197 return -EINVAL;
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);
204 int ret;
206 switch (info) {
207 case IIO_CHAN_INFO_RAW:
208 case IIO_CHAN_INFO_CALIBSCALE:
209 ret = regmap_read(st->regmap, ad5380_info_to_reg(chan, info),
210 val);
211 if (ret)
212 return ret;
213 *val >>= chan->scan_type.shift;
214 return IIO_VAL_INT;
215 case IIO_CHAN_INFO_CALIBBIAS:
216 ret = regmap_read(st->regmap, AD5380_REG_OFFSET(chan->address),
217 val);
218 if (ret)
219 return ret;
220 *val >>= chan->scan_type.shift;
221 *val -= (1 << chan->scan_type.realbits) / 2;
222 return IIO_VAL_INT;
223 case IIO_CHAN_INFO_SCALE:
224 *val = 2 * st->vref;
225 *val2 = chan->scan_type.realbits;
226 return IIO_VAL_FRACTIONAL_LOG2;
227 default:
228 break;
231 return -EINVAL;
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[] = {
241 .name = "powerdown",
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),
249 { },
252 #define AD5380_CHANNEL(_bits) { \
253 .type = IIO_VOLTAGE, \
254 .indexed = 1, \
255 .output = 1, \
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), \
260 .scan_type = { \
261 .sign = 'u', \
262 .realbits = (_bits), \
263 .storagebits = 16, \
264 .shift = 14 - (_bits), \
265 }, \
266 .ext_info = ad5380_ext_info, \
269 static const struct ad5380_chip_info ad5380_chip_info_tbl[] = {
270 [ID_AD5380_3] = {
271 .channel_template = AD5380_CHANNEL(14),
272 .num_channels = 40,
273 .int_vref = 1250,
275 [ID_AD5380_5] = {
276 .channel_template = AD5380_CHANNEL(14),
277 .num_channels = 40,
278 .int_vref = 2500,
280 [ID_AD5381_3] = {
281 .channel_template = AD5380_CHANNEL(12),
282 .num_channels = 16,
283 .int_vref = 1250,
285 [ID_AD5381_5] = {
286 .channel_template = AD5380_CHANNEL(12),
287 .num_channels = 16,
288 .int_vref = 2500,
290 [ID_AD5382_3] = {
291 .channel_template = AD5380_CHANNEL(14),
292 .num_channels = 32,
293 .int_vref = 1250,
295 [ID_AD5382_5] = {
296 .channel_template = AD5380_CHANNEL(14),
297 .num_channels = 32,
298 .int_vref = 2500,
300 [ID_AD5383_3] = {
301 .channel_template = AD5380_CHANNEL(12),
302 .num_channels = 32,
303 .int_vref = 1250,
305 [ID_AD5383_5] = {
306 .channel_template = AD5380_CHANNEL(12),
307 .num_channels = 32,
308 .int_vref = 2500,
310 [ID_AD5390_3] = {
311 .channel_template = AD5380_CHANNEL(14),
312 .num_channels = 16,
313 .int_vref = 1250,
315 [ID_AD5390_5] = {
316 .channel_template = AD5380_CHANNEL(14),
317 .num_channels = 16,
318 .int_vref = 2500,
320 [ID_AD5391_3] = {
321 .channel_template = AD5380_CHANNEL(12),
322 .num_channels = 16,
323 .int_vref = 1250,
325 [ID_AD5391_5] = {
326 .channel_template = AD5380_CHANNEL(12),
327 .num_channels = 16,
328 .int_vref = 2500,
330 [ID_AD5392_3] = {
331 .channel_template = AD5380_CHANNEL(14),
332 .num_channels = 8,
333 .int_vref = 1250,
335 [ID_AD5392_5] = {
336 .channel_template = AD5380_CHANNEL(14),
337 .num_channels = 8,
338 .int_vref = 2500,
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;
346 unsigned int i;
348 channels = devm_kcalloc(dev, st->chip_info->num_channels,
349 sizeof(struct iio_chan_spec), GFP_KERNEL);
351 if (!channels)
352 return -ENOMEM;
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;
362 return 0;
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;
371 int ret;
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");
376 return -ENOMEM;
379 st = iio_priv(indio_dev);
381 st->chip_info = &ad5380_chip_info_tbl[type];
382 st->regmap = regmap;
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);
392 if (ret)
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;
404 } else {
405 st->vref = ret / 1000;
408 ret = regmap_write(st->regmap, AD5380_REG_SF_CTRL, ctrl);
409 if (ret)
410 return dev_err_probe(dev, ret, "Failed to write to device\n");
412 ret = devm_iio_device_register(dev, indio_dev);
413 if (ret)
414 return dev_err_probe(dev, ret, "Failed to register iio device\n");
416 return 0;
419 static bool ad5380_reg_false(struct device *dev, unsigned int reg)
421 return false;
424 static const struct regmap_config ad5380_regmap_config = {
425 .reg_bits = 10,
426 .val_bits = 14,
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);
444 if (IS_ERR(regmap))
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 = {
472 .driver = {
473 .name = "ad5380",
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);
489 #else
491 static inline int ad5380_spi_register_driver(void)
493 return 0;
496 static inline void ad5380_spi_unregister_driver(void)
500 #endif
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);
511 if (IS_ERR(regmap))
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 = {
539 .driver = {
540 .name = "ad5380",
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);
556 #else
558 static inline int ad5380_i2c_register_driver(void)
560 return 0;
563 static inline void ad5380_i2c_unregister_driver(void)
567 #endif
569 static int __init ad5380_spi_init(void)
571 int ret;
573 ret = ad5380_spi_register_driver();
574 if (ret)
575 return ret;
577 ret = ad5380_i2c_register_driver();
578 if (ret) {
579 ad5380_spi_unregister_driver();
580 return ret;
583 return 0;
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");