xref: /linux/drivers/iio/adc/ad7606.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AD7606 SPI ADC driver
4  *
5  * Copyright 2011 Analog Devices Inc.
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/property.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/sysfs.h>
20 #include <linux/util_macros.h>
21 
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/trigger.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/trigger_consumer.h>
28 
29 #include "ad7606.h"
30 
31 /*
32  * Scales are computed as 5000/32768 and 10000/32768 respectively,
33  * so that when applied to the raw values they provide mV values
34  */
35 static const unsigned int ad7606_scale_avail[2] = {
36 	152588, 305176
37 };
38 
39 
40 static const unsigned int ad7616_sw_scale_avail[3] = {
41 	76293, 152588, 305176
42 };
43 
44 static const unsigned int ad7606_oversampling_avail[7] = {
45 	1, 2, 4, 8, 16, 32, 64,
46 };
47 
48 static const unsigned int ad7616_oversampling_avail[8] = {
49 	1, 2, 4, 8, 16, 32, 64, 128,
50 };
51 
52 static int ad7606_reset(struct ad7606_state *st)
53 {
54 	if (st->gpio_reset) {
55 		gpiod_set_value(st->gpio_reset, 1);
56 		ndelay(100); /* t_reset >= 100ns */
57 		gpiod_set_value(st->gpio_reset, 0);
58 		return 0;
59 	}
60 
61 	return -ENODEV;
62 }
63 
64 static int ad7606_reg_access(struct iio_dev *indio_dev,
65 			     unsigned int reg,
66 			     unsigned int writeval,
67 			     unsigned int *readval)
68 {
69 	struct ad7606_state *st = iio_priv(indio_dev);
70 	int ret;
71 
72 	mutex_lock(&st->lock);
73 	if (readval) {
74 		ret = st->bops->reg_read(st, reg);
75 		if (ret < 0)
76 			goto err_unlock;
77 		*readval = ret;
78 		ret = 0;
79 	} else {
80 		ret = st->bops->reg_write(st, reg, writeval);
81 	}
82 err_unlock:
83 	mutex_unlock(&st->lock);
84 	return ret;
85 }
86 
87 static int ad7606_read_samples(struct ad7606_state *st)
88 {
89 	unsigned int num = st->chip_info->num_channels - 1;
90 	u16 *data = st->data;
91 	int ret;
92 
93 	/*
94 	 * The frstdata signal is set to high while and after reading the sample
95 	 * of the first channel and low for all other channels. This can be used
96 	 * to check that the incoming data is correctly aligned. During normal
97 	 * operation the data should never become unaligned, but some glitch or
98 	 * electrostatic discharge might cause an extra read or clock cycle.
99 	 * Monitoring the frstdata signal allows to recover from such failure
100 	 * situations.
101 	 */
102 
103 	if (st->gpio_frstdata) {
104 		ret = st->bops->read_block(st->dev, 1, data);
105 		if (ret)
106 			return ret;
107 
108 		if (!gpiod_get_value(st->gpio_frstdata)) {
109 			ad7606_reset(st);
110 			return -EIO;
111 		}
112 
113 		data++;
114 		num--;
115 	}
116 
117 	return st->bops->read_block(st->dev, num, data);
118 }
119 
120 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
121 {
122 	struct iio_poll_func *pf = p;
123 	struct iio_dev *indio_dev = pf->indio_dev;
124 	struct ad7606_state *st = iio_priv(indio_dev);
125 	int ret;
126 
127 	mutex_lock(&st->lock);
128 
129 	ret = ad7606_read_samples(st);
130 	if (ret == 0)
131 		iio_push_to_buffers_with_timestamp(indio_dev, st->data,
132 						   iio_get_time_ns(indio_dev));
133 
134 	iio_trigger_notify_done(indio_dev->trig);
135 	/* The rising edge of the CONVST signal starts a new conversion. */
136 	gpiod_set_value(st->gpio_convst, 1);
137 
138 	mutex_unlock(&st->lock);
139 
140 	return IRQ_HANDLED;
141 }
142 
143 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
144 {
145 	struct ad7606_state *st = iio_priv(indio_dev);
146 	int ret;
147 
148 	gpiod_set_value(st->gpio_convst, 1);
149 	ret = wait_for_completion_timeout(&st->completion,
150 					  msecs_to_jiffies(1000));
151 	if (!ret) {
152 		ret = -ETIMEDOUT;
153 		goto error_ret;
154 	}
155 
156 	ret = ad7606_read_samples(st);
157 	if (ret == 0)
158 		ret = st->data[ch];
159 
160 error_ret:
161 	gpiod_set_value(st->gpio_convst, 0);
162 
163 	return ret;
164 }
165 
166 static int ad7606_read_raw(struct iio_dev *indio_dev,
167 			   struct iio_chan_spec const *chan,
168 			   int *val,
169 			   int *val2,
170 			   long m)
171 {
172 	int ret, ch = 0;
173 	struct ad7606_state *st = iio_priv(indio_dev);
174 
175 	switch (m) {
176 	case IIO_CHAN_INFO_RAW:
177 		iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
178 			ret = ad7606_scan_direct(indio_dev, chan->address);
179 			if (ret < 0)
180 				return ret;
181 			*val = (short) ret;
182 			return IIO_VAL_INT;
183 		}
184 		unreachable();
185 	case IIO_CHAN_INFO_SCALE:
186 		if (st->sw_mode_en)
187 			ch = chan->address;
188 		*val = 0;
189 		*val2 = st->scale_avail[st->range[ch]];
190 		return IIO_VAL_INT_PLUS_MICRO;
191 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
192 		*val = st->oversampling;
193 		return IIO_VAL_INT;
194 	}
195 	return -EINVAL;
196 }
197 
198 static ssize_t ad7606_show_avail(char *buf, const unsigned int *vals,
199 				 unsigned int n, bool micros)
200 {
201 	size_t len = 0;
202 	int i;
203 
204 	for (i = 0; i < n; i++) {
205 		len += scnprintf(buf + len, PAGE_SIZE - len,
206 			micros ? "0.%06u " : "%u ", vals[i]);
207 	}
208 	buf[len - 1] = '\n';
209 
210 	return len;
211 }
212 
213 static ssize_t in_voltage_scale_available_show(struct device *dev,
214 					       struct device_attribute *attr,
215 					       char *buf)
216 {
217 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
218 	struct ad7606_state *st = iio_priv(indio_dev);
219 
220 	return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true);
221 }
222 
223 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
224 
225 static int ad7606_write_scale_hw(struct iio_dev *indio_dev, int ch, int val)
226 {
227 	struct ad7606_state *st = iio_priv(indio_dev);
228 
229 	gpiod_set_value(st->gpio_range, val);
230 
231 	return 0;
232 }
233 
234 static int ad7606_write_os_hw(struct iio_dev *indio_dev, int val)
235 {
236 	struct ad7606_state *st = iio_priv(indio_dev);
237 	DECLARE_BITMAP(values, 3);
238 
239 	values[0] = val;
240 
241 	gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
242 			      st->gpio_os->info, values);
243 
244 	/* AD7616 requires a reset to update value */
245 	if (st->chip_info->os_req_reset)
246 		ad7606_reset(st);
247 
248 	return 0;
249 }
250 
251 static int ad7606_write_raw(struct iio_dev *indio_dev,
252 			    struct iio_chan_spec const *chan,
253 			    int val,
254 			    int val2,
255 			    long mask)
256 {
257 	struct ad7606_state *st = iio_priv(indio_dev);
258 	int i, ret, ch = 0;
259 
260 	switch (mask) {
261 	case IIO_CHAN_INFO_SCALE:
262 		mutex_lock(&st->lock);
263 		i = find_closest(val2, st->scale_avail, st->num_scales);
264 		if (st->sw_mode_en)
265 			ch = chan->address;
266 		ret = st->write_scale(indio_dev, ch, i);
267 		if (ret < 0) {
268 			mutex_unlock(&st->lock);
269 			return ret;
270 		}
271 		st->range[ch] = i;
272 		mutex_unlock(&st->lock);
273 
274 		return 0;
275 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
276 		if (val2)
277 			return -EINVAL;
278 		i = find_closest(val, st->oversampling_avail,
279 				 st->num_os_ratios);
280 		mutex_lock(&st->lock);
281 		ret = st->write_os(indio_dev, i);
282 		if (ret < 0) {
283 			mutex_unlock(&st->lock);
284 			return ret;
285 		}
286 		st->oversampling = st->oversampling_avail[i];
287 		mutex_unlock(&st->lock);
288 
289 		return 0;
290 	default:
291 		return -EINVAL;
292 	}
293 }
294 
295 static ssize_t ad7606_oversampling_ratio_avail(struct device *dev,
296 					       struct device_attribute *attr,
297 					       char *buf)
298 {
299 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
300 	struct ad7606_state *st = iio_priv(indio_dev);
301 
302 	return ad7606_show_avail(buf, st->oversampling_avail,
303 				 st->num_os_ratios, false);
304 }
305 
306 static IIO_DEVICE_ATTR(oversampling_ratio_available, 0444,
307 		       ad7606_oversampling_ratio_avail, NULL, 0);
308 
309 static struct attribute *ad7606_attributes_os_and_range[] = {
310 	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
311 	&iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
312 	NULL,
313 };
314 
315 static const struct attribute_group ad7606_attribute_group_os_and_range = {
316 	.attrs = ad7606_attributes_os_and_range,
317 };
318 
319 static struct attribute *ad7606_attributes_os[] = {
320 	&iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
321 	NULL,
322 };
323 
324 static const struct attribute_group ad7606_attribute_group_os = {
325 	.attrs = ad7606_attributes_os,
326 };
327 
328 static struct attribute *ad7606_attributes_range[] = {
329 	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
330 	NULL,
331 };
332 
333 static const struct attribute_group ad7606_attribute_group_range = {
334 	.attrs = ad7606_attributes_range,
335 };
336 
337 static const struct iio_chan_spec ad7605_channels[] = {
338 	IIO_CHAN_SOFT_TIMESTAMP(4),
339 	AD7605_CHANNEL(0),
340 	AD7605_CHANNEL(1),
341 	AD7605_CHANNEL(2),
342 	AD7605_CHANNEL(3),
343 };
344 
345 static const struct iio_chan_spec ad7606_channels[] = {
346 	IIO_CHAN_SOFT_TIMESTAMP(8),
347 	AD7606_CHANNEL(0),
348 	AD7606_CHANNEL(1),
349 	AD7606_CHANNEL(2),
350 	AD7606_CHANNEL(3),
351 	AD7606_CHANNEL(4),
352 	AD7606_CHANNEL(5),
353 	AD7606_CHANNEL(6),
354 	AD7606_CHANNEL(7),
355 };
356 
357 /*
358  * The current assumption that this driver makes for AD7616, is that it's
359  * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
360  * To activate them, following pins must be pulled high:
361  *	-SER/PAR
362  *	-SEQEN
363  * And following pins must be pulled low:
364  *	-WR/BURST
365  *	-DB4/SER1W
366  */
367 static const struct iio_chan_spec ad7616_channels[] = {
368 	IIO_CHAN_SOFT_TIMESTAMP(16),
369 	AD7606_CHANNEL(0),
370 	AD7606_CHANNEL(1),
371 	AD7606_CHANNEL(2),
372 	AD7606_CHANNEL(3),
373 	AD7606_CHANNEL(4),
374 	AD7606_CHANNEL(5),
375 	AD7606_CHANNEL(6),
376 	AD7606_CHANNEL(7),
377 	AD7606_CHANNEL(8),
378 	AD7606_CHANNEL(9),
379 	AD7606_CHANNEL(10),
380 	AD7606_CHANNEL(11),
381 	AD7606_CHANNEL(12),
382 	AD7606_CHANNEL(13),
383 	AD7606_CHANNEL(14),
384 	AD7606_CHANNEL(15),
385 };
386 
387 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
388 	/* More devices added in future */
389 	[ID_AD7605_4] = {
390 		.channels = ad7605_channels,
391 		.num_channels = 5,
392 	},
393 	[ID_AD7606_8] = {
394 		.channels = ad7606_channels,
395 		.num_channels = 9,
396 		.oversampling_avail = ad7606_oversampling_avail,
397 		.oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
398 	},
399 	[ID_AD7606_6] = {
400 		.channels = ad7606_channels,
401 		.num_channels = 7,
402 		.oversampling_avail = ad7606_oversampling_avail,
403 		.oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
404 	},
405 	[ID_AD7606_4] = {
406 		.channels = ad7606_channels,
407 		.num_channels = 5,
408 		.oversampling_avail = ad7606_oversampling_avail,
409 		.oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
410 	},
411 	[ID_AD7606B] = {
412 		.channels = ad7606_channels,
413 		.num_channels = 9,
414 		.oversampling_avail = ad7606_oversampling_avail,
415 		.oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
416 	},
417 	[ID_AD7616] = {
418 		.channels = ad7616_channels,
419 		.num_channels = 17,
420 		.oversampling_avail = ad7616_oversampling_avail,
421 		.oversampling_num = ARRAY_SIZE(ad7616_oversampling_avail),
422 		.os_req_reset = true,
423 		.init_delay_ms = 15,
424 	},
425 };
426 
427 static int ad7606_request_gpios(struct ad7606_state *st)
428 {
429 	struct device *dev = st->dev;
430 
431 	st->gpio_convst = devm_gpiod_get(dev, "adi,conversion-start",
432 					 GPIOD_OUT_LOW);
433 	if (IS_ERR(st->gpio_convst))
434 		return PTR_ERR(st->gpio_convst);
435 
436 	st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
437 	if (IS_ERR(st->gpio_reset))
438 		return PTR_ERR(st->gpio_reset);
439 
440 	st->gpio_range = devm_gpiod_get_optional(dev, "adi,range",
441 						 GPIOD_OUT_LOW);
442 	if (IS_ERR(st->gpio_range))
443 		return PTR_ERR(st->gpio_range);
444 
445 	st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
446 						   GPIOD_OUT_HIGH);
447 	if (IS_ERR(st->gpio_standby))
448 		return PTR_ERR(st->gpio_standby);
449 
450 	st->gpio_frstdata = devm_gpiod_get_optional(dev, "adi,first-data",
451 						    GPIOD_IN);
452 	if (IS_ERR(st->gpio_frstdata))
453 		return PTR_ERR(st->gpio_frstdata);
454 
455 	if (!st->chip_info->oversampling_num)
456 		return 0;
457 
458 	st->gpio_os = devm_gpiod_get_array_optional(dev,
459 						    "adi,oversampling-ratio",
460 						    GPIOD_OUT_LOW);
461 	return PTR_ERR_OR_ZERO(st->gpio_os);
462 }
463 
464 /*
465  * The BUSY signal indicates when conversions are in progress, so when a rising
466  * edge of CONVST is applied, BUSY goes logic high and transitions low at the
467  * end of the entire conversion process. The falling edge of the BUSY signal
468  * triggers this interrupt.
469  */
470 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
471 {
472 	struct iio_dev *indio_dev = dev_id;
473 	struct ad7606_state *st = iio_priv(indio_dev);
474 
475 	if (iio_buffer_enabled(indio_dev)) {
476 		gpiod_set_value(st->gpio_convst, 0);
477 		iio_trigger_poll_nested(st->trig);
478 	} else {
479 		complete(&st->completion);
480 	}
481 
482 	return IRQ_HANDLED;
483 };
484 
485 static int ad7606_validate_trigger(struct iio_dev *indio_dev,
486 				   struct iio_trigger *trig)
487 {
488 	struct ad7606_state *st = iio_priv(indio_dev);
489 
490 	if (st->trig != trig)
491 		return -EINVAL;
492 
493 	return 0;
494 }
495 
496 static int ad7606_buffer_postenable(struct iio_dev *indio_dev)
497 {
498 	struct ad7606_state *st = iio_priv(indio_dev);
499 
500 	gpiod_set_value(st->gpio_convst, 1);
501 
502 	return 0;
503 }
504 
505 static int ad7606_buffer_predisable(struct iio_dev *indio_dev)
506 {
507 	struct ad7606_state *st = iio_priv(indio_dev);
508 
509 	gpiod_set_value(st->gpio_convst, 0);
510 
511 	return 0;
512 }
513 
514 static const struct iio_buffer_setup_ops ad7606_buffer_ops = {
515 	.postenable = &ad7606_buffer_postenable,
516 	.predisable = &ad7606_buffer_predisable,
517 };
518 
519 static const struct iio_info ad7606_info_no_os_or_range = {
520 	.read_raw = &ad7606_read_raw,
521 	.validate_trigger = &ad7606_validate_trigger,
522 };
523 
524 static const struct iio_info ad7606_info_os_and_range = {
525 	.read_raw = &ad7606_read_raw,
526 	.write_raw = &ad7606_write_raw,
527 	.attrs = &ad7606_attribute_group_os_and_range,
528 	.validate_trigger = &ad7606_validate_trigger,
529 };
530 
531 static const struct iio_info ad7606_info_os_range_and_debug = {
532 	.read_raw = &ad7606_read_raw,
533 	.write_raw = &ad7606_write_raw,
534 	.debugfs_reg_access = &ad7606_reg_access,
535 	.attrs = &ad7606_attribute_group_os_and_range,
536 	.validate_trigger = &ad7606_validate_trigger,
537 };
538 
539 static const struct iio_info ad7606_info_os = {
540 	.read_raw = &ad7606_read_raw,
541 	.write_raw = &ad7606_write_raw,
542 	.attrs = &ad7606_attribute_group_os,
543 	.validate_trigger = &ad7606_validate_trigger,
544 };
545 
546 static const struct iio_info ad7606_info_range = {
547 	.read_raw = &ad7606_read_raw,
548 	.write_raw = &ad7606_write_raw,
549 	.attrs = &ad7606_attribute_group_range,
550 	.validate_trigger = &ad7606_validate_trigger,
551 };
552 
553 static const struct iio_trigger_ops ad7606_trigger_ops = {
554 	.validate_device = iio_trigger_validate_own_device,
555 };
556 
557 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
558 		 const char *name, unsigned int id,
559 		 const struct ad7606_bus_ops *bops)
560 {
561 	struct ad7606_state *st;
562 	int ret;
563 	struct iio_dev *indio_dev;
564 
565 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
566 	if (!indio_dev)
567 		return -ENOMEM;
568 
569 	st = iio_priv(indio_dev);
570 	dev_set_drvdata(dev, indio_dev);
571 
572 	st->dev = dev;
573 	mutex_init(&st->lock);
574 	st->bops = bops;
575 	st->base_address = base_address;
576 	/* tied to logic low, analog input range is +/- 5V */
577 	st->range[0] = 0;
578 	st->oversampling = 1;
579 	st->scale_avail = ad7606_scale_avail;
580 	st->num_scales = ARRAY_SIZE(ad7606_scale_avail);
581 
582 	ret = devm_regulator_get_enable(dev, "avcc");
583 	if (ret)
584 		return dev_err_probe(dev, ret,
585 				     "Failed to enable specified AVcc supply\n");
586 
587 	st->chip_info = &ad7606_chip_info_tbl[id];
588 
589 	if (st->chip_info->oversampling_num) {
590 		st->oversampling_avail = st->chip_info->oversampling_avail;
591 		st->num_os_ratios = st->chip_info->oversampling_num;
592 	}
593 
594 	ret = ad7606_request_gpios(st);
595 	if (ret)
596 		return ret;
597 
598 	if (st->gpio_os) {
599 		if (st->gpio_range)
600 			indio_dev->info = &ad7606_info_os_and_range;
601 		else
602 			indio_dev->info = &ad7606_info_os;
603 	} else {
604 		if (st->gpio_range)
605 			indio_dev->info = &ad7606_info_range;
606 		else
607 			indio_dev->info = &ad7606_info_no_os_or_range;
608 	}
609 	indio_dev->modes = INDIO_DIRECT_MODE;
610 	indio_dev->name = name;
611 	indio_dev->channels = st->chip_info->channels;
612 	indio_dev->num_channels = st->chip_info->num_channels;
613 
614 	init_completion(&st->completion);
615 
616 	ret = ad7606_reset(st);
617 	if (ret)
618 		dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
619 
620 	/* AD7616 requires al least 15ms to reconfigure after a reset */
621 	if (st->chip_info->init_delay_ms) {
622 		if (msleep_interruptible(st->chip_info->init_delay_ms))
623 			return -ERESTARTSYS;
624 	}
625 
626 	st->write_scale = ad7606_write_scale_hw;
627 	st->write_os = ad7606_write_os_hw;
628 
629 	if (st->bops->sw_mode_config)
630 		st->sw_mode_en = device_property_present(st->dev,
631 							 "adi,sw-mode");
632 
633 	if (st->sw_mode_en) {
634 		/* Scale of 0.076293 is only available in sw mode */
635 		st->scale_avail = ad7616_sw_scale_avail;
636 		st->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail);
637 
638 		/* After reset, in software mode, ±10 V is set by default */
639 		memset32(st->range, 2, ARRAY_SIZE(st->range));
640 		indio_dev->info = &ad7606_info_os_range_and_debug;
641 
642 		ret = st->bops->sw_mode_config(indio_dev);
643 		if (ret < 0)
644 			return ret;
645 	}
646 
647 	st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
648 					  indio_dev->name,
649 					  iio_device_id(indio_dev));
650 	if (!st->trig)
651 		return -ENOMEM;
652 
653 	st->trig->ops = &ad7606_trigger_ops;
654 	iio_trigger_set_drvdata(st->trig, indio_dev);
655 	ret = devm_iio_trigger_register(dev, st->trig);
656 	if (ret)
657 		return ret;
658 
659 	indio_dev->trig = iio_trigger_get(st->trig);
660 
661 	ret = devm_request_threaded_irq(dev, irq,
662 					NULL,
663 					&ad7606_interrupt,
664 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
665 					name, indio_dev);
666 	if (ret)
667 		return ret;
668 
669 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
670 					      &iio_pollfunc_store_time,
671 					      &ad7606_trigger_handler,
672 					      &ad7606_buffer_ops);
673 	if (ret)
674 		return ret;
675 
676 	return devm_iio_device_register(dev, indio_dev);
677 }
678 EXPORT_SYMBOL_NS_GPL(ad7606_probe, IIO_AD7606);
679 
680 #ifdef CONFIG_PM_SLEEP
681 
682 static int ad7606_suspend(struct device *dev)
683 {
684 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
685 	struct ad7606_state *st = iio_priv(indio_dev);
686 
687 	if (st->gpio_standby) {
688 		gpiod_set_value(st->gpio_range, 1);
689 		gpiod_set_value(st->gpio_standby, 0);
690 	}
691 
692 	return 0;
693 }
694 
695 static int ad7606_resume(struct device *dev)
696 {
697 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
698 	struct ad7606_state *st = iio_priv(indio_dev);
699 
700 	if (st->gpio_standby) {
701 		gpiod_set_value(st->gpio_range, st->range[0]);
702 		gpiod_set_value(st->gpio_standby, 1);
703 		ad7606_reset(st);
704 	}
705 
706 	return 0;
707 }
708 
709 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
710 EXPORT_SYMBOL_NS_GPL(ad7606_pm_ops, IIO_AD7606);
711 
712 #endif
713 
714 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
715 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
716 MODULE_LICENSE("GPL v2");
717