spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / staging / iio / dac / ad5380.c
blobeff97ae05c4be37b3356b6c66b1d6b64050a6c10
1 /*
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.
8 */
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>
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "dac.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)
37 /**
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;
50 /**
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
59 struct ad5380_state {
60 struct regmap *regmap;
61 const struct ad5380_chip_info *chip_info;
62 struct regulator *vref_reg;
63 int vref;
64 bool pwr_down;
67 enum ad5380_type {
68 ID_AD5380_3,
69 ID_AD5380_5,
70 ID_AD5381_3,
71 ID_AD5381_5,
72 ID_AD5382_3,
73 ID_AD5382_5,
74 ID_AD5383_3,
75 ID_AD5383_5,
76 ID_AD5390_3,
77 ID_AD5390_5,
78 ID_AD5391_3,
79 ID_AD5391_5,
80 ID_AD5392_3,
81 ID_AD5392_5,
84 #define AD5380_CHANNEL(_bits) { \
85 .type = IIO_VOLTAGE, \
86 .indexed = 1, \
87 .output = 1, \
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[] = {
95 [ID_AD5380_3] = {
96 .channel_template = AD5380_CHANNEL(14),
97 .num_channels = 40,
98 .int_vref = 1250000,
100 [ID_AD5380_5] = {
101 .channel_template = AD5380_CHANNEL(14),
102 .num_channels = 40,
103 .int_vref = 2500000,
105 [ID_AD5381_3] = {
106 .channel_template = AD5380_CHANNEL(12),
107 .num_channels = 16,
108 .int_vref = 1250000,
110 [ID_AD5381_5] = {
111 .channel_template = AD5380_CHANNEL(12),
112 .num_channels = 16,
113 .int_vref = 2500000,
115 [ID_AD5382_3] = {
116 .channel_template = AD5380_CHANNEL(14),
117 .num_channels = 32,
118 .int_vref = 1250000,
120 [ID_AD5382_5] = {
121 .channel_template = AD5380_CHANNEL(14),
122 .num_channels = 32,
123 .int_vref = 2500000,
125 [ID_AD5383_3] = {
126 .channel_template = AD5380_CHANNEL(12),
127 .num_channels = 32,
128 .int_vref = 1250000,
130 [ID_AD5383_5] = {
131 .channel_template = AD5380_CHANNEL(12),
132 .num_channels = 32,
133 .int_vref = 2500000,
135 [ID_AD5390_3] = {
136 .channel_template = AD5380_CHANNEL(14),
137 .num_channels = 16,
138 .int_vref = 1250000,
140 [ID_AD5390_5] = {
141 .channel_template = AD5380_CHANNEL(14),
142 .num_channels = 16,
143 .int_vref = 2500000,
145 [ID_AD5391_3] = {
146 .channel_template = AD5380_CHANNEL(12),
147 .num_channels = 16,
148 .int_vref = 1250000,
150 [ID_AD5391_5] = {
151 .channel_template = AD5380_CHANNEL(12),
152 .num_channels = 16,
153 .int_vref = 2500000,
155 [ID_AD5392_3] = {
156 .channel_template = AD5380_CHANNEL(14),
157 .num_channels = 8,
158 .int_vref = 1250000,
160 [ID_AD5392_5] = {
161 .channel_template = AD5380_CHANNEL(14),
162 .num_channels = 8,
163 .int_vref = 2500000,
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);
181 bool pwr_down;
182 int ret;
184 ret = strtobool(buf, &pwr_down);
185 if (ret)
186 return ret;
188 mutex_lock(&indio_dev->mlock);
190 if (pwr_down)
191 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_DOWN, 0);
192 else
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,
203 S_IRUGO | S_IWUSR,
204 ad5380_read_dac_powerdown,
205 ad5380_write_dac_powerdown, 0);
207 static const char ad5380_powerdown_modes[][15] = {
208 [0] = "100kohm_to_gnd",
209 [1] = "three_state",
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);
217 unsigned int mode;
218 int ret;
220 ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
221 if (ret)
222 return ret;
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);
234 unsigned int i;
235 int ret;
237 for (i = 0; i < ARRAY_SIZE(ad5380_powerdown_modes); ++i) {
238 if (sysfs_streq(buf, ad5380_powerdown_modes[i]))
239 break;
242 if (i == ARRAY_SIZE(ad5380_powerdown_modes))
243 return -EINVAL;
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,
253 S_IRUGO | S_IWUSR,
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,
264 NULL,
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,
272 long info)
274 switch (info) {
275 case 0:
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);
281 default:
282 break;
285 return 0;
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);
294 switch (info) {
295 case 0:
296 case IIO_CHAN_INFO_CALIBSCALE:
297 if (val >= max_val || val < 0)
298 return -EINVAL;
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)
306 return -EINVAL;
308 return regmap_write(st->regmap,
309 AD5380_REG_OFFSET(chan->address),
310 val << chan->scan_type.shift);
311 default:
312 break;
314 return -EINVAL;
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;
322 int ret;
324 switch (info) {
325 case 0:
326 case IIO_CHAN_INFO_CALIBSCALE:
327 ret = regmap_read(st->regmap, ad5380_info_to_reg(chan, info),
328 val);
329 if (ret)
330 return ret;
331 *val >>= chan->scan_type.shift;
332 return IIO_VAL_INT;
333 case IIO_CHAN_INFO_CALIBBIAS:
334 ret = regmap_read(st->regmap, AD5380_REG_OFFSET(chan->address),
335 val);
336 if (ret)
337 return ret;
338 *val >>= chan->scan_type.shift;
339 val -= (1 << chan->scan_type.realbits) / 2;
340 return IIO_VAL_INT;
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;
346 default:
347 break;
350 return -EINVAL;
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;
364 unsigned int i;
366 channels = kcalloc(sizeof(struct iio_chan_spec),
367 st->chip_info->num_channels, GFP_KERNEL);
369 if (!channels)
370 return -ENOMEM;
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;
380 return 0;
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;
389 int ret;
391 indio_dev = iio_allocate_device(sizeof(*st));
392 if (indio_dev == NULL) {
393 dev_err(dev, "Failed to allocate iio device\n");
394 ret = -ENOMEM;
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];
402 st->regmap = regmap;
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);
411 if (ret) {
412 dev_err(dev, "Failed to allocate channel spec: %d\n", ret);
413 goto error_free;
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);
422 if (ret) {
423 dev_err(dev, "Failed to enable vref regulators: %d\n",
424 ret);
425 goto error_free_reg;
428 st->vref = regulator_get_voltage(st->vref_reg);
429 } else {
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);
435 if (ret) {
436 dev_err(dev, "Failed to write to device: %d\n", ret);
437 goto error_disable_reg;
440 ret = iio_device_register(indio_dev);
441 if (ret) {
442 dev_err(dev, "Failed to register iio device: %d\n", ret);
443 goto error_disable_reg;
446 return 0;
448 error_disable_reg:
449 if (!IS_ERR(st->vref_reg))
450 regulator_disable(st->vref_reg);
451 error_free_reg:
452 if (!IS_ERR(st->vref_reg))
453 regulator_put(st->vref_reg);
455 kfree(indio_dev->channels);
456 error_free:
457 iio_free_device(indio_dev);
458 error_regmap_exit:
459 regmap_exit(regmap);
461 return ret;
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);
481 return 0;
484 static bool ad5380_reg_false(struct device *dev, unsigned int reg)
486 return false;
489 static const struct regmap_config ad5380_regmap_config = {
490 .reg_bits = 10,
491 .val_bits = 14,
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);
509 if (IS_ERR(regmap))
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 = {
542 .driver = {
543 .name = "ad5380",
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);
561 #else
563 static inline int ad5380_spi_register_driver(void)
565 return 0;
568 static inline void ad5380_spi_unregister_driver(void)
572 #endif
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);
583 if (IS_ERR(regmap))
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 = {
616 .driver = {
617 .name = "ad5380",
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);
635 #else
637 static inline int ad5380_i2c_register_driver(void)
639 return 0;
642 static inline void ad5380_i2c_unregister_driver(void)
646 #endif
648 static int __init ad5380_spi_init(void)
650 int ret;
652 ret = ad5380_spi_register_driver();
653 if (ret)
654 return ret;
656 ret = ad5380_i2c_register_driver();
657 if (ret) {
658 ad5380_spi_unregister_driver();
659 return ret;
662 return 0;
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");