xref: /openbmc/linux/drivers/iio/dac/ad5380.c (revision 76426e23)
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
5  *
6  * Copyright 2011 Analog Devices Inc.
7  */
8 
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>
19 
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 
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)
29 
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)
33 
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
39 */
40 
41 struct ad5380_chip_info {
42 	struct iio_chan_spec	channel_template;
43 	unsigned int		num_channels;
44 	unsigned int		int_vref;
45 };
46 
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  */
55 
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;
62 };
63 
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,
79 };
80 
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)
83 {
84 	struct ad5380_state *st = iio_priv(indio_dev);
85 
86 	return sprintf(buf, "%d\n", st->pwr_down);
87 }
88 
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)
92 {
93 	struct ad5380_state *st = iio_priv(indio_dev);
94 	bool pwr_down;
95 	int ret;
96 
97 	ret = strtobool(buf, &pwr_down);
98 	if (ret)
99 		return ret;
100 
101 	mutex_lock(&indio_dev->mlock);
102 
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);
107 
108 	st->pwr_down = pwr_down;
109 
110 	mutex_unlock(&indio_dev->mlock);
111 
112 	return ret ? ret : len;
113 }
114 
115 static const char * const ad5380_powerdown_modes[] = {
116 	"100kohm_to_gnd",
117 	"three_state",
118 };
119 
120 static int ad5380_get_powerdown_mode(struct iio_dev *indio_dev,
121 	const struct iio_chan_spec *chan)
122 {
123 	struct ad5380_state *st = iio_priv(indio_dev);
124 	unsigned int mode;
125 	int ret;
126 
127 	ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
128 	if (ret)
129 		return ret;
130 
131 	mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1;
132 
133 	return mode;
134 }
135 
136 static int ad5380_set_powerdown_mode(struct iio_dev *indio_dev,
137 	const struct iio_chan_spec *chan, unsigned int mode)
138 {
139 	struct ad5380_state *st = iio_priv(indio_dev);
140 	int ret;
141 
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);
145 
146 	return ret;
147 }
148 
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,
154 };
155 
156 static unsigned int ad5380_info_to_reg(struct iio_chan_spec const *chan,
157 	long info)
158 {
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;
168 	}
169 
170 	return 0;
171 }
172 
173 static int ad5380_write_raw(struct iio_dev *indio_dev,
174 	struct iio_chan_spec const *chan, int val, int val2, long info)
175 {
176 	const unsigned int max_val = (1 << chan->scan_type.realbits);
177 	struct ad5380_state *st = iio_priv(indio_dev);
178 
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;
184 
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;
192 
193 		return regmap_write(st->regmap,
194 			AD5380_REG_OFFSET(chan->address),
195 			val << chan->scan_type.shift);
196 	default:
197 		break;
198 	}
199 	return -EINVAL;
200 }
201 
202 static int ad5380_read_raw(struct iio_dev *indio_dev,
203 	struct iio_chan_spec const *chan, int *val, int *val2, long info)
204 {
205 	struct ad5380_state *st = iio_priv(indio_dev);
206 	int ret;
207 
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;
231 	}
232 
233 	return -EINVAL;
234 }
235 
236 static const struct iio_info ad5380_info = {
237 	.read_raw = ad5380_read_raw,
238 	.write_raw = ad5380_write_raw,
239 };
240 
241 static struct iio_chan_spec_ext_info ad5380_ext_info[] = {
242 	{
243 		.name = "powerdown",
244 		.read = ad5380_read_dac_powerdown,
245 		.write = ad5380_write_dac_powerdown,
246 		.shared = IIO_SEPARATE,
247 	},
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 	{ },
252 };
253 
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,				\
269 }
270 
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,
276 	},
277 	[ID_AD5380_5] = {
278 		.channel_template = AD5380_CHANNEL(14),
279 		.num_channels = 40,
280 		.int_vref = 2500,
281 	},
282 	[ID_AD5381_3] = {
283 		.channel_template = AD5380_CHANNEL(12),
284 		.num_channels = 16,
285 		.int_vref = 1250,
286 	},
287 	[ID_AD5381_5] = {
288 		.channel_template = AD5380_CHANNEL(12),
289 		.num_channels = 16,
290 		.int_vref = 2500,
291 	},
292 	[ID_AD5382_3] = {
293 		.channel_template = AD5380_CHANNEL(14),
294 		.num_channels = 32,
295 		.int_vref = 1250,
296 	},
297 	[ID_AD5382_5] = {
298 		.channel_template = AD5380_CHANNEL(14),
299 		.num_channels = 32,
300 		.int_vref = 2500,
301 	},
302 	[ID_AD5383_3] = {
303 		.channel_template = AD5380_CHANNEL(12),
304 		.num_channels = 32,
305 		.int_vref = 1250,
306 	},
307 	[ID_AD5383_5] = {
308 		.channel_template = AD5380_CHANNEL(12),
309 		.num_channels = 32,
310 		.int_vref = 2500,
311 	},
312 	[ID_AD5390_3] = {
313 		.channel_template = AD5380_CHANNEL(14),
314 		.num_channels = 16,
315 		.int_vref = 1250,
316 	},
317 	[ID_AD5390_5] = {
318 		.channel_template = AD5380_CHANNEL(14),
319 		.num_channels = 16,
320 		.int_vref = 2500,
321 	},
322 	[ID_AD5391_3] = {
323 		.channel_template = AD5380_CHANNEL(12),
324 		.num_channels = 16,
325 		.int_vref = 1250,
326 	},
327 	[ID_AD5391_5] = {
328 		.channel_template = AD5380_CHANNEL(12),
329 		.num_channels = 16,
330 		.int_vref = 2500,
331 	},
332 	[ID_AD5392_3] = {
333 		.channel_template = AD5380_CHANNEL(14),
334 		.num_channels = 8,
335 		.int_vref = 1250,
336 	},
337 	[ID_AD5392_5] = {
338 		.channel_template = AD5380_CHANNEL(14),
339 		.num_channels = 8,
340 		.int_vref = 2500,
341 	},
342 };
343 
344 static int ad5380_alloc_channels(struct iio_dev *indio_dev)
345 {
346 	struct ad5380_state *st = iio_priv(indio_dev);
347 	struct iio_chan_spec *channels;
348 	unsigned int i;
349 
350 	channels = kcalloc(st->chip_info->num_channels,
351 			   sizeof(struct iio_chan_spec), GFP_KERNEL);
352 
353 	if (!channels)
354 		return -ENOMEM;
355 
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;
360 	}
361 
362 	indio_dev->channels = channels;
363 
364 	return 0;
365 }
366 
367 static int ad5380_probe(struct device *dev, struct regmap *regmap,
368 			enum ad5380_type type, const char *name)
369 {
370 	struct iio_dev *indio_dev;
371 	struct ad5380_state *st;
372 	unsigned int ctrl = 0;
373 	int ret;
374 
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;
379 	}
380 
381 	st = iio_priv(indio_dev);
382 	dev_set_drvdata(dev, indio_dev);
383 
384 	st->chip_info = &ad5380_chip_info_tbl[type];
385 	st->regmap = regmap;
386 
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;
392 
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;
397 	}
398 
399 	if (st->chip_info->int_vref == 2500)
400 		ctrl |= AD5380_CTRL_INT_VREF_2V5;
401 
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;
409 		}
410 
411 		ret = regulator_get_voltage(st->vref_reg);
412 		if (ret < 0)
413 			goto error_disable_reg;
414 
415 		st->vref = ret / 1000;
416 	} else {
417 		st->vref = st->chip_info->int_vref;
418 		ctrl |= AD5380_CTRL_INT_VREF_EN;
419 	}
420 
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;
425 	}
426 
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;
431 	}
432 
433 	return 0;
434 
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);
440 
441 	return ret;
442 }
443 
444 static int ad5380_remove(struct device *dev)
445 {
446 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
447 	struct ad5380_state *st = iio_priv(indio_dev);
448 
449 	iio_device_unregister(indio_dev);
450 
451 	kfree(indio_dev->channels);
452 
453 	if (!IS_ERR(st->vref_reg)) {
454 		regulator_disable(st->vref_reg);
455 	}
456 
457 	return 0;
458 }
459 
460 static bool ad5380_reg_false(struct device *dev, unsigned int reg)
461 {
462 	return false;
463 }
464 
465 static const struct regmap_config ad5380_regmap_config = {
466 	.reg_bits = 10,
467 	.val_bits = 14,
468 
469 	.max_register = AD5380_REG_DATA(40),
470 	.cache_type = REGCACHE_RBTREE,
471 
472 	.volatile_reg = ad5380_reg_false,
473 	.readable_reg = ad5380_reg_false,
474 };
475 
476 #if IS_ENABLED(CONFIG_SPI_MASTER)
477 
478 static int ad5380_spi_probe(struct spi_device *spi)
479 {
480 	const struct spi_device_id *id = spi_get_device_id(spi);
481 	struct regmap *regmap;
482 
483 	regmap = devm_regmap_init_spi(spi, &ad5380_regmap_config);
484 
485 	if (IS_ERR(regmap))
486 		return PTR_ERR(regmap);
487 
488 	return ad5380_probe(&spi->dev, regmap, id->driver_data, id->name);
489 }
490 
491 static int ad5380_spi_remove(struct spi_device *spi)
492 {
493 	return ad5380_remove(&spi->dev);
494 }
495 
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 },
513 	{ }
514 };
515 MODULE_DEVICE_TABLE(spi, ad5380_spi_ids);
516 
517 static struct spi_driver ad5380_spi_driver = {
518 	.driver = {
519 		   .name = "ad5380",
520 	},
521 	.probe = ad5380_spi_probe,
522 	.remove = ad5380_spi_remove,
523 	.id_table = ad5380_spi_ids,
524 };
525 
526 static inline int ad5380_spi_register_driver(void)
527 {
528 	return spi_register_driver(&ad5380_spi_driver);
529 }
530 
531 static inline void ad5380_spi_unregister_driver(void)
532 {
533 	spi_unregister_driver(&ad5380_spi_driver);
534 }
535 
536 #else
537 
538 static inline int ad5380_spi_register_driver(void)
539 {
540 	return 0;
541 }
542 
543 static inline void ad5380_spi_unregister_driver(void)
544 {
545 }
546 
547 #endif
548 
549 #if IS_ENABLED(CONFIG_I2C)
550 
551 static int ad5380_i2c_probe(struct i2c_client *i2c,
552 			    const struct i2c_device_id *id)
553 {
554 	struct regmap *regmap;
555 
556 	regmap = devm_regmap_init_i2c(i2c, &ad5380_regmap_config);
557 
558 	if (IS_ERR(regmap))
559 		return PTR_ERR(regmap);
560 
561 	return ad5380_probe(&i2c->dev, regmap, id->driver_data, id->name);
562 }
563 
564 static int ad5380_i2c_remove(struct i2c_client *i2c)
565 {
566 	return ad5380_remove(&i2c->dev);
567 }
568 
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 },
586 	{ }
587 };
588 MODULE_DEVICE_TABLE(i2c, ad5380_i2c_ids);
589 
590 static struct i2c_driver ad5380_i2c_driver = {
591 	.driver = {
592 		   .name = "ad5380",
593 	},
594 	.probe = ad5380_i2c_probe,
595 	.remove = ad5380_i2c_remove,
596 	.id_table = ad5380_i2c_ids,
597 };
598 
599 static inline int ad5380_i2c_register_driver(void)
600 {
601 	return i2c_add_driver(&ad5380_i2c_driver);
602 }
603 
604 static inline void ad5380_i2c_unregister_driver(void)
605 {
606 	i2c_del_driver(&ad5380_i2c_driver);
607 }
608 
609 #else
610 
611 static inline int ad5380_i2c_register_driver(void)
612 {
613 	return 0;
614 }
615 
616 static inline void ad5380_i2c_unregister_driver(void)
617 {
618 }
619 
620 #endif
621 
622 static int __init ad5380_spi_init(void)
623 {
624 	int ret;
625 
626 	ret = ad5380_spi_register_driver();
627 	if (ret)
628 		return ret;
629 
630 	ret = ad5380_i2c_register_driver();
631 	if (ret) {
632 		ad5380_spi_unregister_driver();
633 		return ret;
634 	}
635 
636 	return 0;
637 }
638 module_init(ad5380_spi_init);
639 
640 static void __exit ad5380_spi_exit(void)
641 {
642 	ad5380_i2c_unregister_driver();
643 	ad5380_spi_unregister_driver();
644 
645 }
646 module_exit(ad5380_spi_exit);
647 
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");
651