blob: b0d220c3a12686fa74f3990dd8a553c250ef2e00 [file] [log] [blame]
Cristian Popfd9373e2021-01-15 13:21:05 +02001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Analog Devices AD5766, AD5767
4 * Digital to Analog Converters driver
5 * Copyright 2019-2020 Analog Devices Inc.
6 */
7#include <linux/bitfield.h>
Mihail Chindris885b9792021-10-07 08:00:37 +00008#include <linux/bitops.h>
Cristian Popfd9373e2021-01-15 13:21:05 +02009#include <linux/delay.h>
10#include <linux/device.h>
11#include <linux/gpio/consumer.h>
12#include <linux/iio/iio.h>
Mihail Chindris885b9792021-10-07 08:00:37 +000013#include <linux/iio/triggered_buffer.h>
14#include <linux/iio/trigger_consumer.h>
Cristian Popfd9373e2021-01-15 13:21:05 +020015#include <linux/module.h>
16#include <linux/spi/spi.h>
17#include <asm/unaligned.h>
18
19#define AD5766_UPPER_WORD_SPI_MASK GENMASK(31, 16)
20#define AD5766_LOWER_WORD_SPI_MASK GENMASK(15, 0)
21#define AD5766_DITHER_SOURCE_MASK(ch) GENMASK(((2 * ch) + 1), (2 * ch))
22#define AD5766_DITHER_SOURCE(ch, source) BIT((ch * 2) + source)
23#define AD5766_DITHER_SCALE_MASK(x) AD5766_DITHER_SOURCE_MASK(x)
24#define AD5766_DITHER_SCALE(ch, scale) (scale << (ch * 2))
25#define AD5766_DITHER_ENABLE_MASK(ch) BIT(ch)
26#define AD5766_DITHER_ENABLE(ch, state) ((!state) << ch)
27#define AD5766_DITHER_INVERT_MASK(ch) BIT(ch)
28#define AD5766_DITHER_INVERT(ch, state) (state << ch)
29
30#define AD5766_CMD_NOP_MUX_OUT 0x00
31#define AD5766_CMD_SDO_CNTRL 0x01
32#define AD5766_CMD_WR_IN_REG(x) (0x10 | ((x) & GENMASK(3, 0)))
33#define AD5766_CMD_WR_DAC_REG(x) (0x20 | ((x) & GENMASK(3, 0)))
34#define AD5766_CMD_SW_LDAC 0x30
35#define AD5766_CMD_SPAN_REG 0x40
36#define AD5766_CMD_WR_PWR_DITHER 0x51
37#define AD5766_CMD_WR_DAC_REG_ALL 0x60
38#define AD5766_CMD_SW_FULL_RESET 0x70
39#define AD5766_CMD_READBACK_REG(x) (0x80 | ((x) & GENMASK(3, 0)))
40#define AD5766_CMD_DITHER_SIG_1 0x90
41#define AD5766_CMD_DITHER_SIG_2 0xA0
42#define AD5766_CMD_INV_DITHER 0xB0
43#define AD5766_CMD_DITHER_SCALE_1 0xC0
44#define AD5766_CMD_DITHER_SCALE_2 0xD0
45
46#define AD5766_FULL_RESET_CODE 0x1234
47
48enum ad5766_type {
49 ID_AD5766,
50 ID_AD5767,
51};
52
53enum ad5766_voltage_range {
54 AD5766_VOLTAGE_RANGE_M20V_0V,
55 AD5766_VOLTAGE_RANGE_M16V_to_0V,
56 AD5766_VOLTAGE_RANGE_M10V_to_0V,
57 AD5766_VOLTAGE_RANGE_M12V_to_14V,
58 AD5766_VOLTAGE_RANGE_M16V_to_10V,
59 AD5766_VOLTAGE_RANGE_M10V_to_6V,
60 AD5766_VOLTAGE_RANGE_M5V_to_5V,
61 AD5766_VOLTAGE_RANGE_M10V_to_10V,
62};
63
64/**
65 * struct ad5766_chip_info - chip specific information
66 * @num_channels: number of channels
67 * @channels: channel specification
68 */
69struct ad5766_chip_info {
70 unsigned int num_channels;
71 const struct iio_chan_spec *channels;
72};
73
74enum {
75 AD5766_DITHER_ENABLE,
76 AD5766_DITHER_INVERT,
77 AD5766_DITHER_SOURCE,
78};
79
80/*
81 * Dither signal can also be scaled.
82 * Available dither scale strings corresponding to "dither_scale" field in
83 * "struct ad5766_state".
84 */
85static const char * const ad5766_dither_scales[] = {
86 "1",
87 "0.75",
88 "0.5",
89 "0.25",
90};
91
92/**
93 * struct ad5766_state - driver instance specific data
94 * @spi: SPI device
Bhaskar Chowdhuryabfdfd12021-03-23 06:52:15 +053095 * @lock: Lock used to restrict concurrent access to SPI device
Cristian Popfd9373e2021-01-15 13:21:05 +020096 * @chip_info: Chip model specific constants
97 * @gpio_reset: Reset GPIO, used to reset the device
98 * @crt_range: Current selected output range
99 * @dither_enable: Power enable bit for each channel dither block (for
100 * example, D15 = DAC 15,D8 = DAC 8, and D0 = DAC 0)
101 * 0 - Normal operation, 1 - Power down
102 * @dither_invert: Inverts the dither signal applied to the selected DAC
103 * outputs
104 * @dither_source: Selects between 2 possible sources:
105 * 1: N0, 2: N1
106 * Two bits are used for each channel
107 * @dither_scale: Two bits are used for each of the 16 channels:
108 * 0: 1 SCALING, 1: 0.75 SCALING, 2: 0.5 SCALING,
109 * 3: 0.25 SCALING.
110 * @data: SPI transfer buffers
111 */
112struct ad5766_state {
113 struct spi_device *spi;
114 struct mutex lock;
115 const struct ad5766_chip_info *chip_info;
116 struct gpio_desc *gpio_reset;
117 enum ad5766_voltage_range crt_range;
118 u16 dither_enable;
119 u16 dither_invert;
120 u32 dither_source;
121 u32 dither_scale;
122 union {
123 u32 d32;
124 u16 w16[2];
125 u8 b8[4];
126 } data[3] ____cacheline_aligned;
127};
128
129struct ad5766_span_tbl {
130 int min;
131 int max;
132};
133
134static const struct ad5766_span_tbl ad5766_span_tbl[] = {
135 [AD5766_VOLTAGE_RANGE_M20V_0V] = {-20, 0},
136 [AD5766_VOLTAGE_RANGE_M16V_to_0V] = {-16, 0},
137 [AD5766_VOLTAGE_RANGE_M10V_to_0V] = {-10, 0},
138 [AD5766_VOLTAGE_RANGE_M12V_to_14V] = {-12, 14},
139 [AD5766_VOLTAGE_RANGE_M16V_to_10V] = {-16, 10},
140 [AD5766_VOLTAGE_RANGE_M10V_to_6V] = {-10, 6},
141 [AD5766_VOLTAGE_RANGE_M5V_to_5V] = {-5, 5},
142 [AD5766_VOLTAGE_RANGE_M10V_to_10V] = {-10, 10},
143};
144
145static int __ad5766_spi_read(struct ad5766_state *st, u8 dac, int *val)
146{
147 int ret;
148 struct spi_transfer xfers[] = {
149 {
150 .tx_buf = &st->data[0].d32,
151 .bits_per_word = 8,
152 .len = 3,
153 .cs_change = 1,
154 }, {
155 .tx_buf = &st->data[1].d32,
156 .rx_buf = &st->data[2].d32,
157 .bits_per_word = 8,
158 .len = 3,
159 },
160 };
161
162 st->data[0].d32 = AD5766_CMD_READBACK_REG(dac);
163 st->data[1].d32 = AD5766_CMD_NOP_MUX_OUT;
164
165 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
166 if (ret)
167 return ret;
168
169 *val = st->data[2].w16[1];
170
171 return ret;
172}
173
174static int __ad5766_spi_write(struct ad5766_state *st, u8 command, u16 data)
175{
176 st->data[0].b8[0] = command;
177 put_unaligned_be16(data, &st->data[0].b8[1]);
178
179 return spi_write(st->spi, &st->data[0].b8[0], 3);
180}
181
182static int ad5766_read(struct iio_dev *indio_dev, u8 dac, int *val)
183{
184 struct ad5766_state *st = iio_priv(indio_dev);
185 int ret;
186
187 mutex_lock(&st->lock);
188 ret = __ad5766_spi_read(st, dac, val);
189 mutex_unlock(&st->lock);
190
191 return ret;
192}
193
194static int ad5766_write(struct iio_dev *indio_dev, u8 dac, u16 data)
195{
196 struct ad5766_state *st = iio_priv(indio_dev);
197 int ret;
198
199 mutex_lock(&st->lock);
200 ret = __ad5766_spi_write(st, AD5766_CMD_WR_DAC_REG(dac), data);
201 mutex_unlock(&st->lock);
202
203 return ret;
204}
205
206static int ad5766_reset(struct ad5766_state *st)
207{
208 int ret;
209
210 if (st->gpio_reset) {
211 gpiod_set_value_cansleep(st->gpio_reset, 1);
212 ndelay(100); /* t_reset >= 100ns */
213 gpiod_set_value_cansleep(st->gpio_reset, 0);
214 } else {
215 ret = __ad5766_spi_write(st, AD5766_CMD_SW_FULL_RESET,
216 AD5766_FULL_RESET_CODE);
217 if (ret < 0)
218 return ret;
219 }
220
221 /*
222 * Minimum time between a reset and the subsequent successful write is
223 * typically 25 ns
224 */
225 ndelay(25);
226
227 return 0;
228}
229
230static int ad5766_read_raw(struct iio_dev *indio_dev,
231 struct iio_chan_spec const *chan,
232 int *val,
233 int *val2,
234 long m)
235{
236 struct ad5766_state *st = iio_priv(indio_dev);
237 int ret;
238
239 switch (m) {
240 case IIO_CHAN_INFO_RAW:
241 ret = ad5766_read(indio_dev, chan->address, val);
242 if (ret)
243 return ret;
244
245 return IIO_VAL_INT;
246 case IIO_CHAN_INFO_OFFSET:
247 *val = ad5766_span_tbl[st->crt_range].min;
248
249 return IIO_VAL_INT;
250 case IIO_CHAN_INFO_SCALE:
251 *val = ad5766_span_tbl[st->crt_range].max -
252 ad5766_span_tbl[st->crt_range].min;
253 *val2 = st->chip_info->channels[0].scan_type.realbits;
254
255 return IIO_VAL_FRACTIONAL_LOG2;
256 default:
257 return -EINVAL;
258 }
259}
260
261static int ad5766_write_raw(struct iio_dev *indio_dev,
262 struct iio_chan_spec const *chan,
263 int val,
264 int val2,
265 long info)
266{
267 switch (info) {
268 case IIO_CHAN_INFO_RAW:
269 {
270 const int max_val = GENMASK(chan->scan_type.realbits - 1, 0);
271
272 if (val > max_val || val < 0)
273 return -EINVAL;
274 val <<= chan->scan_type.shift;
275 return ad5766_write(indio_dev, chan->address, val);
276 }
277 default:
278 return -EINVAL;
279 }
280}
281
282static const struct iio_info ad5766_info = {
283 .read_raw = ad5766_read_raw,
284 .write_raw = ad5766_write_raw,
285};
286
287static int ad5766_get_dither_source(struct iio_dev *dev,
288 const struct iio_chan_spec *chan)
289{
290 struct ad5766_state *st = iio_priv(dev);
291 u32 source;
292
293 source = st->dither_source & AD5766_DITHER_SOURCE_MASK(chan->channel);
294 source = source >> (chan->channel * 2);
295 source -= 1;
296
297 return source;
298}
299
300static int ad5766_set_dither_source(struct iio_dev *dev,
301 const struct iio_chan_spec *chan,
302 unsigned int source)
303{
304 struct ad5766_state *st = iio_priv(dev);
305 uint16_t val;
306 int ret;
307
308 st->dither_source &= ~AD5766_DITHER_SOURCE_MASK(chan->channel);
309 st->dither_source |= AD5766_DITHER_SOURCE(chan->channel, source);
310
311 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_source);
312 ret = ad5766_write(dev, AD5766_CMD_DITHER_SIG_1, val);
313 if (ret)
314 return ret;
315
316 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_source);
317
318 return ad5766_write(dev, AD5766_CMD_DITHER_SIG_2, val);
319}
320
321static int ad5766_get_dither_scale(struct iio_dev *dev,
322 const struct iio_chan_spec *chan)
323{
324 struct ad5766_state *st = iio_priv(dev);
325 u32 scale;
326
327 scale = st->dither_scale & AD5766_DITHER_SCALE_MASK(chan->channel);
328
329 return (scale >> (chan->channel * 2));
330}
331
332static int ad5766_set_dither_scale(struct iio_dev *dev,
333 const struct iio_chan_spec *chan,
334 unsigned int scale)
335{
336 int ret;
337 struct ad5766_state *st = iio_priv(dev);
338 uint16_t val;
339
340 st->dither_scale &= ~AD5766_DITHER_SCALE_MASK(chan->channel);
341 st->dither_scale |= AD5766_DITHER_SCALE(chan->channel, scale);
342
343 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_scale);
344 ret = ad5766_write(dev, AD5766_CMD_DITHER_SCALE_1, val);
345 if (ret)
346 return ret;
347 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_scale);
348
349 return ad5766_write(dev, AD5766_CMD_DITHER_SCALE_2, val);
350}
351
352static const struct iio_enum ad5766_dither_scale_enum = {
353 .items = ad5766_dither_scales,
354 .num_items = ARRAY_SIZE(ad5766_dither_scales),
355 .set = ad5766_set_dither_scale,
356 .get = ad5766_get_dither_scale,
357};
358
359static ssize_t ad5766_read_ext(struct iio_dev *indio_dev,
360 uintptr_t private,
361 const struct iio_chan_spec *chan,
362 char *buf)
363{
364 struct ad5766_state *st = iio_priv(indio_dev);
365
366 switch (private) {
367 case AD5766_DITHER_ENABLE:
368 return sprintf(buf, "%u\n",
369 !(st->dither_enable & BIT(chan->channel)));
370 break;
371 case AD5766_DITHER_INVERT:
372 return sprintf(buf, "%u\n",
373 !!(st->dither_invert & BIT(chan->channel)));
374 break;
375 case AD5766_DITHER_SOURCE:
376 return sprintf(buf, "%d\n",
377 ad5766_get_dither_source(indio_dev, chan));
378 default:
379 return -EINVAL;
380 }
381}
382
383static ssize_t ad5766_write_ext(struct iio_dev *indio_dev,
384 uintptr_t private,
385 const struct iio_chan_spec *chan,
386 const char *buf, size_t len)
387{
388 struct ad5766_state *st = iio_priv(indio_dev);
389 bool readin;
390 int ret;
391
392 ret = kstrtobool(buf, &readin);
393 if (ret)
394 return ret;
395
396 switch (private) {
397 case AD5766_DITHER_ENABLE:
398 st->dither_enable &= ~AD5766_DITHER_ENABLE_MASK(chan->channel);
399 st->dither_enable |= AD5766_DITHER_ENABLE(chan->channel,
400 readin);
401 ret = ad5766_write(indio_dev, AD5766_CMD_WR_PWR_DITHER,
402 st->dither_enable);
403 break;
404 case AD5766_DITHER_INVERT:
405 st->dither_invert &= ~AD5766_DITHER_INVERT_MASK(chan->channel);
406 st->dither_invert |= AD5766_DITHER_INVERT(chan->channel,
407 readin);
408 ret = ad5766_write(indio_dev, AD5766_CMD_INV_DITHER,
409 st->dither_invert);
410 break;
411 case AD5766_DITHER_SOURCE:
412 ret = ad5766_set_dither_source(indio_dev, chan, readin);
413 break;
414 default:
415 return -EINVAL;
416 }
417
418 return ret ? ret : len;
419}
420
421#define _AD5766_CHAN_EXT_INFO(_name, _what, _shared) { \
422 .name = _name, \
423 .read = ad5766_read_ext, \
424 .write = ad5766_write_ext, \
425 .private = _what, \
426 .shared = _shared, \
427}
428
429#define IIO_ENUM_AVAILABLE_SHARED(_name, _shared, _e) \
430{ \
431 .name = (_name "_available"), \
432 .shared = _shared, \
433 .read = iio_enum_available_read, \
434 .private = (uintptr_t)(_e), \
435}
436
437static const struct iio_chan_spec_ext_info ad5766_ext_info[] = {
438
439 _AD5766_CHAN_EXT_INFO("dither_enable", AD5766_DITHER_ENABLE,
440 IIO_SEPARATE),
441 _AD5766_CHAN_EXT_INFO("dither_invert", AD5766_DITHER_INVERT,
442 IIO_SEPARATE),
443 _AD5766_CHAN_EXT_INFO("dither_source", AD5766_DITHER_SOURCE,
444 IIO_SEPARATE),
445 IIO_ENUM("dither_scale", IIO_SEPARATE, &ad5766_dither_scale_enum),
446 IIO_ENUM_AVAILABLE_SHARED("dither_scale",
447 IIO_SEPARATE,
448 &ad5766_dither_scale_enum),
449 {}
450};
451
452#define AD576x_CHANNEL(_chan, _bits) { \
453 .type = IIO_VOLTAGE, \
454 .indexed = 1, \
455 .output = 1, \
456 .channel = (_chan), \
457 .address = (_chan), \
458 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
459 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | \
460 BIT(IIO_CHAN_INFO_SCALE), \
Mihail Chindris885b9792021-10-07 08:00:37 +0000461 .scan_index = (_chan), \
Cristian Popfd9373e2021-01-15 13:21:05 +0200462 .scan_type = { \
463 .sign = 'u', \
464 .realbits = (_bits), \
465 .storagebits = 16, \
466 .shift = 16 - (_bits), \
467 }, \
468 .ext_info = ad5766_ext_info, \
469}
470
471#define DECLARE_AD576x_CHANNELS(_name, _bits) \
472const struct iio_chan_spec _name[] = { \
473 AD576x_CHANNEL(0, (_bits)), \
474 AD576x_CHANNEL(1, (_bits)), \
475 AD576x_CHANNEL(2, (_bits)), \
476 AD576x_CHANNEL(3, (_bits)), \
477 AD576x_CHANNEL(4, (_bits)), \
478 AD576x_CHANNEL(5, (_bits)), \
479 AD576x_CHANNEL(6, (_bits)), \
480 AD576x_CHANNEL(7, (_bits)), \
481 AD576x_CHANNEL(8, (_bits)), \
482 AD576x_CHANNEL(9, (_bits)), \
483 AD576x_CHANNEL(10, (_bits)), \
484 AD576x_CHANNEL(11, (_bits)), \
485 AD576x_CHANNEL(12, (_bits)), \
486 AD576x_CHANNEL(13, (_bits)), \
487 AD576x_CHANNEL(14, (_bits)), \
488 AD576x_CHANNEL(15, (_bits)), \
489}
490
491static DECLARE_AD576x_CHANNELS(ad5766_channels, 16);
492static DECLARE_AD576x_CHANNELS(ad5767_channels, 12);
493
494static const struct ad5766_chip_info ad5766_chip_infos[] = {
495 [ID_AD5766] = {
496 .num_channels = ARRAY_SIZE(ad5766_channels),
497 .channels = ad5766_channels,
498 },
499 [ID_AD5767] = {
500 .num_channels = ARRAY_SIZE(ad5767_channels),
501 .channels = ad5767_channels,
502 },
503};
504
505static int ad5766_get_output_range(struct ad5766_state *st)
506{
507 int i, ret, min, max, tmp[2];
508
509 ret = device_property_read_u32_array(&st->spi->dev,
Mihail Chindrisd9de0fb2021-10-07 08:00:34 +0000510 "output-range-microvolts",
Cristian Popfd9373e2021-01-15 13:21:05 +0200511 tmp, 2);
512 if (ret)
513 return ret;
514
Mihail Chindrisd9de0fb2021-10-07 08:00:34 +0000515 min = tmp[0] / 1000000;
516 max = tmp[1] / 1000000;
Cristian Popfd9373e2021-01-15 13:21:05 +0200517 for (i = 0; i < ARRAY_SIZE(ad5766_span_tbl); i++) {
518 if (ad5766_span_tbl[i].min != min ||
519 ad5766_span_tbl[i].max != max)
520 continue;
521
522 st->crt_range = i;
523
524 return 0;
525 }
526
527 return -EINVAL;
528}
529
530static int ad5766_default_setup(struct ad5766_state *st)
531{
532 uint16_t val;
533 int ret, i;
534
535 /* Always issue a reset before writing to the span register. */
536 ret = ad5766_reset(st);
537 if (ret)
538 return ret;
539
540 ret = ad5766_get_output_range(st);
541 if (ret)
542 return ret;
543
544 /* Dither power down */
545 st->dither_enable = GENMASK(15, 0);
546 ret = __ad5766_spi_write(st, AD5766_CMD_WR_PWR_DITHER,
547 st->dither_enable);
548 if (ret)
549 return ret;
550
551 st->dither_source = 0;
552 for (i = 0; i < ARRAY_SIZE(ad5766_channels); i++)
553 st->dither_source |= AD5766_DITHER_SOURCE(i, 0);
554 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_source);
555 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SIG_1, val);
556 if (ret)
557 return ret;
558
559 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_source);
560 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SIG_2, val);
561 if (ret)
562 return ret;
563
564 st->dither_scale = 0;
565 val = FIELD_GET(AD5766_LOWER_WORD_SPI_MASK, st->dither_scale);
566 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SCALE_1, val);
567 if (ret)
568 return ret;
569
570 val = FIELD_GET(AD5766_UPPER_WORD_SPI_MASK, st->dither_scale);
571 ret = __ad5766_spi_write(st, AD5766_CMD_DITHER_SCALE_2, val);
572 if (ret)
573 return ret;
574
575 st->dither_invert = 0;
576 ret = __ad5766_spi_write(st, AD5766_CMD_INV_DITHER, st->dither_invert);
577 if (ret)
578 return ret;
579
580 return __ad5766_spi_write(st, AD5766_CMD_SPAN_REG, st->crt_range);
581}
582
Mihail Chindris885b9792021-10-07 08:00:37 +0000583static irqreturn_t ad5766_trigger_handler(int irq, void *p)
584{
585 struct iio_poll_func *pf = p;
586 struct iio_dev *indio_dev = pf->indio_dev;
587 struct iio_buffer *buffer = indio_dev->buffer;
588 struct ad5766_state *st = iio_priv(indio_dev);
589 int ret, ch, i;
590 u16 data[ARRAY_SIZE(ad5766_channels)];
591
592 ret = iio_pop_from_buffer(buffer, data);
593 if (ret)
594 goto done;
595
596 i = 0;
597 mutex_lock(&st->lock);
598 for_each_set_bit(ch, indio_dev->active_scan_mask,
599 st->chip_info->num_channels - 1)
600 __ad5766_spi_write(st, AD5766_CMD_WR_IN_REG(ch), data[i++]);
601
602 __ad5766_spi_write(st, AD5766_CMD_SW_LDAC,
603 *indio_dev->active_scan_mask);
604 mutex_unlock(&st->lock);
605
606done:
607 iio_trigger_notify_done(indio_dev->trig);
608
609 return IRQ_HANDLED;
610}
611
Cristian Popfd9373e2021-01-15 13:21:05 +0200612static int ad5766_probe(struct spi_device *spi)
613{
614 enum ad5766_type type;
615 struct iio_dev *indio_dev;
616 struct ad5766_state *st;
617 int ret;
618
619 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
620 if (!indio_dev)
621 return -ENOMEM;
622
623 st = iio_priv(indio_dev);
624 mutex_init(&st->lock);
625
626 st->spi = spi;
627 type = spi_get_device_id(spi)->driver_data;
628 st->chip_info = &ad5766_chip_infos[type];
629
630 indio_dev->channels = st->chip_info->channels;
631 indio_dev->num_channels = st->chip_info->num_channels;
632 indio_dev->info = &ad5766_info;
Cristian Popfd9373e2021-01-15 13:21:05 +0200633 indio_dev->name = spi_get_device_id(spi)->name;
634 indio_dev->modes = INDIO_DIRECT_MODE;
635
636 st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset",
637 GPIOD_OUT_LOW);
638 if (IS_ERR(st->gpio_reset))
639 return PTR_ERR(st->gpio_reset);
640
641 ret = ad5766_default_setup(st);
642 if (ret)
643 return ret;
644
Mihail Chindris885b9792021-10-07 08:00:37 +0000645 /* Configure trigger buffer */
646 ret = devm_iio_triggered_buffer_setup_ext(&spi->dev, indio_dev, NULL,
647 ad5766_trigger_handler,
648 IIO_BUFFER_DIRECTION_OUT,
649 NULL,
650 NULL);
651 if (ret)
652 return ret;
653
Cristian Popfd9373e2021-01-15 13:21:05 +0200654 return devm_iio_device_register(&spi->dev, indio_dev);
655}
656
657static const struct of_device_id ad5766_dt_match[] = {
658 { .compatible = "adi,ad5766" },
659 { .compatible = "adi,ad5767" },
660 {}
661};
662MODULE_DEVICE_TABLE(of, ad5766_dt_match);
663
664static const struct spi_device_id ad5766_spi_ids[] = {
665 { "ad5766", ID_AD5766 },
666 { "ad5767", ID_AD5767 },
667 {}
668};
669MODULE_DEVICE_TABLE(spi, ad5766_spi_ids);
670
671static struct spi_driver ad5766_driver = {
672 .driver = {
673 .name = "ad5766",
674 .of_match_table = ad5766_dt_match,
675 },
676 .probe = ad5766_probe,
677 .id_table = ad5766_spi_ids,
678};
679module_spi_driver(ad5766_driver);
680
681MODULE_AUTHOR("Denis-Gabriel Gheorghescu <denis.gheorghescu@analog.com>");
682MODULE_DESCRIPTION("Analog Devices AD5766/AD5767 DACs");
683MODULE_LICENSE("GPL v2");