treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / dac / ad5380.c
blob2ebe08326048954fab107ea442aca4bb54940464
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
56 struct ad5380_state {
57 struct regmap *regmap;
58 const struct ad5380_chip_info *chip_info;
59 struct regulator *vref_reg;
60 int vref;
61 bool pwr_down;
64 enum ad5380_type {
65 ID_AD5380_3,
66 ID_AD5380_5,
67 ID_AD5381_3,
68 ID_AD5381_5,
69 ID_AD5382_3,
70 ID_AD5382_5,
71 ID_AD5383_3,
72 ID_AD5383_5,
73 ID_AD5390_3,
74 ID_AD5390_5,
75 ID_AD5391_3,
76 ID_AD5391_5,
77 ID_AD5392_3,
78 ID_AD5392_5,
81 static ssize_t ad5380_read_dac_powerdown(struct iio_dev *indio_dev,
82 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
84 struct ad5380_state *st = iio_priv(indio_dev);
86 return sprintf(buf, "%d\n", st->pwr_down);
89 static ssize_t ad5380_write_dac_powerdown(struct iio_dev *indio_dev,
90 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
91 size_t len)
93 struct ad5380_state *st = iio_priv(indio_dev);
94 bool pwr_down;
95 int ret;
97 ret = strtobool(buf, &pwr_down);
98 if (ret)
99 return ret;
101 mutex_lock(&indio_dev->mlock);
103 if (pwr_down)
104 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_DOWN, 0);
105 else
106 ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_UP, 0);
108 st->pwr_down = pwr_down;
110 mutex_unlock(&indio_dev->mlock);
112 return ret ? ret : len;
115 static const char * const ad5380_powerdown_modes[] = {
116 "100kohm_to_gnd",
117 "three_state",
120 static int ad5380_get_powerdown_mode(struct iio_dev *indio_dev,
121 const struct iio_chan_spec *chan)
123 struct ad5380_state *st = iio_priv(indio_dev);
124 unsigned int mode;
125 int ret;
127 ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
128 if (ret)
129 return ret;
131 mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1;
133 return mode;
136 static int ad5380_set_powerdown_mode(struct iio_dev *indio_dev,
137 const struct iio_chan_spec *chan, unsigned int mode)
139 struct ad5380_state *st = iio_priv(indio_dev);
140 int ret;
142 ret = regmap_update_bits(st->regmap, AD5380_REG_SF_CTRL,
143 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET,
144 mode << AD5380_CTRL_PWR_DOWN_MODE_OFFSET);
146 return ret;
149 static const struct iio_enum ad5380_powerdown_mode_enum = {
150 .items = ad5380_powerdown_modes,
151 .num_items = ARRAY_SIZE(ad5380_powerdown_modes),
152 .get = ad5380_get_powerdown_mode,
153 .set = ad5380_set_powerdown_mode,
156 static unsigned int ad5380_info_to_reg(struct iio_chan_spec const *chan,
157 long info)
159 switch (info) {
160 case IIO_CHAN_INFO_RAW:
161 return AD5380_REG_DATA(chan->address);
162 case IIO_CHAN_INFO_CALIBBIAS:
163 return AD5380_REG_OFFSET(chan->address);
164 case IIO_CHAN_INFO_CALIBSCALE:
165 return AD5380_REG_GAIN(chan->address);
166 default:
167 break;
170 return 0;
173 static int ad5380_write_raw(struct iio_dev *indio_dev,
174 struct iio_chan_spec const *chan, int val, int val2, long info)
176 const unsigned int max_val = (1 << chan->scan_type.realbits);
177 struct ad5380_state *st = iio_priv(indio_dev);
179 switch (info) {
180 case IIO_CHAN_INFO_RAW:
181 case IIO_CHAN_INFO_CALIBSCALE:
182 if (val >= max_val || val < 0)
183 return -EINVAL;
185 return regmap_write(st->regmap,
186 ad5380_info_to_reg(chan, info),
187 val << chan->scan_type.shift);
188 case IIO_CHAN_INFO_CALIBBIAS:
189 val += (1 << chan->scan_type.realbits) / 2;
190 if (val >= max_val || val < 0)
191 return -EINVAL;
193 return regmap_write(st->regmap,
194 AD5380_REG_OFFSET(chan->address),
195 val << chan->scan_type.shift);
196 default:
197 break;
199 return -EINVAL;
202 static int ad5380_read_raw(struct iio_dev *indio_dev,
203 struct iio_chan_spec const *chan, int *val, int *val2, long info)
205 struct ad5380_state *st = iio_priv(indio_dev);
206 int ret;
208 switch (info) {
209 case IIO_CHAN_INFO_RAW:
210 case IIO_CHAN_INFO_CALIBSCALE:
211 ret = regmap_read(st->regmap, ad5380_info_to_reg(chan, info),
212 val);
213 if (ret)
214 return ret;
215 *val >>= chan->scan_type.shift;
216 return IIO_VAL_INT;
217 case IIO_CHAN_INFO_CALIBBIAS:
218 ret = regmap_read(st->regmap, AD5380_REG_OFFSET(chan->address),
219 val);
220 if (ret)
221 return ret;
222 *val >>= chan->scan_type.shift;
223 *val -= (1 << chan->scan_type.realbits) / 2;
224 return IIO_VAL_INT;
225 case IIO_CHAN_INFO_SCALE:
226 *val = 2 * st->vref;
227 *val2 = chan->scan_type.realbits;
228 return IIO_VAL_FRACTIONAL_LOG2;
229 default:
230 break;
233 return -EINVAL;
236 static const struct iio_info ad5380_info = {
237 .read_raw = ad5380_read_raw,
238 .write_raw = ad5380_write_raw,
241 static struct iio_chan_spec_ext_info ad5380_ext_info[] = {
243 .name = "powerdown",
244 .read = ad5380_read_dac_powerdown,
245 .write = ad5380_write_dac_powerdown,
246 .shared = IIO_SEPARATE,
248 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
249 &ad5380_powerdown_mode_enum),
250 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5380_powerdown_mode_enum),
251 { },
254 #define AD5380_CHANNEL(_bits) { \
255 .type = IIO_VOLTAGE, \
256 .indexed = 1, \
257 .output = 1, \
258 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
259 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
260 BIT(IIO_CHAN_INFO_CALIBBIAS), \
261 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
262 .scan_type = { \
263 .sign = 'u', \
264 .realbits = (_bits), \
265 .storagebits = 16, \
266 .shift = 14 - (_bits), \
267 }, \
268 .ext_info = ad5380_ext_info, \
271 static const struct ad5380_chip_info ad5380_chip_info_tbl[] = {
272 [ID_AD5380_3] = {
273 .channel_template = AD5380_CHANNEL(14),
274 .num_channels = 40,
275 .int_vref = 1250,
277 [ID_AD5380_5] = {
278 .channel_template = AD5380_CHANNEL(14),
279 .num_channels = 40,
280 .int_vref = 2500,
282 [ID_AD5381_3] = {
283 .channel_template = AD5380_CHANNEL(12),
284 .num_channels = 16,
285 .int_vref = 1250,
287 [ID_AD5381_5] = {
288 .channel_template = AD5380_CHANNEL(12),
289 .num_channels = 16,
290 .int_vref = 2500,
292 [ID_AD5382_3] = {
293 .channel_template = AD5380_CHANNEL(14),
294 .num_channels = 32,
295 .int_vref = 1250,
297 [ID_AD5382_5] = {
298 .channel_template = AD5380_CHANNEL(14),
299 .num_channels = 32,
300 .int_vref = 2500,
302 [ID_AD5383_3] = {
303 .channel_template = AD5380_CHANNEL(12),
304 .num_channels = 32,
305 .int_vref = 1250,
307 [ID_AD5383_5] = {
308 .channel_template = AD5380_CHANNEL(12),
309 .num_channels = 32,
310 .int_vref = 2500,
312 [ID_AD5390_3] = {
313 .channel_template = AD5380_CHANNEL(14),
314 .num_channels = 16,
315 .int_vref = 1250,
317 [ID_AD5390_5] = {
318 .channel_template = AD5380_CHANNEL(14),
319 .num_channels = 16,
320 .int_vref = 2500,
322 [ID_AD5391_3] = {
323 .channel_template = AD5380_CHANNEL(12),
324 .num_channels = 16,
325 .int_vref = 1250,
327 [ID_AD5391_5] = {
328 .channel_template = AD5380_CHANNEL(12),
329 .num_channels = 16,
330 .int_vref = 2500,
332 [ID_AD5392_3] = {
333 .channel_template = AD5380_CHANNEL(14),
334 .num_channels = 8,
335 .int_vref = 1250,
337 [ID_AD5392_5] = {
338 .channel_template = AD5380_CHANNEL(14),
339 .num_channels = 8,
340 .int_vref = 2500,
344 static int ad5380_alloc_channels(struct iio_dev *indio_dev)
346 struct ad5380_state *st = iio_priv(indio_dev);
347 struct iio_chan_spec *channels;
348 unsigned int i;
350 channels = kcalloc(st->chip_info->num_channels,
351 sizeof(struct iio_chan_spec), GFP_KERNEL);
353 if (!channels)
354 return -ENOMEM;
356 for (i = 0; i < st->chip_info->num_channels; ++i) {
357 channels[i] = st->chip_info->channel_template;
358 channels[i].channel = i;
359 channels[i].address = i;
362 indio_dev->channels = channels;
364 return 0;
367 static int ad5380_probe(struct device *dev, struct regmap *regmap,
368 enum ad5380_type type, const char *name)
370 struct iio_dev *indio_dev;
371 struct ad5380_state *st;
372 unsigned int ctrl = 0;
373 int ret;
375 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
376 if (indio_dev == NULL) {
377 dev_err(dev, "Failed to allocate iio device\n");
378 return -ENOMEM;
381 st = iio_priv(indio_dev);
382 dev_set_drvdata(dev, indio_dev);
384 st->chip_info = &ad5380_chip_info_tbl[type];
385 st->regmap = regmap;
387 indio_dev->dev.parent = dev;
388 indio_dev->name = name;
389 indio_dev->info = &ad5380_info;
390 indio_dev->modes = INDIO_DIRECT_MODE;
391 indio_dev->num_channels = st->chip_info->num_channels;
393 ret = ad5380_alloc_channels(indio_dev);
394 if (ret) {
395 dev_err(dev, "Failed to allocate channel spec: %d\n", ret);
396 return ret;
399 if (st->chip_info->int_vref == 2500)
400 ctrl |= AD5380_CTRL_INT_VREF_2V5;
402 st->vref_reg = devm_regulator_get(dev, "vref");
403 if (!IS_ERR(st->vref_reg)) {
404 ret = regulator_enable(st->vref_reg);
405 if (ret) {
406 dev_err(dev, "Failed to enable vref regulators: %d\n",
407 ret);
408 goto error_free_reg;
411 ret = regulator_get_voltage(st->vref_reg);
412 if (ret < 0)
413 goto error_disable_reg;
415 st->vref = ret / 1000;
416 } else {
417 st->vref = st->chip_info->int_vref;
418 ctrl |= AD5380_CTRL_INT_VREF_EN;
421 ret = regmap_write(st->regmap, AD5380_REG_SF_CTRL, ctrl);
422 if (ret) {
423 dev_err(dev, "Failed to write to device: %d\n", ret);
424 goto error_disable_reg;
427 ret = iio_device_register(indio_dev);
428 if (ret) {
429 dev_err(dev, "Failed to register iio device: %d\n", ret);
430 goto error_disable_reg;
433 return 0;
435 error_disable_reg:
436 if (!IS_ERR(st->vref_reg))
437 regulator_disable(st->vref_reg);
438 error_free_reg:
439 kfree(indio_dev->channels);
441 return ret;
444 static int ad5380_remove(struct device *dev)
446 struct iio_dev *indio_dev = dev_get_drvdata(dev);
447 struct ad5380_state *st = iio_priv(indio_dev);
449 iio_device_unregister(indio_dev);
451 kfree(indio_dev->channels);
453 if (!IS_ERR(st->vref_reg)) {
454 regulator_disable(st->vref_reg);
457 return 0;
460 static bool ad5380_reg_false(struct device *dev, unsigned int reg)
462 return false;
465 static const struct regmap_config ad5380_regmap_config = {
466 .reg_bits = 10,
467 .val_bits = 14,
469 .max_register = AD5380_REG_DATA(40),
470 .cache_type = REGCACHE_RBTREE,
472 .volatile_reg = ad5380_reg_false,
473 .readable_reg = ad5380_reg_false,
476 #if IS_ENABLED(CONFIG_SPI_MASTER)
478 static int ad5380_spi_probe(struct spi_device *spi)
480 const struct spi_device_id *id = spi_get_device_id(spi);
481 struct regmap *regmap;
483 regmap = devm_regmap_init_spi(spi, &ad5380_regmap_config);
485 if (IS_ERR(regmap))
486 return PTR_ERR(regmap);
488 return ad5380_probe(&spi->dev, regmap, id->driver_data, id->name);
491 static int ad5380_spi_remove(struct spi_device *spi)
493 return ad5380_remove(&spi->dev);
496 static const struct spi_device_id ad5380_spi_ids[] = {
497 { "ad5380-3", ID_AD5380_3 },
498 { "ad5380-5", ID_AD5380_5 },
499 { "ad5381-3", ID_AD5381_3 },
500 { "ad5381-5", ID_AD5381_5 },
501 { "ad5382-3", ID_AD5382_3 },
502 { "ad5382-5", ID_AD5382_5 },
503 { "ad5383-3", ID_AD5383_3 },
504 { "ad5383-5", ID_AD5383_5 },
505 { "ad5384-3", ID_AD5380_3 },
506 { "ad5384-5", ID_AD5380_5 },
507 { "ad5390-3", ID_AD5390_3 },
508 { "ad5390-5", ID_AD5390_5 },
509 { "ad5391-3", ID_AD5391_3 },
510 { "ad5391-5", ID_AD5391_5 },
511 { "ad5392-3", ID_AD5392_3 },
512 { "ad5392-5", ID_AD5392_5 },
515 MODULE_DEVICE_TABLE(spi, ad5380_spi_ids);
517 static struct spi_driver ad5380_spi_driver = {
518 .driver = {
519 .name = "ad5380",
521 .probe = ad5380_spi_probe,
522 .remove = ad5380_spi_remove,
523 .id_table = ad5380_spi_ids,
526 static inline int ad5380_spi_register_driver(void)
528 return spi_register_driver(&ad5380_spi_driver);
531 static inline void ad5380_spi_unregister_driver(void)
533 spi_unregister_driver(&ad5380_spi_driver);
536 #else
538 static inline int ad5380_spi_register_driver(void)
540 return 0;
543 static inline void ad5380_spi_unregister_driver(void)
547 #endif
549 #if IS_ENABLED(CONFIG_I2C)
551 static int ad5380_i2c_probe(struct i2c_client *i2c,
552 const struct i2c_device_id *id)
554 struct regmap *regmap;
556 regmap = devm_regmap_init_i2c(i2c, &ad5380_regmap_config);
558 if (IS_ERR(regmap))
559 return PTR_ERR(regmap);
561 return ad5380_probe(&i2c->dev, regmap, id->driver_data, id->name);
564 static int ad5380_i2c_remove(struct i2c_client *i2c)
566 return ad5380_remove(&i2c->dev);
569 static const struct i2c_device_id ad5380_i2c_ids[] = {
570 { "ad5380-3", ID_AD5380_3 },
571 { "ad5380-5", ID_AD5380_5 },
572 { "ad5381-3", ID_AD5381_3 },
573 { "ad5381-5", ID_AD5381_5 },
574 { "ad5382-3", ID_AD5382_3 },
575 { "ad5382-5", ID_AD5382_5 },
576 { "ad5383-3", ID_AD5383_3 },
577 { "ad5383-5", ID_AD5383_5 },
578 { "ad5384-3", ID_AD5380_3 },
579 { "ad5384-5", ID_AD5380_5 },
580 { "ad5390-3", ID_AD5390_3 },
581 { "ad5390-5", ID_AD5390_5 },
582 { "ad5391-3", ID_AD5391_3 },
583 { "ad5391-5", ID_AD5391_5 },
584 { "ad5392-3", ID_AD5392_3 },
585 { "ad5392-5", ID_AD5392_5 },
588 MODULE_DEVICE_TABLE(i2c, ad5380_i2c_ids);
590 static struct i2c_driver ad5380_i2c_driver = {
591 .driver = {
592 .name = "ad5380",
594 .probe = ad5380_i2c_probe,
595 .remove = ad5380_i2c_remove,
596 .id_table = ad5380_i2c_ids,
599 static inline int ad5380_i2c_register_driver(void)
601 return i2c_add_driver(&ad5380_i2c_driver);
604 static inline void ad5380_i2c_unregister_driver(void)
606 i2c_del_driver(&ad5380_i2c_driver);
609 #else
611 static inline int ad5380_i2c_register_driver(void)
613 return 0;
616 static inline void ad5380_i2c_unregister_driver(void)
620 #endif
622 static int __init ad5380_spi_init(void)
624 int ret;
626 ret = ad5380_spi_register_driver();
627 if (ret)
628 return ret;
630 ret = ad5380_i2c_register_driver();
631 if (ret) {
632 ad5380_spi_unregister_driver();
633 return ret;
636 return 0;
638 module_init(ad5380_spi_init);
640 static void __exit ad5380_spi_exit(void)
642 ad5380_i2c_unregister_driver();
643 ad5380_spi_unregister_driver();
646 module_exit(ad5380_spi_exit);
648 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
649 MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC");
650 MODULE_LICENSE("GPL v2");