]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/iio/dac/ad5380.c
iio: adc: stm32-adc: fix a race when using several adcs with dma and irq
[mirror_ubuntu-bionic-kernel.git] / drivers / iio / dac / ad5380.c
1 /*
2 * Analog devices AD5380, AD5381, AD5382, AD5383, AD5390, AD5391, AD5392
3 * multi-channel Digital to Analog Converters driver
4 *
5 * Copyright 2011 Analog Devices Inc.
6 *
7 * Licensed under the GPL-2.
8 */
9
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>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23
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)
30
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)
34
35 /**
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
40 */
41
42 struct ad5380_chip_info {
43 struct iio_chan_spec channel_template;
44 unsigned int num_channels;
45 unsigned int int_vref;
46 };
47
48 /**
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
55 */
56
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 };
64
65 enum ad5380_type {
66 ID_AD5380_3,
67 ID_AD5380_5,
68 ID_AD5381_3,
69 ID_AD5381_5,
70 ID_AD5382_3,
71 ID_AD5382_5,
72 ID_AD5383_3,
73 ID_AD5383_5,
74 ID_AD5390_3,
75 ID_AD5390_5,
76 ID_AD5391_3,
77 ID_AD5391_5,
78 ID_AD5392_3,
79 ID_AD5392_5,
80 };
81
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)
84 {
85 struct ad5380_state *st = iio_priv(indio_dev);
86
87 return sprintf(buf, "%d\n", st->pwr_down);
88 }
89
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,
92 size_t len)
93 {
94 struct ad5380_state *st = iio_priv(indio_dev);
95 bool pwr_down;
96 int ret;
97
98 ret = strtobool(buf, &pwr_down);
99 if (ret)
100 return ret;
101
102 mutex_lock(&indio_dev->mlock);
103
104 if (pwr_down)
105 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_DOWN, 0);
106 else
107 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_UP, 0);
108
109 st->pwr_down = pwr_down;
110
111 mutex_unlock(&indio_dev->mlock);
112
113 return ret ? ret : len;
114 }
115
116 static const char * const ad5380_powerdown_modes[] = {
117 "100kohm_to_gnd",
118 "three_state",
119 };
120
121 static int ad5380_get_powerdown_mode(struct iio_dev *indio_dev,
122 const struct iio_chan_spec *chan)
123 {
124 struct ad5380_state *st = iio_priv(indio_dev);
125 unsigned int mode;
126 int ret;
127
128 ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
129 if (ret)
130 return ret;
131
132 mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1;
133
134 return mode;
135 }
136
137 static int ad5380_set_powerdown_mode(struct iio_dev *indio_dev,
138 const struct iio_chan_spec *chan, unsigned int mode)
139 {
140 struct ad5380_state *st = iio_priv(indio_dev);
141 int ret;
142
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);
146
147 return ret;
148 }
149
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,
155 };
156
157 static unsigned int ad5380_info_to_reg(struct iio_chan_spec const *chan,
158 long info)
159 {
160 switch (info) {
161 case 0:
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);
167 default:
168 break;
169 }
170
171 return 0;
172 }
173
174 static int ad5380_write_raw(struct iio_dev *indio_dev,
175 struct iio_chan_spec const *chan, int val, int val2, long info)
176 {
177 const unsigned int max_val = (1 << chan->scan_type.realbits);
178 struct ad5380_state *st = iio_priv(indio_dev);
179
180 switch (info) {
181 case IIO_CHAN_INFO_RAW:
182 case IIO_CHAN_INFO_CALIBSCALE:
183 if (val >= max_val || val < 0)
184 return -EINVAL;
185
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)
192 return -EINVAL;
193
194 return regmap_write(st->regmap,
195 AD5380_REG_OFFSET(chan->address),
196 val << chan->scan_type.shift);
197 default:
198 break;
199 }
200 return -EINVAL;
201 }
202
203 static int ad5380_read_raw(struct iio_dev *indio_dev,
204 struct iio_chan_spec const *chan, int *val, int *val2, long info)
205 {
206 struct ad5380_state *st = iio_priv(indio_dev);
207 int ret;
208
209 switch (info) {
210 case IIO_CHAN_INFO_RAW:
211 case IIO_CHAN_INFO_CALIBSCALE:
212 ret = regmap_read(st->regmap, ad5380_info_to_reg(chan, info),
213 val);
214 if (ret)
215 return ret;
216 *val >>= chan->scan_type.shift;
217 return IIO_VAL_INT;
218 case IIO_CHAN_INFO_CALIBBIAS:
219 ret = regmap_read(st->regmap, AD5380_REG_OFFSET(chan->address),
220 val);
221 if (ret)
222 return ret;
223 *val >>= chan->scan_type.shift;
224 val -= (1 << chan->scan_type.realbits) / 2;
225 return IIO_VAL_INT;
226 case IIO_CHAN_INFO_SCALE:
227 *val = 2 * st->vref;
228 *val2 = chan->scan_type.realbits;
229 return IIO_VAL_FRACTIONAL_LOG2;
230 default:
231 break;
232 }
233
234 return -EINVAL;
235 }
236
237 static const struct iio_info ad5380_info = {
238 .read_raw = ad5380_read_raw,
239 .write_raw = ad5380_write_raw,
240 };
241
242 static struct iio_chan_spec_ext_info ad5380_ext_info[] = {
243 {
244 .name = "powerdown",
245 .read = ad5380_read_dac_powerdown,
246 .write = ad5380_write_dac_powerdown,
247 .shared = IIO_SEPARATE,
248 },
249 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
250 &ad5380_powerdown_mode_enum),
251 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5380_powerdown_mode_enum),
252 { },
253 };
254
255 #define AD5380_CHANNEL(_bits) { \
256 .type = IIO_VOLTAGE, \
257 .indexed = 1, \
258 .output = 1, \
259 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
260 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
261 BIT(IIO_CHAN_INFO_CALIBBIAS), \
262 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
263 .scan_type = { \
264 .sign = 'u', \
265 .realbits = (_bits), \
266 .storagebits = 16, \
267 .shift = 14 - (_bits), \
268 }, \
269 .ext_info = ad5380_ext_info, \
270 }
271
272 static const struct ad5380_chip_info ad5380_chip_info_tbl[] = {
273 [ID_AD5380_3] = {
274 .channel_template = AD5380_CHANNEL(14),
275 .num_channels = 40,
276 .int_vref = 1250,
277 },
278 [ID_AD5380_5] = {
279 .channel_template = AD5380_CHANNEL(14),
280 .num_channels = 40,
281 .int_vref = 2500,
282 },
283 [ID_AD5381_3] = {
284 .channel_template = AD5380_CHANNEL(12),
285 .num_channels = 16,
286 .int_vref = 1250,
287 },
288 [ID_AD5381_5] = {
289 .channel_template = AD5380_CHANNEL(12),
290 .num_channels = 16,
291 .int_vref = 2500,
292 },
293 [ID_AD5382_3] = {
294 .channel_template = AD5380_CHANNEL(14),
295 .num_channels = 32,
296 .int_vref = 1250,
297 },
298 [ID_AD5382_5] = {
299 .channel_template = AD5380_CHANNEL(14),
300 .num_channels = 32,
301 .int_vref = 2500,
302 },
303 [ID_AD5383_3] = {
304 .channel_template = AD5380_CHANNEL(12),
305 .num_channels = 32,
306 .int_vref = 1250,
307 },
308 [ID_AD5383_5] = {
309 .channel_template = AD5380_CHANNEL(12),
310 .num_channels = 32,
311 .int_vref = 2500,
312 },
313 [ID_AD5390_3] = {
314 .channel_template = AD5380_CHANNEL(14),
315 .num_channels = 16,
316 .int_vref = 1250,
317 },
318 [ID_AD5390_5] = {
319 .channel_template = AD5380_CHANNEL(14),
320 .num_channels = 16,
321 .int_vref = 2500,
322 },
323 [ID_AD5391_3] = {
324 .channel_template = AD5380_CHANNEL(12),
325 .num_channels = 16,
326 .int_vref = 1250,
327 },
328 [ID_AD5391_5] = {
329 .channel_template = AD5380_CHANNEL(12),
330 .num_channels = 16,
331 .int_vref = 2500,
332 },
333 [ID_AD5392_3] = {
334 .channel_template = AD5380_CHANNEL(14),
335 .num_channels = 8,
336 .int_vref = 1250,
337 },
338 [ID_AD5392_5] = {
339 .channel_template = AD5380_CHANNEL(14),
340 .num_channels = 8,
341 .int_vref = 2500,
342 },
343 };
344
345 static int ad5380_alloc_channels(struct iio_dev *indio_dev)
346 {
347 struct ad5380_state *st = iio_priv(indio_dev);
348 struct iio_chan_spec *channels;
349 unsigned int i;
350
351 channels = kcalloc(st->chip_info->num_channels,
352 sizeof(struct iio_chan_spec), GFP_KERNEL);
353
354 if (!channels)
355 return -ENOMEM;
356
357 for (i = 0; i < st->chip_info->num_channels; ++i) {
358 channels[i] = st->chip_info->channel_template;
359 channels[i].channel = i;
360 channels[i].address = i;
361 }
362
363 indio_dev->channels = channels;
364
365 return 0;
366 }
367
368 static int ad5380_probe(struct device *dev, struct regmap *regmap,
369 enum ad5380_type type, const char *name)
370 {
371 struct iio_dev *indio_dev;
372 struct ad5380_state *st;
373 unsigned int ctrl = 0;
374 int ret;
375
376 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
377 if (indio_dev == NULL) {
378 dev_err(dev, "Failed to allocate iio device\n");
379 return -ENOMEM;
380 }
381
382 st = iio_priv(indio_dev);
383 dev_set_drvdata(dev, indio_dev);
384
385 st->chip_info = &ad5380_chip_info_tbl[type];
386 st->regmap = regmap;
387
388 indio_dev->dev.parent = dev;
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;
393
394 ret = ad5380_alloc_channels(indio_dev);
395 if (ret) {
396 dev_err(dev, "Failed to allocate channel spec: %d\n", ret);
397 return ret;
398 }
399
400 if (st->chip_info->int_vref == 2500)
401 ctrl |= AD5380_CTRL_INT_VREF_2V5;
402
403 st->vref_reg = devm_regulator_get(dev, "vref");
404 if (!IS_ERR(st->vref_reg)) {
405 ret = regulator_enable(st->vref_reg);
406 if (ret) {
407 dev_err(dev, "Failed to enable vref regulators: %d\n",
408 ret);
409 goto error_free_reg;
410 }
411
412 ret = regulator_get_voltage(st->vref_reg);
413 if (ret < 0)
414 goto error_disable_reg;
415
416 st->vref = ret / 1000;
417 } else {
418 st->vref = st->chip_info->int_vref;
419 ctrl |= AD5380_CTRL_INT_VREF_EN;
420 }
421
422 ret = regmap_write(st->regmap, AD5380_REG_SF_CTRL, ctrl);
423 if (ret) {
424 dev_err(dev, "Failed to write to device: %d\n", ret);
425 goto error_disable_reg;
426 }
427
428 ret = iio_device_register(indio_dev);
429 if (ret) {
430 dev_err(dev, "Failed to register iio device: %d\n", ret);
431 goto error_disable_reg;
432 }
433
434 return 0;
435
436 error_disable_reg:
437 if (!IS_ERR(st->vref_reg))
438 regulator_disable(st->vref_reg);
439 error_free_reg:
440 kfree(indio_dev->channels);
441
442 return ret;
443 }
444
445 static int ad5380_remove(struct device *dev)
446 {
447 struct iio_dev *indio_dev = dev_get_drvdata(dev);
448 struct ad5380_state *st = iio_priv(indio_dev);
449
450 iio_device_unregister(indio_dev);
451
452 kfree(indio_dev->channels);
453
454 if (!IS_ERR(st->vref_reg)) {
455 regulator_disable(st->vref_reg);
456 }
457
458 return 0;
459 }
460
461 static bool ad5380_reg_false(struct device *dev, unsigned int reg)
462 {
463 return false;
464 }
465
466 static const struct regmap_config ad5380_regmap_config = {
467 .reg_bits = 10,
468 .val_bits = 14,
469
470 .max_register = AD5380_REG_DATA(40),
471 .cache_type = REGCACHE_RBTREE,
472
473 .volatile_reg = ad5380_reg_false,
474 .readable_reg = ad5380_reg_false,
475 };
476
477 #if IS_ENABLED(CONFIG_SPI_MASTER)
478
479 static int ad5380_spi_probe(struct spi_device *spi)
480 {
481 const struct spi_device_id *id = spi_get_device_id(spi);
482 struct regmap *regmap;
483
484 regmap = devm_regmap_init_spi(spi, &ad5380_regmap_config);
485
486 if (IS_ERR(regmap))
487 return PTR_ERR(regmap);
488
489 return ad5380_probe(&spi->dev, regmap, id->driver_data, id->name);
490 }
491
492 static int ad5380_spi_remove(struct spi_device *spi)
493 {
494 return ad5380_remove(&spi->dev);
495 }
496
497 static const struct spi_device_id ad5380_spi_ids[] = {
498 { "ad5380-3", ID_AD5380_3 },
499 { "ad5380-5", ID_AD5380_5 },
500 { "ad5381-3", ID_AD5381_3 },
501 { "ad5381-5", ID_AD5381_5 },
502 { "ad5382-3", ID_AD5382_3 },
503 { "ad5382-5", ID_AD5382_5 },
504 { "ad5383-3", ID_AD5383_3 },
505 { "ad5383-5", ID_AD5383_5 },
506 { "ad5384-3", ID_AD5380_3 },
507 { "ad5384-5", ID_AD5380_5 },
508 { "ad5390-3", ID_AD5390_3 },
509 { "ad5390-5", ID_AD5390_5 },
510 { "ad5391-3", ID_AD5391_3 },
511 { "ad5391-5", ID_AD5391_5 },
512 { "ad5392-3", ID_AD5392_3 },
513 { "ad5392-5", ID_AD5392_5 },
514 { }
515 };
516 MODULE_DEVICE_TABLE(spi, ad5380_spi_ids);
517
518 static struct spi_driver ad5380_spi_driver = {
519 .driver = {
520 .name = "ad5380",
521 },
522 .probe = ad5380_spi_probe,
523 .remove = ad5380_spi_remove,
524 .id_table = ad5380_spi_ids,
525 };
526
527 static inline int ad5380_spi_register_driver(void)
528 {
529 return spi_register_driver(&ad5380_spi_driver);
530 }
531
532 static inline void ad5380_spi_unregister_driver(void)
533 {
534 spi_unregister_driver(&ad5380_spi_driver);
535 }
536
537 #else
538
539 static inline int ad5380_spi_register_driver(void)
540 {
541 return 0;
542 }
543
544 static inline void ad5380_spi_unregister_driver(void)
545 {
546 }
547
548 #endif
549
550 #if IS_ENABLED(CONFIG_I2C)
551
552 static int ad5380_i2c_probe(struct i2c_client *i2c,
553 const struct i2c_device_id *id)
554 {
555 struct regmap *regmap;
556
557 regmap = devm_regmap_init_i2c(i2c, &ad5380_regmap_config);
558
559 if (IS_ERR(regmap))
560 return PTR_ERR(regmap);
561
562 return ad5380_probe(&i2c->dev, regmap, id->driver_data, id->name);
563 }
564
565 static int ad5380_i2c_remove(struct i2c_client *i2c)
566 {
567 return ad5380_remove(&i2c->dev);
568 }
569
570 static const struct i2c_device_id ad5380_i2c_ids[] = {
571 { "ad5380-3", ID_AD5380_3 },
572 { "ad5380-5", ID_AD5380_5 },
573 { "ad5381-3", ID_AD5381_3 },
574 { "ad5381-5", ID_AD5381_5 },
575 { "ad5382-3", ID_AD5382_3 },
576 { "ad5382-5", ID_AD5382_5 },
577 { "ad5383-3", ID_AD5383_3 },
578 { "ad5383-5", ID_AD5383_5 },
579 { "ad5384-3", ID_AD5380_3 },
580 { "ad5384-5", ID_AD5380_5 },
581 { "ad5390-3", ID_AD5390_3 },
582 { "ad5390-5", ID_AD5390_5 },
583 { "ad5391-3", ID_AD5391_3 },
584 { "ad5391-5", ID_AD5391_5 },
585 { "ad5392-3", ID_AD5392_3 },
586 { "ad5392-5", ID_AD5392_5 },
587 { }
588 };
589 MODULE_DEVICE_TABLE(i2c, ad5380_i2c_ids);
590
591 static struct i2c_driver ad5380_i2c_driver = {
592 .driver = {
593 .name = "ad5380",
594 },
595 .probe = ad5380_i2c_probe,
596 .remove = ad5380_i2c_remove,
597 .id_table = ad5380_i2c_ids,
598 };
599
600 static inline int ad5380_i2c_register_driver(void)
601 {
602 return i2c_add_driver(&ad5380_i2c_driver);
603 }
604
605 static inline void ad5380_i2c_unregister_driver(void)
606 {
607 i2c_del_driver(&ad5380_i2c_driver);
608 }
609
610 #else
611
612 static inline int ad5380_i2c_register_driver(void)
613 {
614 return 0;
615 }
616
617 static inline void ad5380_i2c_unregister_driver(void)
618 {
619 }
620
621 #endif
622
623 static int __init ad5380_spi_init(void)
624 {
625 int ret;
626
627 ret = ad5380_spi_register_driver();
628 if (ret)
629 return ret;
630
631 ret = ad5380_i2c_register_driver();
632 if (ret) {
633 ad5380_spi_unregister_driver();
634 return ret;
635 }
636
637 return 0;
638 }
639 module_init(ad5380_spi_init);
640
641 static void __exit ad5380_spi_exit(void)
642 {
643 ad5380_i2c_unregister_driver();
644 ad5380_spi_unregister_driver();
645
646 }
647 module_exit(ad5380_spi_exit);
648
649 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
650 MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
651 MODULE_LICENSE("GPL v2");