xref: /linux/drivers/iio/dac/ad5446.c (revision c0e79fd89749b0cda1c72049e2772dd2eeada86f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * AD5446 SPI DAC driver
4  *
5  * Copyright 2010 Analog Devices Inc.
6  */
7 
8 #include <linux/interrupt.h>
9 #include <linux/workqueue.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/list.h>
15 #include <linux/spi/spi.h>
16 #include <linux/i2c.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
19 #include <linux/module.h>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 
24 #define MODE_PWRDWN_1k		0x1
25 #define MODE_PWRDWN_100k	0x2
26 #define MODE_PWRDWN_TRISTATE	0x3
27 
28 /**
29  * struct ad5446_state - driver instance specific data
30  * @spi:		spi_device
31  * @chip_info:		chip model specific constants, available modes etc
32  * @reg:		supply regulator
33  * @vref_mv:		actual reference voltage used
34  */
35 
36 struct ad5446_state {
37 	struct device		*dev;
38 	const struct ad5446_chip_info	*chip_info;
39 	struct regulator		*reg;
40 	unsigned short			vref_mv;
41 	unsigned			cached_val;
42 	unsigned			pwr_down_mode;
43 	unsigned			pwr_down;
44 };
45 
46 /**
47  * struct ad5446_chip_info - chip specific information
48  * @channel:		channel spec for the DAC
49  * @int_vref_mv:	AD5620/40/60: the internal reference voltage
50  * @write:		chip specific helper function to write to the register
51  */
52 
53 struct ad5446_chip_info {
54 	struct iio_chan_spec	channel;
55 	u16			int_vref_mv;
56 	int			(*write)(struct ad5446_state *st, unsigned val);
57 };
58 
59 static const char * const ad5446_powerdown_modes[] = {
60 	"1kohm_to_gnd", "100kohm_to_gnd", "three_state"
61 };
62 
63 static int ad5446_set_powerdown_mode(struct iio_dev *indio_dev,
64 	const struct iio_chan_spec *chan, unsigned int mode)
65 {
66 	struct ad5446_state *st = iio_priv(indio_dev);
67 
68 	st->pwr_down_mode = mode + 1;
69 
70 	return 0;
71 }
72 
73 static int ad5446_get_powerdown_mode(struct iio_dev *indio_dev,
74 	const struct iio_chan_spec *chan)
75 {
76 	struct ad5446_state *st = iio_priv(indio_dev);
77 
78 	return st->pwr_down_mode - 1;
79 }
80 
81 static const struct iio_enum ad5446_powerdown_mode_enum = {
82 	.items = ad5446_powerdown_modes,
83 	.num_items = ARRAY_SIZE(ad5446_powerdown_modes),
84 	.get = ad5446_get_powerdown_mode,
85 	.set = ad5446_set_powerdown_mode,
86 };
87 
88 static ssize_t ad5446_read_dac_powerdown(struct iio_dev *indio_dev,
89 					   uintptr_t private,
90 					   const struct iio_chan_spec *chan,
91 					   char *buf)
92 {
93 	struct ad5446_state *st = iio_priv(indio_dev);
94 
95 	return sprintf(buf, "%d\n", st->pwr_down);
96 }
97 
98 static ssize_t ad5446_write_dac_powerdown(struct iio_dev *indio_dev,
99 					    uintptr_t private,
100 					    const struct iio_chan_spec *chan,
101 					    const char *buf, size_t len)
102 {
103 	struct ad5446_state *st = iio_priv(indio_dev);
104 	unsigned int shift;
105 	unsigned int val;
106 	bool powerdown;
107 	int ret;
108 
109 	ret = strtobool(buf, &powerdown);
110 	if (ret)
111 		return ret;
112 
113 	mutex_lock(&indio_dev->mlock);
114 	st->pwr_down = powerdown;
115 
116 	if (st->pwr_down) {
117 		shift = chan->scan_type.realbits + chan->scan_type.shift;
118 		val = st->pwr_down_mode << shift;
119 	} else {
120 		val = st->cached_val;
121 	}
122 
123 	ret = st->chip_info->write(st, val);
124 	mutex_unlock(&indio_dev->mlock);
125 
126 	return ret ? ret : len;
127 }
128 
129 static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown[] = {
130 	{
131 		.name = "powerdown",
132 		.read = ad5446_read_dac_powerdown,
133 		.write = ad5446_write_dac_powerdown,
134 		.shared = IIO_SEPARATE,
135 	},
136 	IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5446_powerdown_mode_enum),
137 	IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum),
138 	{ },
139 };
140 
141 #define _AD5446_CHANNEL(bits, storage, _shift, ext) { \
142 	.type = IIO_VOLTAGE, \
143 	.indexed = 1, \
144 	.output = 1, \
145 	.channel = 0, \
146 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
147 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
148 	.scan_type = { \
149 		.sign = 'u', \
150 		.realbits = (bits), \
151 		.storagebits = (storage), \
152 		.shift = (_shift), \
153 		}, \
154 	.ext_info = (ext), \
155 }
156 
157 #define AD5446_CHANNEL(bits, storage, shift) \
158 	_AD5446_CHANNEL(bits, storage, shift, NULL)
159 
160 #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \
161 	_AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown)
162 
163 static int ad5446_read_raw(struct iio_dev *indio_dev,
164 			   struct iio_chan_spec const *chan,
165 			   int *val,
166 			   int *val2,
167 			   long m)
168 {
169 	struct ad5446_state *st = iio_priv(indio_dev);
170 
171 	switch (m) {
172 	case IIO_CHAN_INFO_RAW:
173 		*val = st->cached_val;
174 		return IIO_VAL_INT;
175 	case IIO_CHAN_INFO_SCALE:
176 		*val = st->vref_mv;
177 		*val2 = chan->scan_type.realbits;
178 		return IIO_VAL_FRACTIONAL_LOG2;
179 	}
180 	return -EINVAL;
181 }
182 
183 static int ad5446_write_raw(struct iio_dev *indio_dev,
184 			       struct iio_chan_spec const *chan,
185 			       int val,
186 			       int val2,
187 			       long mask)
188 {
189 	struct ad5446_state *st = iio_priv(indio_dev);
190 	int ret = 0;
191 
192 	switch (mask) {
193 	case IIO_CHAN_INFO_RAW:
194 		if (val >= (1 << chan->scan_type.realbits) || val < 0)
195 			return -EINVAL;
196 
197 		val <<= chan->scan_type.shift;
198 		mutex_lock(&indio_dev->mlock);
199 		st->cached_val = val;
200 		if (!st->pwr_down)
201 			ret = st->chip_info->write(st, val);
202 		mutex_unlock(&indio_dev->mlock);
203 		break;
204 	default:
205 		ret = -EINVAL;
206 	}
207 
208 	return ret;
209 }
210 
211 static const struct iio_info ad5446_info = {
212 	.read_raw = ad5446_read_raw,
213 	.write_raw = ad5446_write_raw,
214 };
215 
216 static int ad5446_probe(struct device *dev, const char *name,
217 			const struct ad5446_chip_info *chip_info)
218 {
219 	struct ad5446_state *st;
220 	struct iio_dev *indio_dev;
221 	struct regulator *reg;
222 	int ret, voltage_uv = 0;
223 
224 	reg = devm_regulator_get(dev, "vcc");
225 	if (!IS_ERR(reg)) {
226 		ret = regulator_enable(reg);
227 		if (ret)
228 			return ret;
229 
230 		ret = regulator_get_voltage(reg);
231 		if (ret < 0)
232 			goto error_disable_reg;
233 
234 		voltage_uv = ret;
235 	}
236 
237 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
238 	if (indio_dev == NULL) {
239 		ret = -ENOMEM;
240 		goto error_disable_reg;
241 	}
242 	st = iio_priv(indio_dev);
243 	st->chip_info = chip_info;
244 
245 	dev_set_drvdata(dev, indio_dev);
246 	st->reg = reg;
247 	st->dev = dev;
248 
249 	/* Establish that the iio_dev is a child of the device */
250 	indio_dev->dev.parent = dev;
251 	indio_dev->name = name;
252 	indio_dev->info = &ad5446_info;
253 	indio_dev->modes = INDIO_DIRECT_MODE;
254 	indio_dev->channels = &st->chip_info->channel;
255 	indio_dev->num_channels = 1;
256 
257 	st->pwr_down_mode = MODE_PWRDWN_1k;
258 
259 	if (st->chip_info->int_vref_mv)
260 		st->vref_mv = st->chip_info->int_vref_mv;
261 	else if (voltage_uv)
262 		st->vref_mv = voltage_uv / 1000;
263 	else
264 		dev_warn(dev, "reference voltage unspecified\n");
265 
266 	ret = iio_device_register(indio_dev);
267 	if (ret)
268 		goto error_disable_reg;
269 
270 	return 0;
271 
272 error_disable_reg:
273 	if (!IS_ERR(reg))
274 		regulator_disable(reg);
275 	return ret;
276 }
277 
278 static int ad5446_remove(struct device *dev)
279 {
280 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
281 	struct ad5446_state *st = iio_priv(indio_dev);
282 
283 	iio_device_unregister(indio_dev);
284 	if (!IS_ERR(st->reg))
285 		regulator_disable(st->reg);
286 
287 	return 0;
288 }
289 
290 #if IS_ENABLED(CONFIG_SPI_MASTER)
291 
292 static int ad5446_write(struct ad5446_state *st, unsigned val)
293 {
294 	struct spi_device *spi = to_spi_device(st->dev);
295 	__be16 data = cpu_to_be16(val);
296 
297 	return spi_write(spi, &data, sizeof(data));
298 }
299 
300 static int ad5660_write(struct ad5446_state *st, unsigned val)
301 {
302 	struct spi_device *spi = to_spi_device(st->dev);
303 	uint8_t data[3];
304 
305 	data[0] = (val >> 16) & 0xFF;
306 	data[1] = (val >> 8) & 0xFF;
307 	data[2] = val & 0xFF;
308 
309 	return spi_write(spi, data, sizeof(data));
310 }
311 
312 /**
313  * ad5446_supported_spi_device_ids:
314  * The AD5620/40/60 parts are available in different fixed internal reference
315  * voltage options. The actual part numbers may look differently
316  * (and a bit cryptic), however this style is used to make clear which
317  * parts are supported here.
318  */
319 enum ad5446_supported_spi_device_ids {
320 	ID_AD5300,
321 	ID_AD5310,
322 	ID_AD5320,
323 	ID_AD5444,
324 	ID_AD5446,
325 	ID_AD5450,
326 	ID_AD5451,
327 	ID_AD5541A,
328 	ID_AD5512A,
329 	ID_AD5553,
330 	ID_AD5600,
331 	ID_AD5601,
332 	ID_AD5611,
333 	ID_AD5621,
334 	ID_AD5641,
335 	ID_AD5620_2500,
336 	ID_AD5620_1250,
337 	ID_AD5640_2500,
338 	ID_AD5640_1250,
339 	ID_AD5660_2500,
340 	ID_AD5660_1250,
341 	ID_AD5662,
342 };
343 
344 static const struct ad5446_chip_info ad5446_spi_chip_info[] = {
345 	[ID_AD5300] = {
346 		.channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
347 		.write = ad5446_write,
348 	},
349 	[ID_AD5310] = {
350 		.channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
351 		.write = ad5446_write,
352 	},
353 	[ID_AD5320] = {
354 		.channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
355 		.write = ad5446_write,
356 	},
357 	[ID_AD5444] = {
358 		.channel = AD5446_CHANNEL(12, 16, 2),
359 		.write = ad5446_write,
360 	},
361 	[ID_AD5446] = {
362 		.channel = AD5446_CHANNEL(14, 16, 0),
363 		.write = ad5446_write,
364 	},
365 	[ID_AD5450] = {
366 		.channel = AD5446_CHANNEL(8, 16, 6),
367 		.write = ad5446_write,
368 	},
369 	[ID_AD5451] = {
370 		.channel = AD5446_CHANNEL(10, 16, 4),
371 		.write = ad5446_write,
372 	},
373 	[ID_AD5541A] = {
374 		.channel = AD5446_CHANNEL(16, 16, 0),
375 		.write = ad5446_write,
376 	},
377 	[ID_AD5512A] = {
378 		.channel = AD5446_CHANNEL(12, 16, 4),
379 		.write = ad5446_write,
380 	},
381 	[ID_AD5553] = {
382 		.channel = AD5446_CHANNEL(14, 16, 0),
383 		.write = ad5446_write,
384 	},
385 	[ID_AD5600] = {
386 		.channel = AD5446_CHANNEL(16, 16, 0),
387 		.write = ad5446_write,
388 	},
389 	[ID_AD5601] = {
390 		.channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6),
391 		.write = ad5446_write,
392 	},
393 	[ID_AD5611] = {
394 		.channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4),
395 		.write = ad5446_write,
396 	},
397 	[ID_AD5621] = {
398 		.channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
399 		.write = ad5446_write,
400 	},
401 	[ID_AD5641] = {
402 		.channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
403 		.write = ad5446_write,
404 	},
405 	[ID_AD5620_2500] = {
406 		.channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
407 		.int_vref_mv = 2500,
408 		.write = ad5446_write,
409 	},
410 	[ID_AD5620_1250] = {
411 		.channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
412 		.int_vref_mv = 1250,
413 		.write = ad5446_write,
414 	},
415 	[ID_AD5640_2500] = {
416 		.channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
417 		.int_vref_mv = 2500,
418 		.write = ad5446_write,
419 	},
420 	[ID_AD5640_1250] = {
421 		.channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
422 		.int_vref_mv = 1250,
423 		.write = ad5446_write,
424 	},
425 	[ID_AD5660_2500] = {
426 		.channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
427 		.int_vref_mv = 2500,
428 		.write = ad5660_write,
429 	},
430 	[ID_AD5660_1250] = {
431 		.channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
432 		.int_vref_mv = 1250,
433 		.write = ad5660_write,
434 	},
435 	[ID_AD5662] = {
436 		.channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
437 		.write = ad5660_write,
438 	},
439 };
440 
441 static const struct spi_device_id ad5446_spi_ids[] = {
442 	{"ad5300", ID_AD5300},
443 	{"ad5310", ID_AD5310},
444 	{"ad5320", ID_AD5320},
445 	{"ad5444", ID_AD5444},
446 	{"ad5446", ID_AD5446},
447 	{"ad5450", ID_AD5450},
448 	{"ad5451", ID_AD5451},
449 	{"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */
450 	{"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */
451 	{"ad5512a", ID_AD5512A},
452 	{"ad5541a", ID_AD5541A},
453 	{"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */
454 	{"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */
455 	{"ad5553", ID_AD5553},
456 	{"ad5600", ID_AD5600},
457 	{"ad5601", ID_AD5601},
458 	{"ad5611", ID_AD5611},
459 	{"ad5621", ID_AD5621},
460 	{"ad5641", ID_AD5641},
461 	{"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
462 	{"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
463 	{"ad5640-2500", ID_AD5640_2500},
464 	{"ad5640-1250", ID_AD5640_1250},
465 	{"ad5660-2500", ID_AD5660_2500},
466 	{"ad5660-1250", ID_AD5660_1250},
467 	{"ad5662", ID_AD5662},
468 	{"dac081s101", ID_AD5300}, /* compatible Texas Instruments chips */
469 	{"dac101s101", ID_AD5310},
470 	{"dac121s101", ID_AD5320},
471 	{"dac7512", ID_AD5320},
472 	{}
473 };
474 MODULE_DEVICE_TABLE(spi, ad5446_spi_ids);
475 
476 #ifdef CONFIG_OF
477 static const struct of_device_id ad5446_of_ids[] = {
478 	{ .compatible = "ti,dac7512" },
479 	{ }
480 };
481 MODULE_DEVICE_TABLE(of, ad5446_of_ids);
482 #endif
483 
484 static int ad5446_spi_probe(struct spi_device *spi)
485 {
486 	const struct spi_device_id *id = spi_get_device_id(spi);
487 
488 	return ad5446_probe(&spi->dev, id->name,
489 		&ad5446_spi_chip_info[id->driver_data]);
490 }
491 
492 static int ad5446_spi_remove(struct spi_device *spi)
493 {
494 	return ad5446_remove(&spi->dev);
495 }
496 
497 static struct spi_driver ad5446_spi_driver = {
498 	.driver = {
499 		.name	= "ad5446",
500 		.of_match_table = of_match_ptr(ad5446_of_ids),
501 	},
502 	.probe		= ad5446_spi_probe,
503 	.remove		= ad5446_spi_remove,
504 	.id_table	= ad5446_spi_ids,
505 };
506 
507 static int __init ad5446_spi_register_driver(void)
508 {
509 	return spi_register_driver(&ad5446_spi_driver);
510 }
511 
512 static void ad5446_spi_unregister_driver(void)
513 {
514 	spi_unregister_driver(&ad5446_spi_driver);
515 }
516 
517 #else
518 
519 static inline int ad5446_spi_register_driver(void) { return 0; }
520 static inline void ad5446_spi_unregister_driver(void) { }
521 
522 #endif
523 
524 #if IS_ENABLED(CONFIG_I2C)
525 
526 static int ad5622_write(struct ad5446_state *st, unsigned val)
527 {
528 	struct i2c_client *client = to_i2c_client(st->dev);
529 	__be16 data = cpu_to_be16(val);
530 
531 	return i2c_master_send(client, (char *)&data, sizeof(data));
532 }
533 
534 /**
535  * ad5446_supported_i2c_device_ids:
536  * The AD5620/40/60 parts are available in different fixed internal reference
537  * voltage options. The actual part numbers may look differently
538  * (and a bit cryptic), however this style is used to make clear which
539  * parts are supported here.
540  */
541 enum ad5446_supported_i2c_device_ids {
542 	ID_AD5602,
543 	ID_AD5612,
544 	ID_AD5622,
545 };
546 
547 static const struct ad5446_chip_info ad5446_i2c_chip_info[] = {
548 	[ID_AD5602] = {
549 		.channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
550 		.write = ad5622_write,
551 	},
552 	[ID_AD5612] = {
553 		.channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
554 		.write = ad5622_write,
555 	},
556 	[ID_AD5622] = {
557 		.channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
558 		.write = ad5622_write,
559 	},
560 };
561 
562 static int ad5446_i2c_probe(struct i2c_client *i2c,
563 			    const struct i2c_device_id *id)
564 {
565 	return ad5446_probe(&i2c->dev, id->name,
566 		&ad5446_i2c_chip_info[id->driver_data]);
567 }
568 
569 static int ad5446_i2c_remove(struct i2c_client *i2c)
570 {
571 	return ad5446_remove(&i2c->dev);
572 }
573 
574 static const struct i2c_device_id ad5446_i2c_ids[] = {
575 	{"ad5301", ID_AD5602},
576 	{"ad5311", ID_AD5612},
577 	{"ad5321", ID_AD5622},
578 	{"ad5602", ID_AD5602},
579 	{"ad5612", ID_AD5612},
580 	{"ad5622", ID_AD5622},
581 	{}
582 };
583 MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids);
584 
585 static struct i2c_driver ad5446_i2c_driver = {
586 	.driver = {
587 		   .name = "ad5446",
588 	},
589 	.probe = ad5446_i2c_probe,
590 	.remove = ad5446_i2c_remove,
591 	.id_table = ad5446_i2c_ids,
592 };
593 
594 static int __init ad5446_i2c_register_driver(void)
595 {
596 	return i2c_add_driver(&ad5446_i2c_driver);
597 }
598 
599 static void __exit ad5446_i2c_unregister_driver(void)
600 {
601 	i2c_del_driver(&ad5446_i2c_driver);
602 }
603 
604 #else
605 
606 static inline int ad5446_i2c_register_driver(void) { return 0; }
607 static inline void ad5446_i2c_unregister_driver(void) { }
608 
609 #endif
610 
611 static int __init ad5446_init(void)
612 {
613 	int ret;
614 
615 	ret = ad5446_spi_register_driver();
616 	if (ret)
617 		return ret;
618 
619 	ret = ad5446_i2c_register_driver();
620 	if (ret) {
621 		ad5446_spi_unregister_driver();
622 		return ret;
623 	}
624 
625 	return 0;
626 }
627 module_init(ad5446_init);
628 
629 static void __exit ad5446_exit(void)
630 {
631 	ad5446_i2c_unregister_driver();
632 	ad5446_spi_unregister_driver();
633 }
634 module_exit(ad5446_exit);
635 
636 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
637 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
638 MODULE_LICENSE("GPL v2");
639