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