ad7606.c 17.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7
/*
 * AD7606 SPI ADC driver
 *
 * Copyright 2011 Analog Devices Inc.
 */

8
#include <linux/delay.h>
9 10
#include <linux/device.h>
#include <linux/err.h>
11
#include <linux/gpio/consumer.h>
12 13
#include <linux/interrupt.h>
#include <linux/kernel.h>
14
#include <linux/module.h>
15 16 17 18
#include <linux/regulator/consumer.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
19
#include <linux/util_macros.h>
20

21 22
#include <linux/iio/iio.h>
#include <linux/iio/buffer.h>
23
#include <linux/iio/sysfs.h>
24
#include <linux/iio/trigger.h>
25
#include <linux/iio/triggered_buffer.h>
26
#include <linux/iio/trigger_consumer.h>
27 28 29

#include "ad7606.h"

30 31 32 33
/*
 * Scales are computed as 5000/32768 and 10000/32768 respectively,
 * so that when applied to the raw values they provide mV values
 */
34
static const unsigned int ad7606_scale_avail[2] = {
35 36 37
	152588, 305176
};

38 39 40 41 42

static const unsigned int ad7616_sw_scale_avail[3] = {
	76293, 152588, 305176
};

43 44
static const unsigned int ad7606_oversampling_avail[7] = {
	1, 2, 4, 8, 16, 32, 64,
45 46
};

47 48 49 50
static const unsigned int ad7616_oversampling_avail[8] = {
	1, 2, 4, 8, 16, 32, 64, 128,
};

51
static int ad7606_reset(struct ad7606_state *st)
52
{
53 54
	if (st->gpio_reset) {
		gpiod_set_value(st->gpio_reset, 1);
55
		ndelay(100); /* t_reset >= 100ns */
56
		gpiod_set_value(st->gpio_reset, 0);
57 58 59 60 61 62
		return 0;
	}

	return -ENODEV;
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static int ad7606_reg_access(struct iio_dev *indio_dev,
			     unsigned int reg,
			     unsigned int writeval,
			     unsigned int *readval)
{
	struct ad7606_state *st = iio_priv(indio_dev);
	int ret;

	mutex_lock(&st->lock);
	if (readval) {
		ret = st->bops->reg_read(st, reg);
		if (ret < 0)
			goto err_unlock;
		*readval = ret;
		ret = 0;
	} else {
		ret = st->bops->reg_write(st, reg, writeval);
	}
err_unlock:
	mutex_unlock(&st->lock);
	return ret;
}

86
static int ad7606_read_samples(struct ad7606_state *st)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
{
	unsigned int num = st->chip_info->num_channels;
	u16 *data = st->data;
	int ret;

	/*
	 * The frstdata signal is set to high while and after reading the sample
	 * of the first channel and low for all other channels. This can be used
	 * to check that the incoming data is correctly aligned. During normal
	 * operation the data should never become unaligned, but some glitch or
	 * electrostatic discharge might cause an extra read or clock cycle.
	 * Monitoring the frstdata signal allows to recover from such failure
	 * situations.
	 */

102
	if (st->gpio_frstdata) {
103 104 105 106
		ret = st->bops->read_block(st->dev, 1, data);
		if (ret)
			return ret;

107
		if (!gpiod_get_value(st->gpio_frstdata)) {
108 109 110 111 112 113 114 115 116 117 118
			ad7606_reset(st);
			return -EIO;
		}

		data++;
		num--;
	}

	return st->bops->read_block(st->dev, num, data);
}

119 120 121
static irqreturn_t ad7606_trigger_handler(int irq, void *p)
{
	struct iio_poll_func *pf = p;
122 123
	struct iio_dev *indio_dev = pf->indio_dev;
	struct ad7606_state *st = iio_priv(indio_dev);
124 125
	int ret;

126 127
	mutex_lock(&st->lock);

128 129 130 131 132 133
	ret = ad7606_read_samples(st);
	if (ret == 0)
		iio_push_to_buffers_with_timestamp(indio_dev, st->data,
						   iio_get_time_ns(indio_dev));

	iio_trigger_notify_done(indio_dev->trig);
134 135 136 137 138 139
	/* The rising edge of the CONVST signal starts a new conversion. */
	gpiod_set_value(st->gpio_convst, 1);

	mutex_unlock(&st->lock);

	return IRQ_HANDLED;
140 141
}

142
static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
143
{
144
	struct ad7606_state *st = iio_priv(indio_dev);
145 146
	int ret;

147
	gpiod_set_value(st->gpio_convst, 1);
148 149 150 151
	ret = wait_for_completion_timeout(&st->completion,
					  msecs_to_jiffies(1000));
	if (!ret) {
		ret = -ETIMEDOUT;
152
		goto error_ret;
153
	}
154

155 156 157
	ret = ad7606_read_samples(st);
	if (ret == 0)
		ret = st->data[ch];
158 159

error_ret:
160
	gpiod_set_value(st->gpio_convst, 0);
161 162 163 164

	return ret;
}

165
static int ad7606_read_raw(struct iio_dev *indio_dev,
166 167 168 169
			   struct iio_chan_spec const *chan,
			   int *val,
			   int *val2,
			   long m)
170
{
171
	int ret, ch = 0;
172
	struct ad7606_state *st = iio_priv(indio_dev);
173 174

	switch (m) {
175
	case IIO_CHAN_INFO_RAW:
176 177 178 179 180 181
		ret = iio_device_claim_direct_mode(indio_dev);
		if (ret)
			return ret;

		ret = ad7606_scan_direct(indio_dev, chan->address);
		iio_device_release_direct_mode(indio_dev);
182 183 184

		if (ret < 0)
			return ret;
185
		*val = (short)ret;
186
		return IIO_VAL_INT;
187
	case IIO_CHAN_INFO_SCALE:
188 189
		if (st->sw_mode_en)
			ch = chan->address;
190
		*val = 0;
191
		*val2 = st->scale_avail[st->range[ch]];
192
		return IIO_VAL_INT_PLUS_MICRO;
193 194 195
	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
		*val = st->oversampling;
		return IIO_VAL_INT;
196 197
	}
	return -EINVAL;
198 199
}

200 201
static ssize_t ad7606_show_avail(char *buf, const unsigned int *vals,
				 unsigned int n, bool micros)
202
{
203 204
	size_t len = 0;
	int i;
205

206 207 208 209
	for (i = 0; i < n; i++) {
		len += scnprintf(buf + len, PAGE_SIZE - len,
			micros ? "0.%06u " : "%u ", vals[i]);
	}
210
	buf[len - 1] = '\n';
211

212
	return len;
213 214
}

215 216 217 218 219 220 221 222 223 224
static ssize_t in_voltage_scale_available_show(struct device *dev,
					       struct device_attribute *attr,
					       char *buf)
{
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct ad7606_state *st = iio_priv(indio_dev);

	return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true);
}

225
static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static int ad7606_write_scale_hw(struct iio_dev *indio_dev, int ch, int val)
{
	struct ad7606_state *st = iio_priv(indio_dev);

	gpiod_set_value(st->gpio_range, val);

	return 0;
}

static int ad7606_write_os_hw(struct iio_dev *indio_dev, int val)
{
	struct ad7606_state *st = iio_priv(indio_dev);
	DECLARE_BITMAP(values, 3);

	values[0] = val;

	gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
			      st->gpio_os->info, values);

	/* AD7616 requires a reset to update value */
	if (st->chip_info->os_req_reset)
		ad7606_reset(st);

	return 0;
}

253 254 255 256 257
static int ad7606_write_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int val,
			    int val2,
			    long mask)
258
{
259
	struct ad7606_state *st = iio_priv(indio_dev);
260
	int i, ret, ch = 0;
261

262
	switch (mask) {
263
	case IIO_CHAN_INFO_SCALE:
264
		mutex_lock(&st->lock);
265
		i = find_closest(val2, st->scale_avail, st->num_scales);
266 267 268
		if (st->sw_mode_en)
			ch = chan->address;
		ret = st->write_scale(indio_dev, ch, i);
269 270 271 272 273
		if (ret < 0) {
			mutex_unlock(&st->lock);
			return ret;
		}
		st->range[ch] = i;
274
		mutex_unlock(&st->lock);
275

276
		return 0;
277 278 279
	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
		if (val2)
			return -EINVAL;
280 281
		i = find_closest(val, st->oversampling_avail,
				 st->num_os_ratios);
282
		mutex_lock(&st->lock);
283 284 285 286 287
		ret = st->write_os(indio_dev, i);
		if (ret < 0) {
			mutex_unlock(&st->lock);
			return ret;
		}
288
		st->oversampling = st->oversampling_avail[i];
289
		mutex_unlock(&st->lock);
290

291 292 293
		return 0;
	default:
		return -EINVAL;
294 295 296
	}
}

297 298 299 300 301 302 303 304 305 306 307 308 309
static ssize_t ad7606_oversampling_ratio_avail(struct device *dev,
					       struct device_attribute *attr,
					       char *buf)
{
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct ad7606_state *st = iio_priv(indio_dev);

	return ad7606_show_avail(buf, st->oversampling_avail,
				 st->num_os_ratios, false);
}

static IIO_DEVICE_ATTR(oversampling_ratio_available, 0444,
		       ad7606_oversampling_ratio_avail, NULL, 0);
310

311
static struct attribute *ad7606_attributes_os_and_range[] = {
312
	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
313
	&iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
314 315 316
	NULL,
};

317 318 319
static const struct attribute_group ad7606_attribute_group_os_and_range = {
	.attrs = ad7606_attributes_os_and_range,
};
320

321
static struct attribute *ad7606_attributes_os[] = {
322
	&iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
323 324
	NULL,
};
325

326 327 328 329 330
static const struct attribute_group ad7606_attribute_group_os = {
	.attrs = ad7606_attributes_os,
};

static struct attribute *ad7606_attributes_range[] = {
331
	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
332 333 334 335 336
	NULL,
};

static const struct attribute_group ad7606_attribute_group_range = {
	.attrs = ad7606_attributes_range,
337 338
};

339 340 341 342 343 344 345 346
static const struct iio_chan_spec ad7605_channels[] = {
	IIO_CHAN_SOFT_TIMESTAMP(4),
	AD7605_CHANNEL(0),
	AD7605_CHANNEL(1),
	AD7605_CHANNEL(2),
	AD7605_CHANNEL(3),
};

347 348
static const struct iio_chan_spec ad7606_channels[] = {
	IIO_CHAN_SOFT_TIMESTAMP(8),
349 350 351 352 353 354 355 356
	AD7606_CHANNEL(0),
	AD7606_CHANNEL(1),
	AD7606_CHANNEL(2),
	AD7606_CHANNEL(3),
	AD7606_CHANNEL(4),
	AD7606_CHANNEL(5),
	AD7606_CHANNEL(6),
	AD7606_CHANNEL(7),
357 358
};

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
/*
 * The current assumption that this driver makes for AD7616, is that it's
 * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
 * To activate them, following pins must be pulled high:
 *	-SER/PAR
 *	-SEQEN
 * And following pins must be pulled low:
 *	-WR/BURST
 *	-DB4/SER1W
 */
static const struct iio_chan_spec ad7616_channels[] = {
	IIO_CHAN_SOFT_TIMESTAMP(16),
	AD7606_CHANNEL(0),
	AD7606_CHANNEL(1),
	AD7606_CHANNEL(2),
	AD7606_CHANNEL(3),
	AD7606_CHANNEL(4),
	AD7606_CHANNEL(5),
	AD7606_CHANNEL(6),
	AD7606_CHANNEL(7),
	AD7606_CHANNEL(8),
	AD7606_CHANNEL(9),
	AD7606_CHANNEL(10),
	AD7606_CHANNEL(11),
	AD7606_CHANNEL(12),
	AD7606_CHANNEL(13),
	AD7606_CHANNEL(14),
	AD7606_CHANNEL(15),
};

389
static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
390
	/* More devices added in future */
391 392 393 394
	[ID_AD7605_4] = {
		.channels = ad7605_channels,
		.num_channels = 5,
	},
395
	[ID_AD7606_8] = {
396 397
		.channels = ad7606_channels,
		.num_channels = 9,
398 399
		.oversampling_avail = ad7606_oversampling_avail,
		.oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
400 401
	},
	[ID_AD7606_6] = {
402 403
		.channels = ad7606_channels,
		.num_channels = 7,
404 405
		.oversampling_avail = ad7606_oversampling_avail,
		.oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
406 407
	},
	[ID_AD7606_4] = {
408 409
		.channels = ad7606_channels,
		.num_channels = 5,
410 411
		.oversampling_avail = ad7606_oversampling_avail,
		.oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
412
	},
413 414 415 416 417 418 419
	[ID_AD7616] = {
		.channels = ad7616_channels,
		.num_channels = 17,
		.oversampling_avail = ad7616_oversampling_avail,
		.oversampling_num = ARRAY_SIZE(ad7616_oversampling_avail),
		.os_req_reset = true,
	},
420 421 422 423
};

static int ad7606_request_gpios(struct ad7606_state *st)
{
424
	struct device *dev = st->dev;
425

426
	st->gpio_convst = devm_gpiod_get(dev, "adi,conversion-start",
427 428 429
					 GPIOD_OUT_LOW);
	if (IS_ERR(st->gpio_convst))
		return PTR_ERR(st->gpio_convst);
430

431 432 433
	st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(st->gpio_reset))
		return PTR_ERR(st->gpio_reset);
434

435 436
	st->gpio_range = devm_gpiod_get_optional(dev, "adi,range",
						 GPIOD_OUT_LOW);
437 438
	if (IS_ERR(st->gpio_range))
		return PTR_ERR(st->gpio_range);
439

440 441 442 443
	st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
						   GPIOD_OUT_HIGH);
	if (IS_ERR(st->gpio_standby))
		return PTR_ERR(st->gpio_standby);
444

445
	st->gpio_frstdata = devm_gpiod_get_optional(dev, "adi,first-data",
446 447 448
						    GPIOD_IN);
	if (IS_ERR(st->gpio_frstdata))
		return PTR_ERR(st->gpio_frstdata);
449

450
	if (!st->chip_info->oversampling_num)
451 452
		return 0;

453 454
	st->gpio_os = devm_gpiod_get_array_optional(dev,
						    "adi,oversampling-ratio",
455
						    GPIOD_OUT_LOW);
456
	return PTR_ERR_OR_ZERO(st->gpio_os);
457 458
}

459 460 461 462 463
/*
 * The BUSY signal indicates when conversions are in progress, so when a rising
 * edge of CONVST is applied, BUSY goes logic high and transitions low at the
 * end of the entire conversion process. The falling edge of the BUSY signal
 * triggers this interrupt.
464 465 466
 */
static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
{
467 468
	struct iio_dev *indio_dev = dev_id;
	struct ad7606_state *st = iio_priv(indio_dev);
469

470
	if (iio_buffer_enabled(indio_dev)) {
471 472
		gpiod_set_value(st->gpio_convst, 0);
		iio_trigger_poll_chained(st->trig);
473
	} else {
474
		complete(&st->completion);
475 476 477 478 479
	}

	return IRQ_HANDLED;
};

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
static int ad7606_validate_trigger(struct iio_dev *indio_dev,
				   struct iio_trigger *trig)
{
	struct ad7606_state *st = iio_priv(indio_dev);

	if (st->trig != trig)
		return -EINVAL;

	return 0;
}

static int ad7606_buffer_postenable(struct iio_dev *indio_dev)
{
	struct ad7606_state *st = iio_priv(indio_dev);

	iio_triggered_buffer_postenable(indio_dev);
	gpiod_set_value(st->gpio_convst, 1);

	return 0;
}

static int ad7606_buffer_predisable(struct iio_dev *indio_dev)
{
	struct ad7606_state *st = iio_priv(indio_dev);

	gpiod_set_value(st->gpio_convst, 0);

	return iio_triggered_buffer_predisable(indio_dev);
}

static const struct iio_buffer_setup_ops ad7606_buffer_ops = {
	.postenable = &ad7606_buffer_postenable,
	.predisable = &ad7606_buffer_predisable,
};

515
static const struct iio_info ad7606_info_no_os_or_range = {
516
	.read_raw = &ad7606_read_raw,
517
	.validate_trigger = &ad7606_validate_trigger,
518 519 520 521
};

static const struct iio_info ad7606_info_os_and_range = {
	.read_raw = &ad7606_read_raw,
522
	.write_raw = &ad7606_write_raw,
523
	.attrs = &ad7606_attribute_group_os_and_range,
524
	.validate_trigger = &ad7606_validate_trigger,
525 526
};

527 528 529 530 531 532 533 534
static const struct iio_info ad7606_info_os_range_and_debug = {
	.read_raw = &ad7606_read_raw,
	.write_raw = &ad7606_write_raw,
	.debugfs_reg_access = &ad7606_reg_access,
	.attrs = &ad7606_attribute_group_os_and_range,
	.validate_trigger = &ad7606_validate_trigger,
};

535 536
static const struct iio_info ad7606_info_os = {
	.read_raw = &ad7606_read_raw,
537
	.write_raw = &ad7606_write_raw,
538
	.attrs = &ad7606_attribute_group_os,
539
	.validate_trigger = &ad7606_validate_trigger,
540 541 542 543
};

static const struct iio_info ad7606_info_range = {
	.read_raw = &ad7606_read_raw,
544
	.write_raw = &ad7606_write_raw,
545
	.attrs = &ad7606_attribute_group_range,
546 547 548 549 550
	.validate_trigger = &ad7606_validate_trigger,
};

static const struct iio_trigger_ops ad7606_trigger_ops = {
	.validate_device = iio_trigger_validate_own_device,
551 552
};

553 554 555 556 557 558 559
static void ad7606_regulator_disable(void *data)
{
	struct ad7606_state *st = data;

	regulator_disable(st->reg);
}

560 561 562
int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
		 const char *name, unsigned int id,
		 const struct ad7606_bus_ops *bops)
563 564
{
	struct ad7606_state *st;
565
	int ret;
566
	struct iio_dev *indio_dev;
567

568 569
	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
	if (!indio_dev)
570
		return -ENOMEM;
571

572
	st = iio_priv(indio_dev);
573
	dev_set_drvdata(dev, indio_dev);
574

575
	st->dev = dev;
576
	mutex_init(&st->lock);
577 578
	st->bops = bops;
	st->base_address = base_address;
579
	/* tied to logic low, analog input range is +/- 5V */
580
	st->range[0] = 0;
581
	st->oversampling = 1;
582 583
	st->scale_avail = ad7606_scale_avail;
	st->num_scales = ARRAY_SIZE(ad7606_scale_avail);
584

585
	st->reg = devm_regulator_get(dev, "avcc");
586 587 588 589 590 591 592
	if (IS_ERR(st->reg))
		return PTR_ERR(st->reg);

	ret = regulator_enable(st->reg);
	if (ret) {
		dev_err(dev, "Failed to enable specified AVcc supply\n");
		return ret;
593 594
	}

595 596 597 598
	ret = devm_add_action_or_reset(dev, ad7606_regulator_disable, st);
	if (ret)
		return ret;

599 600
	st->chip_info = &ad7606_chip_info_tbl[id];

601 602 603 604 605
	if (st->chip_info->oversampling_num) {
		st->oversampling_avail = st->chip_info->oversampling_avail;
		st->num_os_ratios = st->chip_info->oversampling_num;
	}

606 607
	ret = ad7606_request_gpios(st);
	if (ret)
608
		return ret;
609

610
	indio_dev->dev.parent = dev;
611 612
	if (st->gpio_os) {
		if (st->gpio_range)
613 614 615 616
			indio_dev->info = &ad7606_info_os_and_range;
		else
			indio_dev->info = &ad7606_info_os;
	} else {
617
		if (st->gpio_range)
618 619 620 621
			indio_dev->info = &ad7606_info_range;
		else
			indio_dev->info = &ad7606_info_no_os_or_range;
	}
622
	indio_dev->modes = INDIO_DIRECT_MODE;
623
	indio_dev->name = name;
624 625
	indio_dev->channels = st->chip_info->channels;
	indio_dev->num_channels = st->chip_info->num_channels;
626

627
	init_completion(&st->completion);
628 629 630 631 632

	ret = ad7606_reset(st);
	if (ret)
		dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");

633 634 635 636
	/* AD7616 requires al least 15ms to reconfigure after a reset */
	if (msleep_interruptible(15))
		return -ERESTARTSYS;

637 638 639
	st->write_scale = ad7606_write_scale_hw;
	st->write_os = ad7606_write_os_hw;

640
	if (st->bops->sw_mode_config)
641 642 643 644
		st->sw_mode_en = device_property_present(st->dev,
							 "adi,sw-mode");

	if (st->sw_mode_en) {
645 646 647 648
		/* Scale of 0.076293 is only available in sw mode */
		st->scale_avail = ad7616_sw_scale_avail;
		st->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail);

649 650
		/* After reset, in software mode, ±10 V is set by default */
		memset32(st->range, 2, ARRAY_SIZE(st->range));
651
		indio_dev->info = &ad7606_info_os_range_and_debug;
652

653
		ret = st->bops->sw_mode_config(indio_dev);
654 655 656 657
		if (ret < 0)
			return ret;
	}

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
	st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
					  indio_dev->name, indio_dev->id);
	if (!st->trig)
		return -ENOMEM;

	st->trig->ops = &ad7606_trigger_ops;
	st->trig->dev.parent = dev;
	iio_trigger_set_drvdata(st->trig, indio_dev);
	ret = devm_iio_trigger_register(dev, st->trig);
	if (ret)
		return ret;

	indio_dev->trig = iio_trigger_get(st->trig);

	ret = devm_request_threaded_irq(dev, irq,
					NULL,
					&ad7606_interrupt,
					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
					name, indio_dev);
677
	if (ret)
678
		return ret;
679

680
	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
681
					      &iio_pollfunc_store_time,
682
					      &ad7606_trigger_handler,
683
					      &ad7606_buffer_ops);
684
	if (ret)
685
		return ret;
686

687
	return devm_iio_device_register(dev, indio_dev);
688
}
689
EXPORT_SYMBOL_GPL(ad7606_probe);
690

691 692 693
#ifdef CONFIG_PM_SLEEP

static int ad7606_suspend(struct device *dev)
694
{
695
	struct iio_dev *indio_dev = dev_get_drvdata(dev);
696 697
	struct ad7606_state *st = iio_priv(indio_dev);

698 699 700
	if (st->gpio_standby) {
		gpiod_set_value(st->gpio_range, 1);
		gpiod_set_value(st->gpio_standby, 0);
701
	}
702 703

	return 0;
704 705
}

706
static int ad7606_resume(struct device *dev)
707
{
708
	struct iio_dev *indio_dev = dev_get_drvdata(dev);
709 710
	struct ad7606_state *st = iio_priv(indio_dev);

711
	if (st->gpio_standby) {
712
		gpiod_set_value(st->gpio_range, st->range[0]);
713
		gpiod_set_value(st->gpio_standby, 1);
714 715
		ad7606_reset(st);
	}
716 717

	return 0;
718
}
719 720 721 722 723

SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
EXPORT_SYMBOL_GPL(ad7606_pm_ops);

#endif
724

725
MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
726 727
MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
MODULE_LICENSE("GPL v2");