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
ad5446_set_powerdown_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,unsigned int mode)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
ad5446_get_powerdown_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan)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
ad5446_read_dac_powerdown(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,char * buf)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
ad5446_write_dac_powerdown(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,const char * buf,size_t len)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
ad5446_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)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
ad5446_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)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
ad5446_probe(struct device * dev,const char * name,const struct ad5446_chip_info * chip_info)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
ad5446_remove(struct device * dev)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
ad5446_write(struct ad5446_state * st,unsigned val)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
ad5660_write(struct ad5446_state * st,unsigned val)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_AD5601,
331 ID_AD5611,
332 ID_AD5621,
333 ID_AD5641,
334 ID_AD5620_2500,
335 ID_AD5620_1250,
336 ID_AD5640_2500,
337 ID_AD5640_1250,
338 ID_AD5660_2500,
339 ID_AD5660_1250,
340 ID_AD5662,
341 };
342
343 static const struct ad5446_chip_info ad5446_spi_chip_info[] = {
344 [ID_AD5300] = {
345 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
346 .write = ad5446_write,
347 },
348 [ID_AD5310] = {
349 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
350 .write = ad5446_write,
351 },
352 [ID_AD5320] = {
353 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
354 .write = ad5446_write,
355 },
356 [ID_AD5444] = {
357 .channel = AD5446_CHANNEL(12, 16, 2),
358 .write = ad5446_write,
359 },
360 [ID_AD5446] = {
361 .channel = AD5446_CHANNEL(14, 16, 0),
362 .write = ad5446_write,
363 },
364 [ID_AD5450] = {
365 .channel = AD5446_CHANNEL(8, 16, 6),
366 .write = ad5446_write,
367 },
368 [ID_AD5451] = {
369 .channel = AD5446_CHANNEL(10, 16, 4),
370 .write = ad5446_write,
371 },
372 [ID_AD5541A] = {
373 .channel = AD5446_CHANNEL(16, 16, 0),
374 .write = ad5446_write,
375 },
376 [ID_AD5512A] = {
377 .channel = AD5446_CHANNEL(12, 16, 4),
378 .write = ad5446_write,
379 },
380 [ID_AD5553] = {
381 .channel = AD5446_CHANNEL(14, 16, 0),
382 .write = ad5446_write,
383 },
384 [ID_AD5601] = {
385 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6),
386 .write = ad5446_write,
387 },
388 [ID_AD5611] = {
389 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4),
390 .write = ad5446_write,
391 },
392 [ID_AD5621] = {
393 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
394 .write = ad5446_write,
395 },
396 [ID_AD5641] = {
397 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
398 .write = ad5446_write,
399 },
400 [ID_AD5620_2500] = {
401 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
402 .int_vref_mv = 2500,
403 .write = ad5446_write,
404 },
405 [ID_AD5620_1250] = {
406 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
407 .int_vref_mv = 1250,
408 .write = ad5446_write,
409 },
410 [ID_AD5640_2500] = {
411 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
412 .int_vref_mv = 2500,
413 .write = ad5446_write,
414 },
415 [ID_AD5640_1250] = {
416 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
417 .int_vref_mv = 1250,
418 .write = ad5446_write,
419 },
420 [ID_AD5660_2500] = {
421 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
422 .int_vref_mv = 2500,
423 .write = ad5660_write,
424 },
425 [ID_AD5660_1250] = {
426 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
427 .int_vref_mv = 1250,
428 .write = ad5660_write,
429 },
430 [ID_AD5662] = {
431 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
432 .write = ad5660_write,
433 },
434 };
435
436 static const struct spi_device_id ad5446_spi_ids[] = {
437 {"ad5300", ID_AD5300},
438 {"ad5310", ID_AD5310},
439 {"ad5320", ID_AD5320},
440 {"ad5444", ID_AD5444},
441 {"ad5446", ID_AD5446},
442 {"ad5450", ID_AD5450},
443 {"ad5451", ID_AD5451},
444 {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */
445 {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */
446 {"ad5512a", ID_AD5512A},
447 {"ad5541a", ID_AD5541A},
448 {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */
449 {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */
450 {"ad5553", ID_AD5553},
451 {"ad5601", ID_AD5601},
452 {"ad5611", ID_AD5611},
453 {"ad5621", ID_AD5621},
454 {"ad5641", ID_AD5641},
455 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
456 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
457 {"ad5640-2500", ID_AD5640_2500},
458 {"ad5640-1250", ID_AD5640_1250},
459 {"ad5660-2500", ID_AD5660_2500},
460 {"ad5660-1250", ID_AD5660_1250},
461 {"ad5662", ID_AD5662},
462 {"dac081s101", ID_AD5300}, /* compatible Texas Instruments chips */
463 {"dac101s101", ID_AD5310},
464 {"dac121s101", ID_AD5320},
465 {"dac7512", ID_AD5320},
466 {}
467 };
468 MODULE_DEVICE_TABLE(spi, ad5446_spi_ids);
469
470 #ifdef CONFIG_OF
471 static const struct of_device_id ad5446_of_ids[] = {
472 { .compatible = "ti,dac7512" },
473 { }
474 };
475 MODULE_DEVICE_TABLE(of, ad5446_of_ids);
476 #endif
477
ad5446_spi_probe(struct spi_device * spi)478 static int ad5446_spi_probe(struct spi_device *spi)
479 {
480 const struct spi_device_id *id = spi_get_device_id(spi);
481
482 return ad5446_probe(&spi->dev, id->name,
483 &ad5446_spi_chip_info[id->driver_data]);
484 }
485
ad5446_spi_remove(struct spi_device * spi)486 static int ad5446_spi_remove(struct spi_device *spi)
487 {
488 return ad5446_remove(&spi->dev);
489 }
490
491 static struct spi_driver ad5446_spi_driver = {
492 .driver = {
493 .name = "ad5446",
494 .of_match_table = of_match_ptr(ad5446_of_ids),
495 },
496 .probe = ad5446_spi_probe,
497 .remove = ad5446_spi_remove,
498 .id_table = ad5446_spi_ids,
499 };
500
ad5446_spi_register_driver(void)501 static int __init ad5446_spi_register_driver(void)
502 {
503 return spi_register_driver(&ad5446_spi_driver);
504 }
505
ad5446_spi_unregister_driver(void)506 static void ad5446_spi_unregister_driver(void)
507 {
508 spi_unregister_driver(&ad5446_spi_driver);
509 }
510
511 #else
512
ad5446_spi_register_driver(void)513 static inline int ad5446_spi_register_driver(void) { return 0; }
ad5446_spi_unregister_driver(void)514 static inline void ad5446_spi_unregister_driver(void) { }
515
516 #endif
517
518 #if IS_ENABLED(CONFIG_I2C)
519
ad5622_write(struct ad5446_state * st,unsigned val)520 static int ad5622_write(struct ad5446_state *st, unsigned val)
521 {
522 struct i2c_client *client = to_i2c_client(st->dev);
523 __be16 data = cpu_to_be16(val);
524
525 return i2c_master_send(client, (char *)&data, sizeof(data));
526 }
527
528 /**
529 * ad5446_supported_i2c_device_ids:
530 * The AD5620/40/60 parts are available in different fixed internal reference
531 * voltage options. The actual part numbers may look differently
532 * (and a bit cryptic), however this style is used to make clear which
533 * parts are supported here.
534 */
535 enum ad5446_supported_i2c_device_ids {
536 ID_AD5602,
537 ID_AD5612,
538 ID_AD5622,
539 };
540
541 static const struct ad5446_chip_info ad5446_i2c_chip_info[] = {
542 [ID_AD5602] = {
543 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
544 .write = ad5622_write,
545 },
546 [ID_AD5612] = {
547 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
548 .write = ad5622_write,
549 },
550 [ID_AD5622] = {
551 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
552 .write = ad5622_write,
553 },
554 };
555
ad5446_i2c_probe(struct i2c_client * i2c,const struct i2c_device_id * id)556 static int ad5446_i2c_probe(struct i2c_client *i2c,
557 const struct i2c_device_id *id)
558 {
559 return ad5446_probe(&i2c->dev, id->name,
560 &ad5446_i2c_chip_info[id->driver_data]);
561 }
562
ad5446_i2c_remove(struct i2c_client * i2c)563 static int ad5446_i2c_remove(struct i2c_client *i2c)
564 {
565 return ad5446_remove(&i2c->dev);
566 }
567
568 static const struct i2c_device_id ad5446_i2c_ids[] = {
569 {"ad5301", ID_AD5602},
570 {"ad5311", ID_AD5612},
571 {"ad5321", ID_AD5622},
572 {"ad5602", ID_AD5602},
573 {"ad5612", ID_AD5612},
574 {"ad5622", ID_AD5622},
575 {}
576 };
577 MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids);
578
579 static struct i2c_driver ad5446_i2c_driver = {
580 .driver = {
581 .name = "ad5446",
582 },
583 .probe = ad5446_i2c_probe,
584 .remove = ad5446_i2c_remove,
585 .id_table = ad5446_i2c_ids,
586 };
587
ad5446_i2c_register_driver(void)588 static int __init ad5446_i2c_register_driver(void)
589 {
590 return i2c_add_driver(&ad5446_i2c_driver);
591 }
592
ad5446_i2c_unregister_driver(void)593 static void __exit ad5446_i2c_unregister_driver(void)
594 {
595 i2c_del_driver(&ad5446_i2c_driver);
596 }
597
598 #else
599
ad5446_i2c_register_driver(void)600 static inline int ad5446_i2c_register_driver(void) { return 0; }
ad5446_i2c_unregister_driver(void)601 static inline void ad5446_i2c_unregister_driver(void) { }
602
603 #endif
604
ad5446_init(void)605 static int __init ad5446_init(void)
606 {
607 int ret;
608
609 ret = ad5446_spi_register_driver();
610 if (ret)
611 return ret;
612
613 ret = ad5446_i2c_register_driver();
614 if (ret) {
615 ad5446_spi_unregister_driver();
616 return ret;
617 }
618
619 return 0;
620 }
621 module_init(ad5446_init);
622
ad5446_exit(void)623 static void __exit ad5446_exit(void)
624 {
625 ad5446_i2c_unregister_driver();
626 ad5446_spi_unregister_driver();
627 }
628 module_exit(ad5446_exit);
629
630 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
631 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
632 MODULE_LICENSE("GPL v2");
633