blob: 63df2e2a8caf4a39ca861b0993aa2098579da688 [file] [log] [blame]
Stefan Popab3af3412018-11-13 13:21:32 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * AD7124 SPI ADC driver
4 *
5 * Copyright 2018 Analog Devices Inc.
6 */
7#include <linux/bitfield.h>
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/device.h>
11#include <linux/err.h>
Alexandru Tachicida4d3d62020-01-13 12:26:52 +020012#include <linux/interrupt.h>
Stefan Popab3af3412018-11-13 13:21:32 +020013#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/regulator/consumer.h>
16#include <linux/spi/spi.h>
17
18#include <linux/iio/iio.h>
19#include <linux/iio/adc/ad_sigma_delta.h>
20#include <linux/iio/sysfs.h>
21
22/* AD7124 registers */
23#define AD7124_COMMS 0x00
24#define AD7124_STATUS 0x00
25#define AD7124_ADC_CONTROL 0x01
26#define AD7124_DATA 0x02
27#define AD7124_IO_CONTROL_1 0x03
28#define AD7124_IO_CONTROL_2 0x04
29#define AD7124_ID 0x05
30#define AD7124_ERROR 0x06
31#define AD7124_ERROR_EN 0x07
32#define AD7124_MCLK_COUNT 0x08
33#define AD7124_CHANNEL(x) (0x09 + (x))
34#define AD7124_CONFIG(x) (0x19 + (x))
35#define AD7124_FILTER(x) (0x21 + (x))
36#define AD7124_OFFSET(x) (0x29 + (x))
37#define AD7124_GAIN(x) (0x31 + (x))
38
39/* AD7124_STATUS */
40#define AD7124_STATUS_POR_FLAG_MSK BIT(4)
41
42/* AD7124_ADC_CONTROL */
Mircea Caprioru11d7c8d2019-11-18 10:38:57 +020043#define AD7124_ADC_CTRL_REF_EN_MSK BIT(8)
44#define AD7124_ADC_CTRL_REF_EN(x) FIELD_PREP(AD7124_ADC_CTRL_REF_EN_MSK, x)
Stefan Popab3af3412018-11-13 13:21:32 +020045#define AD7124_ADC_CTRL_PWR_MSK GENMASK(7, 6)
46#define AD7124_ADC_CTRL_PWR(x) FIELD_PREP(AD7124_ADC_CTRL_PWR_MSK, x)
47#define AD7124_ADC_CTRL_MODE_MSK GENMASK(5, 2)
48#define AD7124_ADC_CTRL_MODE(x) FIELD_PREP(AD7124_ADC_CTRL_MODE_MSK, x)
49
50/* AD7124_CHANNEL_X */
51#define AD7124_CHANNEL_EN_MSK BIT(15)
52#define AD7124_CHANNEL_EN(x) FIELD_PREP(AD7124_CHANNEL_EN_MSK, x)
53#define AD7124_CHANNEL_SETUP_MSK GENMASK(14, 12)
54#define AD7124_CHANNEL_SETUP(x) FIELD_PREP(AD7124_CHANNEL_SETUP_MSK, x)
55#define AD7124_CHANNEL_AINP_MSK GENMASK(9, 5)
56#define AD7124_CHANNEL_AINP(x) FIELD_PREP(AD7124_CHANNEL_AINP_MSK, x)
57#define AD7124_CHANNEL_AINM_MSK GENMASK(4, 0)
58#define AD7124_CHANNEL_AINM(x) FIELD_PREP(AD7124_CHANNEL_AINM_MSK, x)
59
60/* AD7124_CONFIG_X */
61#define AD7124_CONFIG_BIPOLAR_MSK BIT(11)
62#define AD7124_CONFIG_BIPOLAR(x) FIELD_PREP(AD7124_CONFIG_BIPOLAR_MSK, x)
63#define AD7124_CONFIG_REF_SEL_MSK GENMASK(4, 3)
64#define AD7124_CONFIG_REF_SEL(x) FIELD_PREP(AD7124_CONFIG_REF_SEL_MSK, x)
65#define AD7124_CONFIG_PGA_MSK GENMASK(2, 0)
66#define AD7124_CONFIG_PGA(x) FIELD_PREP(AD7124_CONFIG_PGA_MSK, x)
Mircea Caprioru0eaecea2019-06-25 11:11:25 +030067#define AD7124_CONFIG_IN_BUFF_MSK GENMASK(7, 6)
68#define AD7124_CONFIG_IN_BUFF(x) FIELD_PREP(AD7124_CONFIG_IN_BUFF_MSK, x)
Stefan Popab3af3412018-11-13 13:21:32 +020069
70/* AD7124_FILTER_X */
71#define AD7124_FILTER_FS_MSK GENMASK(10, 0)
72#define AD7124_FILTER_FS(x) FIELD_PREP(AD7124_FILTER_FS_MSK, x)
73
74enum ad7124_ids {
75 ID_AD7124_4,
76 ID_AD7124_8,
77};
78
79enum ad7124_ref_sel {
80 AD7124_REFIN1,
81 AD7124_REFIN2,
82 AD7124_INT_REF,
83 AD7124_AVDD_REF,
84};
85
86enum ad7124_power_mode {
87 AD7124_LOW_POWER,
88 AD7124_MID_POWER,
89 AD7124_FULL_POWER,
90};
91
92static const unsigned int ad7124_gain[8] = {
93 1, 2, 4, 8, 16, 32, 64, 128
94};
95
96static const int ad7124_master_clk_freq_hz[3] = {
97 [AD7124_LOW_POWER] = 76800,
98 [AD7124_MID_POWER] = 153600,
99 [AD7124_FULL_POWER] = 614400,
100};
101
102static const char * const ad7124_ref_names[] = {
103 [AD7124_REFIN1] = "refin1",
104 [AD7124_REFIN2] = "refin2",
105 [AD7124_INT_REF] = "int",
106 [AD7124_AVDD_REF] = "avdd",
107};
108
109struct ad7124_chip_info {
110 unsigned int num_inputs;
111};
112
113struct ad7124_channel_config {
114 enum ad7124_ref_sel refsel;
115 bool bipolar;
Mircea Caprioru0eaecea2019-06-25 11:11:25 +0300116 bool buf_positive;
117 bool buf_negative;
Stefan Popab3af3412018-11-13 13:21:32 +0200118 unsigned int ain;
119 unsigned int vref_mv;
120 unsigned int pga_bits;
121 unsigned int odr;
122};
123
124struct ad7124_state {
125 const struct ad7124_chip_info *chip_info;
126 struct ad_sigma_delta sd;
Mircea Caprioru1478a382019-06-25 11:11:26 +0300127 struct ad7124_channel_config *channel_config;
Stefan Popab3af3412018-11-13 13:21:32 +0200128 struct regulator *vref[4];
129 struct clk *mclk;
130 unsigned int adc_control;
131 unsigned int num_channels;
132};
133
134static const struct iio_chan_spec ad7124_channel_template = {
135 .type = IIO_VOLTAGE,
136 .indexed = 1,
137 .differential = 1,
138 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
139 BIT(IIO_CHAN_INFO_SCALE) |
140 BIT(IIO_CHAN_INFO_OFFSET) |
141 BIT(IIO_CHAN_INFO_SAMP_FREQ),
142 .scan_type = {
143 .sign = 'u',
144 .realbits = 24,
145 .storagebits = 32,
146 .shift = 8,
147 .endianness = IIO_BE,
148 },
149};
150
151static struct ad7124_chip_info ad7124_chip_info_tbl[] = {
152 [ID_AD7124_4] = {
153 .num_inputs = 8,
154 },
155 [ID_AD7124_8] = {
156 .num_inputs = 16,
157 },
158};
159
160static int ad7124_find_closest_match(const int *array,
161 unsigned int size, int val)
162{
163 int i, idx;
164 unsigned int diff_new, diff_old;
165
166 diff_old = U32_MAX;
167 idx = 0;
168
169 for (i = 0; i < size; i++) {
170 diff_new = abs(val - array[i]);
171 if (diff_new < diff_old) {
172 diff_old = diff_new;
173 idx = i;
174 }
175 }
176
177 return idx;
178}
179
180static int ad7124_spi_write_mask(struct ad7124_state *st,
181 unsigned int addr,
182 unsigned long mask,
183 unsigned int val,
184 unsigned int bytes)
185{
186 unsigned int readval;
187 int ret;
188
189 ret = ad_sd_read_reg(&st->sd, addr, bytes, &readval);
190 if (ret < 0)
191 return ret;
192
193 readval &= ~mask;
194 readval |= val;
195
196 return ad_sd_write_reg(&st->sd, addr, bytes, readval);
197}
198
199static int ad7124_set_mode(struct ad_sigma_delta *sd,
200 enum ad_sigma_delta_mode mode)
201{
202 struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
203
204 st->adc_control &= ~AD7124_ADC_CTRL_MODE_MSK;
205 st->adc_control |= AD7124_ADC_CTRL_MODE(mode);
206
207 return ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control);
208}
209
210static int ad7124_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
211{
212 struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
213 unsigned int val;
214
215 val = st->channel_config[channel].ain | AD7124_CHANNEL_EN(1) |
216 AD7124_CHANNEL_SETUP(channel);
217
218 return ad_sd_write_reg(&st->sd, AD7124_CHANNEL(channel), 2, val);
219}
220
221static const struct ad_sigma_delta_info ad7124_sigma_delta_info = {
222 .set_channel = ad7124_set_channel,
223 .set_mode = ad7124_set_mode,
224 .has_registers = true,
225 .addr_shift = 0,
226 .read_mask = BIT(6),
227 .data_reg = AD7124_DATA,
Alexandru Tachicida4d3d62020-01-13 12:26:52 +0200228 .irq_flags = IRQF_TRIGGER_LOW,
Stefan Popab3af3412018-11-13 13:21:32 +0200229};
230
231static int ad7124_set_channel_odr(struct ad7124_state *st,
232 unsigned int channel,
233 unsigned int odr)
234{
235 unsigned int fclk, odr_sel_bits;
236 int ret;
237
238 fclk = clk_get_rate(st->mclk);
239 /*
240 * FS[10:0] = fCLK / (fADC x 32) where:
241 * fADC is the output data rate
242 * fCLK is the master clock frequency
243 * FS[10:0] are the bits in the filter register
244 * FS[10:0] can have a value from 1 to 2047
245 */
246 odr_sel_bits = DIV_ROUND_CLOSEST(fclk, odr * 32);
247 if (odr_sel_bits < 1)
248 odr_sel_bits = 1;
249 else if (odr_sel_bits > 2047)
250 odr_sel_bits = 2047;
251
252 ret = ad7124_spi_write_mask(st, AD7124_FILTER(channel),
253 AD7124_FILTER_FS_MSK,
254 AD7124_FILTER_FS(odr_sel_bits), 3);
255 if (ret < 0)
256 return ret;
257 /* fADC = fCLK / (FS[10:0] x 32) */
258 st->channel_config[channel].odr =
259 DIV_ROUND_CLOSEST(fclk, odr_sel_bits * 32);
260
261 return 0;
262}
263
264static int ad7124_set_channel_gain(struct ad7124_state *st,
265 unsigned int channel,
266 unsigned int gain)
267{
268 unsigned int res;
269 int ret;
270
271 res = ad7124_find_closest_match(ad7124_gain,
272 ARRAY_SIZE(ad7124_gain), gain);
273 ret = ad7124_spi_write_mask(st, AD7124_CONFIG(channel),
274 AD7124_CONFIG_PGA_MSK,
275 AD7124_CONFIG_PGA(res), 2);
276 if (ret < 0)
277 return ret;
278
279 st->channel_config[channel].pga_bits = res;
280
281 return 0;
282}
283
284static int ad7124_read_raw(struct iio_dev *indio_dev,
285 struct iio_chan_spec const *chan,
286 int *val, int *val2, long info)
287{
288 struct ad7124_state *st = iio_priv(indio_dev);
289 int idx, ret;
290
291 switch (info) {
292 case IIO_CHAN_INFO_RAW:
293 ret = ad_sigma_delta_single_conversion(indio_dev, chan, val);
294 if (ret < 0)
295 return ret;
296
297 /* After the conversion is performed, disable the channel */
298 ret = ad_sd_write_reg(&st->sd,
299 AD7124_CHANNEL(chan->address), 2,
300 st->channel_config[chan->address].ain |
301 AD7124_CHANNEL_EN(0));
302 if (ret < 0)
303 return ret;
304
305 return IIO_VAL_INT;
306 case IIO_CHAN_INFO_SCALE:
307 idx = st->channel_config[chan->address].pga_bits;
308 *val = st->channel_config[chan->address].vref_mv;
309 if (st->channel_config[chan->address].bipolar)
310 *val2 = chan->scan_type.realbits - 1 + idx;
311 else
312 *val2 = chan->scan_type.realbits + idx;
313
314 return IIO_VAL_FRACTIONAL_LOG2;
315 case IIO_CHAN_INFO_OFFSET:
316 if (st->channel_config[chan->address].bipolar)
317 *val = -(1 << (chan->scan_type.realbits - 1));
318 else
319 *val = 0;
320
321 return IIO_VAL_INT;
322 case IIO_CHAN_INFO_SAMP_FREQ:
323 *val = st->channel_config[chan->address].odr;
324
325 return IIO_VAL_INT;
326 default:
327 return -EINVAL;
328 }
329}
330
331static int ad7124_write_raw(struct iio_dev *indio_dev,
332 struct iio_chan_spec const *chan,
333 int val, int val2, long info)
334{
335 struct ad7124_state *st = iio_priv(indio_dev);
336 unsigned int res, gain, full_scale, vref;
337
338 switch (info) {
339 case IIO_CHAN_INFO_SAMP_FREQ:
340 if (val2 != 0)
341 return -EINVAL;
342
343 return ad7124_set_channel_odr(st, chan->address, val);
344 case IIO_CHAN_INFO_SCALE:
345 if (val != 0)
346 return -EINVAL;
347
348 if (st->channel_config[chan->address].bipolar)
349 full_scale = 1 << (chan->scan_type.realbits - 1);
350 else
351 full_scale = 1 << chan->scan_type.realbits;
352
353 vref = st->channel_config[chan->address].vref_mv * 1000000LL;
354 res = DIV_ROUND_CLOSEST(vref, full_scale);
355 gain = DIV_ROUND_CLOSEST(res, val2);
356
357 return ad7124_set_channel_gain(st, chan->address, gain);
358 default:
359 return -EINVAL;
360 }
361}
362
363static IIO_CONST_ATTR(in_voltage_scale_available,
364 "0.000001164 0.000002328 0.000004656 0.000009313 0.000018626 0.000037252 0.000074505 0.000149011 0.000298023");
365
366static struct attribute *ad7124_attributes[] = {
367 &iio_const_attr_in_voltage_scale_available.dev_attr.attr,
368 NULL,
369};
370
371static const struct attribute_group ad7124_attrs_group = {
372 .attrs = ad7124_attributes,
373};
374
375static const struct iio_info ad7124_info = {
376 .read_raw = ad7124_read_raw,
377 .write_raw = ad7124_write_raw,
378 .validate_trigger = ad_sd_validate_trigger,
379 .attrs = &ad7124_attrs_group,
380};
381
382static int ad7124_soft_reset(struct ad7124_state *st)
383{
384 unsigned int readval, timeout;
385 int ret;
386
387 ret = ad_sd_reset(&st->sd, 64);
388 if (ret < 0)
389 return ret;
390
391 timeout = 100;
392 do {
393 ret = ad_sd_read_reg(&st->sd, AD7124_STATUS, 1, &readval);
394 if (ret < 0)
395 return ret;
396
397 if (!(readval & AD7124_STATUS_POR_FLAG_MSK))
398 return 0;
399
400 /* The AD7124 requires typically 2ms to power up and settle */
401 usleep_range(100, 2000);
402 } while (--timeout);
403
404 dev_err(&st->sd.spi->dev, "Soft reset failed\n");
405
406 return -EIO;
407}
408
409static int ad7124_init_channel_vref(struct ad7124_state *st,
410 unsigned int channel_number)
411{
412 unsigned int refsel = st->channel_config[channel_number].refsel;
413
414 switch (refsel) {
415 case AD7124_REFIN1:
416 case AD7124_REFIN2:
417 case AD7124_AVDD_REF:
418 if (IS_ERR(st->vref[refsel])) {
419 dev_err(&st->sd.spi->dev,
420 "Error, trying to use external voltage reference without a %s regulator.\n",
421 ad7124_ref_names[refsel]);
Colin Ian Kingb34d6c82019-03-14 23:26:03 +0000422 return PTR_ERR(st->vref[refsel]);
Stefan Popab3af3412018-11-13 13:21:32 +0200423 }
424 st->channel_config[channel_number].vref_mv =
425 regulator_get_voltage(st->vref[refsel]);
426 /* Conversion from uV to mV */
427 st->channel_config[channel_number].vref_mv /= 1000;
428 break;
429 case AD7124_INT_REF:
430 st->channel_config[channel_number].vref_mv = 2500;
Mircea Caprioru11d7c8d2019-11-18 10:38:57 +0200431 st->adc_control &= ~AD7124_ADC_CTRL_REF_EN_MSK;
432 st->adc_control |= AD7124_ADC_CTRL_REF_EN(1);
433 return ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL,
434 2, st->adc_control);
Stefan Popab3af3412018-11-13 13:21:32 +0200435 default:
436 dev_err(&st->sd.spi->dev, "Invalid reference %d\n", refsel);
437 return -EINVAL;
438 }
439
440 return 0;
441}
442
443static int ad7124_of_parse_channel_config(struct iio_dev *indio_dev,
444 struct device_node *np)
445{
446 struct ad7124_state *st = iio_priv(indio_dev);
447 struct device_node *child;
448 struct iio_chan_spec *chan;
Mircea Caprioru1478a382019-06-25 11:11:26 +0300449 struct ad7124_channel_config *chan_config;
Stefan Popab3af3412018-11-13 13:21:32 +0200450 unsigned int ain[2], channel = 0, tmp;
451 int ret;
452
453 st->num_channels = of_get_available_child_count(np);
454 if (!st->num_channels) {
455 dev_err(indio_dev->dev.parent, "no channel children\n");
456 return -ENODEV;
457 }
458
459 chan = devm_kcalloc(indio_dev->dev.parent, st->num_channels,
460 sizeof(*chan), GFP_KERNEL);
461 if (!chan)
462 return -ENOMEM;
463
Mircea Caprioru1478a382019-06-25 11:11:26 +0300464 chan_config = devm_kcalloc(indio_dev->dev.parent, st->num_channels,
465 sizeof(*chan_config), GFP_KERNEL);
466 if (!chan_config)
467 return -ENOMEM;
468
Stefan Popab3af3412018-11-13 13:21:32 +0200469 indio_dev->channels = chan;
470 indio_dev->num_channels = st->num_channels;
Mircea Caprioru1478a382019-06-25 11:11:26 +0300471 st->channel_config = chan_config;
Stefan Popab3af3412018-11-13 13:21:32 +0200472
473 for_each_available_child_of_node(np, child) {
474 ret = of_property_read_u32(child, "reg", &channel);
475 if (ret)
476 goto err;
477
478 ret = of_property_read_u32_array(child, "diff-channels",
479 ain, 2);
480 if (ret)
481 goto err;
482
Stefan Popab3af3412018-11-13 13:21:32 +0200483 st->channel_config[channel].ain = AD7124_CHANNEL_AINP(ain[0]) |
484 AD7124_CHANNEL_AINM(ain[1]);
485 st->channel_config[channel].bipolar =
486 of_property_read_bool(child, "bipolar");
487
488 ret = of_property_read_u32(child, "adi,reference-select", &tmp);
489 if (ret)
490 st->channel_config[channel].refsel = AD7124_INT_REF;
491 else
492 st->channel_config[channel].refsel = tmp;
493
Mircea Caprioru0eaecea2019-06-25 11:11:25 +0300494 st->channel_config[channel].buf_positive =
495 of_property_read_bool(child, "adi,buffered-positive");
496 st->channel_config[channel].buf_negative =
497 of_property_read_bool(child, "adi,buffered-negative");
498
Stefan Popab3af3412018-11-13 13:21:32 +0200499 *chan = ad7124_channel_template;
500 chan->address = channel;
501 chan->scan_index = channel;
502 chan->channel = ain[0];
503 chan->channel2 = ain[1];
504
505 chan++;
506 }
507
508 return 0;
509err:
510 of_node_put(child);
511
512 return ret;
513}
514
515static int ad7124_setup(struct ad7124_state *st)
516{
517 unsigned int val, fclk, power_mode;
Mircea Caprioru0eaecea2019-06-25 11:11:25 +0300518 int i, ret, tmp;
Stefan Popab3af3412018-11-13 13:21:32 +0200519
520 fclk = clk_get_rate(st->mclk);
521 if (!fclk)
522 return -EINVAL;
523
524 /* The power mode changes the master clock frequency */
525 power_mode = ad7124_find_closest_match(ad7124_master_clk_freq_hz,
526 ARRAY_SIZE(ad7124_master_clk_freq_hz),
527 fclk);
528 if (fclk != ad7124_master_clk_freq_hz[power_mode]) {
529 ret = clk_set_rate(st->mclk, fclk);
530 if (ret)
531 return ret;
532 }
533
534 /* Set the power mode */
535 st->adc_control &= ~AD7124_ADC_CTRL_PWR_MSK;
536 st->adc_control |= AD7124_ADC_CTRL_PWR(power_mode);
537 ret = ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control);
538 if (ret < 0)
539 return ret;
540
541 for (i = 0; i < st->num_channels; i++) {
542 val = st->channel_config[i].ain | AD7124_CHANNEL_SETUP(i);
543 ret = ad_sd_write_reg(&st->sd, AD7124_CHANNEL(i), 2, val);
544 if (ret < 0)
545 return ret;
546
547 ret = ad7124_init_channel_vref(st, i);
548 if (ret < 0)
549 return ret;
550
Mircea Caprioru0eaecea2019-06-25 11:11:25 +0300551 tmp = (st->channel_config[i].buf_positive << 1) +
552 st->channel_config[i].buf_negative;
553
Stefan Popab3af3412018-11-13 13:21:32 +0200554 val = AD7124_CONFIG_BIPOLAR(st->channel_config[i].bipolar) |
Mircea Caprioru0eaecea2019-06-25 11:11:25 +0300555 AD7124_CONFIG_REF_SEL(st->channel_config[i].refsel) |
556 AD7124_CONFIG_IN_BUFF(tmp);
Stefan Popab3af3412018-11-13 13:21:32 +0200557 ret = ad_sd_write_reg(&st->sd, AD7124_CONFIG(i), 2, val);
558 if (ret < 0)
559 return ret;
560 /*
561 * 9.38 SPS is the minimum output data rate supported
562 * regardless of the selected power mode. Round it up to 10 and
563 * set all the enabled channels to this default value.
564 */
565 ret = ad7124_set_channel_odr(st, i, 10);
566 }
567
568 return ret;
569}
570
571static int ad7124_probe(struct spi_device *spi)
572{
573 const struct spi_device_id *id;
574 struct ad7124_state *st;
575 struct iio_dev *indio_dev;
576 int i, ret;
577
578 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
579 if (!indio_dev)
580 return -ENOMEM;
581
582 st = iio_priv(indio_dev);
583
584 id = spi_get_device_id(spi);
585 st->chip_info = &ad7124_chip_info_tbl[id->driver_data];
586
587 ad_sd_init(&st->sd, indio_dev, spi, &ad7124_sigma_delta_info);
588
589 spi_set_drvdata(spi, indio_dev);
590
591 indio_dev->dev.parent = &spi->dev;
592 indio_dev->name = spi_get_device_id(spi)->name;
593 indio_dev->modes = INDIO_DIRECT_MODE;
594 indio_dev->info = &ad7124_info;
595
596 ret = ad7124_of_parse_channel_config(indio_dev, spi->dev.of_node);
597 if (ret < 0)
598 return ret;
599
600 for (i = 0; i < ARRAY_SIZE(st->vref); i++) {
601 if (i == AD7124_INT_REF)
602 continue;
603
604 st->vref[i] = devm_regulator_get_optional(&spi->dev,
605 ad7124_ref_names[i]);
606 if (PTR_ERR(st->vref[i]) == -ENODEV)
607 continue;
608 else if (IS_ERR(st->vref[i]))
609 return PTR_ERR(st->vref[i]);
610
611 ret = regulator_enable(st->vref[i]);
612 if (ret)
613 return ret;
614 }
615
616 st->mclk = devm_clk_get(&spi->dev, "mclk");
617 if (IS_ERR(st->mclk)) {
618 ret = PTR_ERR(st->mclk);
619 goto error_regulator_disable;
620 }
621
622 ret = clk_prepare_enable(st->mclk);
623 if (ret < 0)
624 goto error_regulator_disable;
625
626 ret = ad7124_soft_reset(st);
627 if (ret < 0)
628 goto error_clk_disable_unprepare;
629
630 ret = ad7124_setup(st);
631 if (ret < 0)
632 goto error_clk_disable_unprepare;
633
634 ret = ad_sd_setup_buffer_and_trigger(indio_dev);
635 if (ret < 0)
636 goto error_clk_disable_unprepare;
637
638 ret = iio_device_register(indio_dev);
639 if (ret < 0) {
640 dev_err(&spi->dev, "Failed to register iio device\n");
641 goto error_remove_trigger;
642 }
643
644 return 0;
645
646error_remove_trigger:
647 ad_sd_cleanup_buffer_and_trigger(indio_dev);
648error_clk_disable_unprepare:
649 clk_disable_unprepare(st->mclk);
650error_regulator_disable:
651 for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
652 if (!IS_ERR_OR_NULL(st->vref[i]))
653 regulator_disable(st->vref[i]);
654 }
655
656 return ret;
657}
658
659static int ad7124_remove(struct spi_device *spi)
660{
661 struct iio_dev *indio_dev = spi_get_drvdata(spi);
662 struct ad7124_state *st = iio_priv(indio_dev);
663 int i;
664
665 iio_device_unregister(indio_dev);
666 ad_sd_cleanup_buffer_and_trigger(indio_dev);
667 clk_disable_unprepare(st->mclk);
668
669 for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
670 if (!IS_ERR_OR_NULL(st->vref[i]))
671 regulator_disable(st->vref[i]);
672 }
673
674 return 0;
675}
676
677static const struct spi_device_id ad7124_id_table[] = {
678 { "ad7124-4", ID_AD7124_4 },
679 { "ad7124-8", ID_AD7124_8 },
680 {}
681};
682MODULE_DEVICE_TABLE(spi, ad7124_id_table);
683
684static const struct of_device_id ad7124_of_match[] = {
685 { .compatible = "adi,ad7124-4" },
686 { .compatible = "adi,ad7124-8" },
687 { },
688};
689MODULE_DEVICE_TABLE(of, ad7124_of_match);
690
691static struct spi_driver ad71124_driver = {
692 .driver = {
693 .name = "ad7124",
694 .of_match_table = ad7124_of_match,
695 },
696 .probe = ad7124_probe,
697 .remove = ad7124_remove,
698 .id_table = ad7124_id_table,
699};
700module_spi_driver(ad71124_driver);
701
702MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
703MODULE_DESCRIPTION("Analog Devices AD7124 SPI driver");
704MODULE_LICENSE("GPL");