xref: /linux/drivers/iio/dac/ad5380.c (revision c8b90d40d5bba8e6fba457b8a7c10d3c0d467e37)
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 struct ad5380_chip_info {
41 	struct iio_chan_spec	channel_template;
42 	unsigned int		num_channels;
43 	unsigned int		int_vref;
44 };
45 
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
53  */
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;
60 };
61 
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,
77 };
78 
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)
81 {
82 	struct ad5380_state *st = iio_priv(indio_dev);
83 
84 	return sysfs_emit(buf, "%d\n", st->pwr_down);
85 }
86 
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)
90 {
91 	struct ad5380_state *st = iio_priv(indio_dev);
92 	bool pwr_down;
93 	int ret;
94 
95 	ret = kstrtobool(buf, &pwr_down);
96 	if (ret)
97 		return ret;
98 
99 	mutex_lock(&st->lock);
100 
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);
105 
106 	st->pwr_down = pwr_down;
107 
108 	mutex_unlock(&st->lock);
109 
110 	return ret ? ret : len;
111 }
112 
113 static const char * const ad5380_powerdown_modes[] = {
114 	"100kohm_to_gnd",
115 	"three_state",
116 };
117 
118 static int ad5380_get_powerdown_mode(struct iio_dev *indio_dev,
119 	const struct iio_chan_spec *chan)
120 {
121 	struct ad5380_state *st = iio_priv(indio_dev);
122 	unsigned int mode;
123 	int ret;
124 
125 	ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode);
126 	if (ret)
127 		return ret;
128 
129 	mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1;
130 
131 	return mode;
132 }
133 
134 static int ad5380_set_powerdown_mode(struct iio_dev *indio_dev,
135 	const struct iio_chan_spec *chan, unsigned int mode)
136 {
137 	struct ad5380_state *st = iio_priv(indio_dev);
138 	int ret;
139 
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);
143 
144 	return ret;
145 }
146 
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,
152 };
153 
154 static unsigned int ad5380_info_to_reg(struct iio_chan_spec const *chan,
155 	long info)
156 {
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;
166 	}
167 
168 	return 0;
169 }
170 
171 static int ad5380_write_raw(struct iio_dev *indio_dev,
172 	struct iio_chan_spec const *chan, int val, int val2, long info)
173 {
174 	const unsigned int max_val = (1 << chan->scan_type.realbits);
175 	struct ad5380_state *st = iio_priv(indio_dev);
176 
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;
182 
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;
190 
191 		return regmap_write(st->regmap,
192 			AD5380_REG_OFFSET(chan->address),
193 			val << chan->scan_type.shift);
194 	default:
195 		break;
196 	}
197 	return -EINVAL;
198 }
199 
200 static int ad5380_read_raw(struct iio_dev *indio_dev,
201 	struct iio_chan_spec const *chan, int *val, int *val2, long info)
202 {
203 	struct ad5380_state *st = iio_priv(indio_dev);
204 	int ret;
205 
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;
229 	}
230 
231 	return -EINVAL;
232 }
233 
234 static const struct iio_info ad5380_info = {
235 	.read_raw = ad5380_read_raw,
236 	.write_raw = ad5380_write_raw,
237 };
238 
239 static const struct iio_chan_spec_ext_info ad5380_ext_info[] = {
240 	{
241 		.name = "powerdown",
242 		.read = ad5380_read_dac_powerdown,
243 		.write = ad5380_write_dac_powerdown,
244 		.shared = IIO_SEPARATE,
245 	},
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 	{ },
250 };
251 
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,				\
267 }
268 
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,
274 	},
275 	[ID_AD5380_5] = {
276 		.channel_template = AD5380_CHANNEL(14),
277 		.num_channels = 40,
278 		.int_vref = 2500,
279 	},
280 	[ID_AD5381_3] = {
281 		.channel_template = AD5380_CHANNEL(12),
282 		.num_channels = 16,
283 		.int_vref = 1250,
284 	},
285 	[ID_AD5381_5] = {
286 		.channel_template = AD5380_CHANNEL(12),
287 		.num_channels = 16,
288 		.int_vref = 2500,
289 	},
290 	[ID_AD5382_3] = {
291 		.channel_template = AD5380_CHANNEL(14),
292 		.num_channels = 32,
293 		.int_vref = 1250,
294 	},
295 	[ID_AD5382_5] = {
296 		.channel_template = AD5380_CHANNEL(14),
297 		.num_channels = 32,
298 		.int_vref = 2500,
299 	},
300 	[ID_AD5383_3] = {
301 		.channel_template = AD5380_CHANNEL(12),
302 		.num_channels = 32,
303 		.int_vref = 1250,
304 	},
305 	[ID_AD5383_5] = {
306 		.channel_template = AD5380_CHANNEL(12),
307 		.num_channels = 32,
308 		.int_vref = 2500,
309 	},
310 	[ID_AD5390_3] = {
311 		.channel_template = AD5380_CHANNEL(14),
312 		.num_channels = 16,
313 		.int_vref = 1250,
314 	},
315 	[ID_AD5390_5] = {
316 		.channel_template = AD5380_CHANNEL(14),
317 		.num_channels = 16,
318 		.int_vref = 2500,
319 	},
320 	[ID_AD5391_3] = {
321 		.channel_template = AD5380_CHANNEL(12),
322 		.num_channels = 16,
323 		.int_vref = 1250,
324 	},
325 	[ID_AD5391_5] = {
326 		.channel_template = AD5380_CHANNEL(12),
327 		.num_channels = 16,
328 		.int_vref = 2500,
329 	},
330 	[ID_AD5392_3] = {
331 		.channel_template = AD5380_CHANNEL(14),
332 		.num_channels = 8,
333 		.int_vref = 1250,
334 	},
335 	[ID_AD5392_5] = {
336 		.channel_template = AD5380_CHANNEL(14),
337 		.num_channels = 8,
338 		.int_vref = 2500,
339 	},
340 };
341 
342 static int ad5380_alloc_channels(struct device *dev, struct iio_dev *indio_dev)
343 {
344 	struct ad5380_state *st = iio_priv(indio_dev);
345 	struct iio_chan_spec *channels;
346 	unsigned int i;
347 
348 	channels = devm_kcalloc(dev, st->chip_info->num_channels,
349 				sizeof(struct iio_chan_spec), GFP_KERNEL);
350 
351 	if (!channels)
352 		return -ENOMEM;
353 
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;
358 	}
359 
360 	indio_dev->channels = channels;
361 
362 	return 0;
363 }
364 
365 static int ad5380_probe(struct device *dev, struct regmap *regmap,
366 			enum ad5380_type type, const char *name)
367 {
368 	struct iio_dev *indio_dev;
369 	struct ad5380_state *st;
370 	unsigned int ctrl = 0;
371 	int ret;
372 
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;
377 	}
378 
379 	st = iio_priv(indio_dev);
380 
381 	st->chip_info = &ad5380_chip_info_tbl[type];
382 	st->regmap = regmap;
383 
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;
388 
389 	mutex_init(&st->lock);
390 
391 	ret = ad5380_alloc_channels(dev, indio_dev);
392 	if (ret)
393 		return dev_err_probe(dev, ret, "Failed to allocate channel spec\n");
394 
395 	if (st->chip_info->int_vref == 2500)
396 		ctrl |= AD5380_CTRL_INT_VREF_2V5;
397 
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;
406 	}
407 
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");
411 
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");
415 
416 	return 0;
417 }
418 
419 static bool ad5380_reg_false(struct device *dev, unsigned int reg)
420 {
421 	return false;
422 }
423 
424 static const struct regmap_config ad5380_regmap_config = {
425 	.reg_bits = 10,
426 	.val_bits = 14,
427 
428 	.max_register = AD5380_REG_DATA(40),
429 	.cache_type = REGCACHE_RBTREE,
430 
431 	.volatile_reg = ad5380_reg_false,
432 	.readable_reg = ad5380_reg_false,
433 };
434 
435 #if IS_ENABLED(CONFIG_SPI_MASTER)
436 
437 static int ad5380_spi_probe(struct spi_device *spi)
438 {
439 	const struct spi_device_id *id = spi_get_device_id(spi);
440 	struct regmap *regmap;
441 
442 	regmap = devm_regmap_init_spi(spi, &ad5380_regmap_config);
443 
444 	if (IS_ERR(regmap))
445 		return PTR_ERR(regmap);
446 
447 	return ad5380_probe(&spi->dev, regmap, id->driver_data, id->name);
448 }
449 
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 },
467 	{ }
468 };
469 MODULE_DEVICE_TABLE(spi, ad5380_spi_ids);
470 
471 static struct spi_driver ad5380_spi_driver = {
472 	.driver = {
473 		   .name = "ad5380",
474 	},
475 	.probe = ad5380_spi_probe,
476 	.id_table = ad5380_spi_ids,
477 };
478 
479 static inline int ad5380_spi_register_driver(void)
480 {
481 	return spi_register_driver(&ad5380_spi_driver);
482 }
483 
484 static inline void ad5380_spi_unregister_driver(void)
485 {
486 	spi_unregister_driver(&ad5380_spi_driver);
487 }
488 
489 #else
490 
491 static inline int ad5380_spi_register_driver(void)
492 {
493 	return 0;
494 }
495 
496 static inline void ad5380_spi_unregister_driver(void)
497 {
498 }
499 
500 #endif
501 
502 #if IS_ENABLED(CONFIG_I2C)
503 
504 static int ad5380_i2c_probe(struct i2c_client *i2c)
505 {
506 	const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
507 	struct regmap *regmap;
508 
509 	regmap = devm_regmap_init_i2c(i2c, &ad5380_regmap_config);
510 
511 	if (IS_ERR(regmap))
512 		return PTR_ERR(regmap);
513 
514 	return ad5380_probe(&i2c->dev, regmap, id->driver_data, id->name);
515 }
516 
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 },
534 	{ }
535 };
536 MODULE_DEVICE_TABLE(i2c, ad5380_i2c_ids);
537 
538 static struct i2c_driver ad5380_i2c_driver = {
539 	.driver = {
540 		   .name = "ad5380",
541 	},
542 	.probe = ad5380_i2c_probe,
543 	.id_table = ad5380_i2c_ids,
544 };
545 
546 static inline int ad5380_i2c_register_driver(void)
547 {
548 	return i2c_add_driver(&ad5380_i2c_driver);
549 }
550 
551 static inline void ad5380_i2c_unregister_driver(void)
552 {
553 	i2c_del_driver(&ad5380_i2c_driver);
554 }
555 
556 #else
557 
558 static inline int ad5380_i2c_register_driver(void)
559 {
560 	return 0;
561 }
562 
563 static inline void ad5380_i2c_unregister_driver(void)
564 {
565 }
566 
567 #endif
568 
569 static int __init ad5380_spi_init(void)
570 {
571 	int ret;
572 
573 	ret = ad5380_spi_register_driver();
574 	if (ret)
575 		return ret;
576 
577 	ret = ad5380_i2c_register_driver();
578 	if (ret) {
579 		ad5380_spi_unregister_driver();
580 		return ret;
581 	}
582 
583 	return 0;
584 }
585 module_init(ad5380_spi_init);
586 
587 static void __exit ad5380_spi_exit(void)
588 {
589 	ad5380_i2c_unregister_driver();
590 	ad5380_spi_unregister_driver();
591 
592 }
593 module_exit(ad5380_spi_exit);
594 
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");
598