ad799x.c 21.1 KB
Newer Older
1 2
/*
 * iio/adc/ad799x.c
3
 * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * based on iio/adc/max1363
 * Copyright (C) 2008-2010 Jonathan Cameron
 *
 * based on linux/drivers/i2c/chips/max123x
 * Copyright (C) 2002-2004 Stefan Eletzhofer
 *
 * based on linux/drivers/acron/char/pcf8583.c
 * Copyright (C) 2000 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * ad799x.c
 *
 * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
 * ad7998 and similar chips.
 *
 */

#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/sysfs.h>
#include <linux/i2c.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/err.h>
34
#include <linux/module.h>
35
#include <linux/bitops.h>
36

37 38 39 40
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/events.h>
#include <linux/iio/buffer.h>
41 42
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
43

44
#define AD799X_CHANNEL_SHIFT			4
45

46 47 48 49 50 51 52 53 54 55 56 57 58
/*
 * AD7991, AD7995 and AD7999 defines
 */

#define AD7991_REF_SEL				0x08
#define AD7991_FLTR				0x04
#define AD7991_BIT_TRIAL_DELAY			0x02
#define AD7991_SAMPLE_DELAY			0x01

/*
 * AD7992, AD7993, AD7994, AD7997 and AD7998 defines
 */

59 60 61 62
#define AD7998_FLTR				BIT(3)
#define AD7998_ALERT_EN				BIT(2)
#define AD7998_BUSY_ALERT			BIT(1)
#define AD7998_BUSY_ALERT_POL			BIT(0)
63 64 65 66 67 68 69 70 71 72

#define AD7998_CONV_RES_REG			0x0
#define AD7998_ALERT_STAT_REG			0x1
#define AD7998_CONF_REG				0x2
#define AD7998_CYCLE_TMR_REG			0x3

#define AD7998_DATALOW_REG(x)			((x) * 3 + 0x4)
#define AD7998_DATAHIGH_REG(x)			((x) * 3 + 0x5)
#define AD7998_HYST_REG(x)			((x) * 3 + 0x6)

73
#define AD7998_CYC_MASK				GENMASK(2, 0)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#define AD7998_CYC_DIS				0x0
#define AD7998_CYC_TCONF_32			0x1
#define AD7998_CYC_TCONF_64			0x2
#define AD7998_CYC_TCONF_128			0x3
#define AD7998_CYC_TCONF_256			0x4
#define AD7998_CYC_TCONF_512			0x5
#define AD7998_CYC_TCONF_1024			0x6
#define AD7998_CYC_TCONF_2048			0x7

#define AD7998_ALERT_STAT_CLEAR			0xFF

/*
 * AD7997 and AD7997 defines
 */

89 90
#define AD7997_8_READ_SINGLE			BIT(7)
#define AD7997_8_READ_SEQUENCE			(BIT(6) | BIT(5) | BIT(4))
91 92 93 94 95 96 97 98 99 100 101 102 103

enum {
	ad7991,
	ad7995,
	ad7999,
	ad7992,
	ad7993,
	ad7994,
	ad7997,
	ad7998
};

/**
104
 * struct ad799x_chip_config - chip specific information
105 106
 * @channel:		channel specification
 * @default_config:	device default configuration
107
 * @info:		pointer to iio_info struct
108
 */
109
struct ad799x_chip_config {
110
	const struct iio_chan_spec	channel[9];
111 112 113 114
	u16				default_config;
	const struct iio_info		*info;
};

115 116 117 118 119 120 121 122 123 124 125 126
/**
 * struct ad799x_chip_info - chip specific information
 * @num_channels:	number of channels
 * @noirq_config:	device configuration w/o IRQ
 * @irq_config:		device configuration w/IRQ
 */
struct ad799x_chip_info {
	int				num_channels;
	const struct ad799x_chip_config	noirq_config;
	const struct ad799x_chip_config	irq_config;
};

127 128
struct ad799x_state {
	struct i2c_client		*client;
129
	const struct ad799x_chip_config	*chip_config;
130 131 132 133 134 135 136 137 138
	struct regulator		*reg;
	struct regulator		*vref;
	unsigned			id;
	u16				config;

	u8				*rx_buf;
	unsigned int			transfer_size;
};

139 140 141 142 143 144 145
static int ad799x_write_config(struct ad799x_state *st, u16 val)
{
	switch (st->id) {
	case ad7997:
	case ad7998:
		return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG,
			val);
146 147 148
	case ad7992:
	case ad7993:
	case ad7994:
149 150
		return i2c_smbus_write_byte_data(st->client, AD7998_CONF_REG,
			val);
151 152 153 154
	default:
		/* Will be written when doing a conversion */
		st->config = val;
		return 0;
155 156 157 158 159 160 161 162 163
	}
}

static int ad799x_read_config(struct ad799x_state *st)
{
	switch (st->id) {
	case ad7997:
	case ad7998:
		return i2c_smbus_read_word_swapped(st->client, AD7998_CONF_REG);
164 165 166
	case ad7992:
	case ad7993:
	case ad7994:
167
		return i2c_smbus_read_byte_data(st->client, AD7998_CONF_REG);
168 169 170
	default:
		/* No readback support */
		return st->config;
171 172 173
	}
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/**
 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
 *
 * Currently there is no option in this driver to disable the saving of
 * timestamps within the ring.
 **/
static irqreturn_t ad799x_trigger_handler(int irq, void *p)
{
	struct iio_poll_func *pf = p;
	struct iio_dev *indio_dev = pf->indio_dev;
	struct ad799x_state *st = iio_priv(indio_dev);
	int b_sent;
	u8 cmd;

	switch (st->id) {
	case ad7991:
	case ad7995:
	case ad7999:
		cmd = st->config |
			(*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
		break;
	case ad7992:
	case ad7993:
	case ad7994:
		cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
			AD7998_CONV_RES_REG;
		break;
	case ad7997:
	case ad7998:
		cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
		break;
	default:
		cmd = 0;
	}

	b_sent = i2c_smbus_read_i2c_block_data(st->client,
			cmd, st->transfer_size, st->rx_buf);
	if (b_sent < 0)
		goto out;

	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
215
			iio_get_time_ns(indio_dev));
216 217 218 219 220
out:
	iio_trigger_notify_done(indio_dev->trig);

	return IRQ_HANDLED;
}
221

222
static int ad799x_update_scan_mode(struct iio_dev *indio_dev,
223
	const unsigned long *scan_mask)
224
{
225 226
	struct ad799x_state *st = iio_priv(indio_dev);

227 228 229 230 231 232 233
	kfree(st->rx_buf);
	st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
	if (!st->rx_buf)
		return -ENOMEM;

	st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;

234
	switch (st->id) {
235 236 237
	case ad7992:
	case ad7993:
	case ad7994:
238 239
	case ad7997:
	case ad7998:
240 241 242
		st->config &= ~(GENMASK(7, 0) << AD799X_CHANNEL_SHIFT);
		st->config |= (*scan_mask << AD799X_CHANNEL_SHIFT);
		return ad799x_write_config(st, st->config);
243
	default:
244
		return 0;
245
	}
246 247
}

248
static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
249
{
250
	u8 cmd;
251

252 253 254 255
	switch (st->id) {
	case ad7991:
	case ad7995:
	case ad7999:
256
		cmd = st->config | (BIT(ch) << AD799X_CHANNEL_SHIFT);
257 258 259 260
		break;
	case ad7992:
	case ad7993:
	case ad7994:
261
		cmd = BIT(ch) << AD799X_CHANNEL_SHIFT;
262 263 264 265 266 267 268
		break;
	case ad7997:
	case ad7998:
		cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
		break;
	default:
		return -EINVAL;
269 270
	}

271
	return i2c_smbus_read_word_swapped(st->client, cmd);
272 273
}

274
static int ad799x_read_raw(struct iio_dev *indio_dev,
275 276 277 278
			   struct iio_chan_spec const *chan,
			   int *val,
			   int *val2,
			   long m)
279
{
280
	int ret;
281
	struct ad799x_state *st = iio_priv(indio_dev);
282 283

	switch (m) {
284
	case IIO_CHAN_INFO_RAW:
285 286 287 288 289
		ret = iio_device_claim_direct_mode(indio_dev);
		if (ret)
			return ret;
		ret = ad799x_scan_direct(st, chan->scan_index);
		iio_device_release_direct_mode(indio_dev);
290 291

		if (ret < 0)
292
			return ret;
293
		*val = (ret >> chan->scan_type.shift) &
294
			GENMASK(chan->scan_type.realbits - 1, 0);
295
		return IIO_VAL_INT;
296
	case IIO_CHAN_INFO_SCALE:
297 298 299 300
		ret = regulator_get_voltage(st->vref);
		if (ret < 0)
			return ret;
		*val = ret / 1000;
301 302
		*val2 = chan->scan_type.realbits;
		return IIO_VAL_FRACTIONAL_LOG2;
303
	}
304
	return -EINVAL;
305
}
306 307 308 309 310 311 312 313 314
static const unsigned int ad7998_frequencies[] = {
	[AD7998_CYC_DIS]	= 0,
	[AD7998_CYC_TCONF_32]	= 15625,
	[AD7998_CYC_TCONF_64]	= 7812,
	[AD7998_CYC_TCONF_128]	= 3906,
	[AD7998_CYC_TCONF_512]	= 976,
	[AD7998_CYC_TCONF_1024]	= 488,
	[AD7998_CYC_TCONF_2048]	= 244,
};
315

316 317 318 319
static ssize_t ad799x_read_frequency(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
320
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
321
	struct ad799x_state *st = iio_priv(indio_dev);
322

323 324
	int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
	if (ret < 0)
325 326
		return ret;

327
	return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]);
328 329 330 331 332 333 334
}

static ssize_t ad799x_write_frequency(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf,
					 size_t len)
{
335
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
336
	struct ad799x_state *st = iio_priv(indio_dev);
337 338

	long val;
339
	int ret, i;
340

341
	ret = kstrtol(buf, 10, &val);
342 343 344
	if (ret)
		return ret;

345
	mutex_lock(&indio_dev->mlock);
346 347
	ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
	if (ret < 0)
348 349
		goto error_ret_mutex;
	/* Wipe the bits clean */
350
	ret &= ~AD7998_CYC_MASK;
351

352 353 354 355
	for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
		if (val == ad7998_frequencies[i])
			break;
	if (i == ARRAY_SIZE(ad7998_frequencies)) {
356 357 358
		ret = -EINVAL;
		goto error_ret_mutex;
	}
359 360 361 362 363 364

	ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG,
		ret | i);
	if (ret < 0)
		goto error_ret_mutex;
	ret = len;
365 366

error_ret_mutex:
367
	mutex_unlock(&indio_dev->mlock);
368

369
	return ret;
370 371
}

372
static int ad799x_read_event_config(struct iio_dev *indio_dev,
373 374 375
				    const struct iio_chan_spec *chan,
				    enum iio_event_type type,
				    enum iio_event_direction dir)
376
{
377 378 379 380 381 382 383 384 385
	struct ad799x_state *st = iio_priv(indio_dev);

	if (!(st->config & AD7998_ALERT_EN))
		return 0;

	if ((st->config >> AD799X_CHANNEL_SHIFT) & BIT(chan->scan_index))
		return 1;

	return 0;
386 387
}

388 389 390 391 392 393 394 395 396
static int ad799x_write_event_config(struct iio_dev *indio_dev,
				     const struct iio_chan_spec *chan,
				     enum iio_event_type type,
				     enum iio_event_direction dir,
				     int state)
{
	struct ad799x_state *st = iio_priv(indio_dev);
	int ret;

397 398 399
	ret = iio_device_claim_direct_mode(indio_dev);
	if (ret)
		return ret;
400 401 402 403 404 405 406 407 408 409 410 411

	if (state)
		st->config |= BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT;
	else
		st->config &= ~(BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT);

	if (st->config >> AD799X_CHANNEL_SHIFT)
		st->config |= AD7998_ALERT_EN;
	else
		st->config &= ~AD7998_ALERT_EN;

	ret = ad799x_write_config(st, st->config);
412
	iio_device_release_direct_mode(indio_dev);
413 414 415
	return ret;
}

416 417 418
static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
					 enum iio_event_direction dir,
					 enum iio_event_info info)
419
{
420 421 422 423 424 425 426 427 428 429 430 431 432
	switch (info) {
	case IIO_EV_INFO_VALUE:
		if (dir == IIO_EV_DIR_FALLING)
			return AD7998_DATALOW_REG(chan->channel);
		else
			return AD7998_DATAHIGH_REG(chan->channel);
	case IIO_EV_INFO_HYSTERESIS:
		return AD7998_HYST_REG(chan->channel);
	default:
		return -EINVAL;
	}

	return 0;
433
}
434 435

static int ad799x_write_event_value(struct iio_dev *indio_dev,
436 437 438 439 440
				    const struct iio_chan_spec *chan,
				    enum iio_event_type type,
				    enum iio_event_direction dir,
				    enum iio_event_info info,
				    int val, int val2)
441 442 443 444
{
	int ret;
	struct ad799x_state *st = iio_priv(indio_dev);

445
	if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0))
446 447
		return -EINVAL;

448
	mutex_lock(&indio_dev->mlock);
449 450
	ret = i2c_smbus_write_word_swapped(st->client,
		ad799x_threshold_reg(chan, dir, info),
451
		val << chan->scan_type.shift);
452 453 454 455 456 457
	mutex_unlock(&indio_dev->mlock);

	return ret;
}

static int ad799x_read_event_value(struct iio_dev *indio_dev,
458 459 460 461 462
				    const struct iio_chan_spec *chan,
				    enum iio_event_type type,
				    enum iio_event_direction dir,
				    enum iio_event_info info,
				    int *val, int *val2)
463 464 465 466 467
{
	int ret;
	struct ad799x_state *st = iio_priv(indio_dev);

	mutex_lock(&indio_dev->mlock);
468 469
	ret = i2c_smbus_read_word_swapped(st->client,
		ad799x_threshold_reg(chan, dir, info));
470 471 472
	mutex_unlock(&indio_dev->mlock);
	if (ret < 0)
		return ret;
473
	*val = (ret >> chan->scan_type.shift) &
474
		GENMASK(chan->scan_type.realbits - 1, 0);
475

476
	return IIO_VAL_INT;
477 478
}

479
static irqreturn_t ad799x_event_handler(int irq, void *private)
480
{
481
	struct iio_dev *indio_dev = private;
482
	struct ad799x_state *st = iio_priv(private);
483
	int i, ret;
484

485 486
	ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG);
	if (ret <= 0)
487
		goto done;
488

489 490
	if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG,
		AD7998_ALERT_STAT_CLEAR) < 0)
491
		goto done;
492 493

	for (i = 0; i < 8; i++) {
494
		if (ret & BIT(i))
495
			iio_push_event(indio_dev,
496
				       i & 0x1 ?
497
				       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
498 499 500
							    (i >> 1),
							    IIO_EV_TYPE_THRESH,
							    IIO_EV_DIR_RISING) :
501
				       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
502 503 504
							    (i >> 1),
							    IIO_EV_TYPE_THRESH,
							    IIO_EV_DIR_FALLING),
505
				       iio_get_time_ns(indio_dev));
506 507
	}

508
done:
509
	return IRQ_HANDLED;
510 511 512 513 514 515 516
}

static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
			      ad799x_read_frequency,
			      ad799x_write_frequency);
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");

517
static struct attribute *ad799x_event_attributes[] = {
518 519 520 521 522
	&iio_dev_attr_sampling_frequency.dev_attr.attr,
	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
	NULL,
};

523 524
static struct attribute_group ad799x_event_attrs_group = {
	.attrs = ad799x_event_attributes,
525 526
};

527 528 529
static const struct iio_info ad7991_info = {
	.read_raw = &ad799x_read_raw,
	.driver_module = THIS_MODULE,
530
	.update_scan_mode = ad799x_update_scan_mode,
531 532
};

533 534 535
static const struct iio_info ad7993_4_7_8_noirq_info = {
	.read_raw = &ad799x_read_raw,
	.driver_module = THIS_MODULE,
536
	.update_scan_mode = ad799x_update_scan_mode,
537 538 539
};

static const struct iio_info ad7993_4_7_8_irq_info = {
540
	.read_raw = &ad799x_read_raw,
541
	.event_attrs = &ad799x_event_attrs_group,
542
	.read_event_config = &ad799x_read_event_config,
543
	.write_event_config = &ad799x_write_event_config,
544 545
	.read_event_value = &ad799x_read_event_value,
	.write_event_value = &ad799x_write_event_value,
546
	.driver_module = THIS_MODULE,
547
	.update_scan_mode = ad799x_update_scan_mode,
548 549
};

550 551 552 553 554 555 556 557 558
static const struct iio_event_spec ad799x_events[] = {
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_RISING,
		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
			BIT(IIO_EV_INFO_ENABLE),
	}, {
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_FALLING,
559
		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
560
			BIT(IIO_EV_INFO_ENABLE),
561 562 563 564
	}, {
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_EITHER,
		.mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
565 566
	},
};
567

568
#define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
569 570 571 572
	.type = IIO_VOLTAGE, \
	.indexed = 1, \
	.channel = (_index), \
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
573
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
574
	.scan_index = (_index), \
575 576 577 578 579 580 581
	.scan_type = { \
		.sign = 'u', \
		.realbits = (_realbits), \
		.storagebits = 16, \
		.shift = 12 - (_realbits), \
		.endianness = IIO_BE, \
	}, \
582 583
	.event_spec = _ev_spec, \
	.num_event_specs = _num_ev_spec, \
584 585
}

586 587 588 589 590 591 592
#define AD799X_CHANNEL(_index, _realbits) \
	_AD799X_CHANNEL(_index, _realbits, NULL, 0)

#define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
	_AD799X_CHANNEL(_index, _realbits, ad799x_events, \
		ARRAY_SIZE(ad799x_events))

593 594
static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
	[ad7991] = {
595
		.num_channels = 5,
596 597 598 599 600 601 602 603 604 605
		.noirq_config = {
			.channel = {
				AD799X_CHANNEL(0, 12),
				AD799X_CHANNEL(1, 12),
				AD799X_CHANNEL(2, 12),
				AD799X_CHANNEL(3, 12),
				IIO_CHAN_SOFT_TIMESTAMP(4),
			},
			.info = &ad7991_info,
		},
606 607
	},
	[ad7995] = {
608
		.num_channels = 5,
609 610 611 612 613 614 615 616 617 618
		.noirq_config = {
			.channel = {
				AD799X_CHANNEL(0, 10),
				AD799X_CHANNEL(1, 10),
				AD799X_CHANNEL(2, 10),
				AD799X_CHANNEL(3, 10),
				IIO_CHAN_SOFT_TIMESTAMP(4),
			},
			.info = &ad7991_info,
		},
619 620
	},
	[ad7999] = {
621
		.num_channels = 5,
622 623 624 625 626 627 628 629 630 631
		.noirq_config = {
			.channel = {
				AD799X_CHANNEL(0, 8),
				AD799X_CHANNEL(1, 8),
				AD799X_CHANNEL(2, 8),
				AD799X_CHANNEL(3, 8),
				IIO_CHAN_SOFT_TIMESTAMP(4),
			},
			.info = &ad7991_info,
		},
632 633
	},
	[ad7992] = {
634
		.num_channels = 3,
635 636 637 638 639 640 641 642 643 644 645 646 647 648
		.noirq_config = {
			.channel = {
				AD799X_CHANNEL(0, 12),
				AD799X_CHANNEL(1, 12),
				IIO_CHAN_SOFT_TIMESTAMP(3),
			},
			.info = &ad7993_4_7_8_noirq_info,
		},
		.irq_config = {
			.channel = {
				AD799X_CHANNEL_WITH_EVENTS(0, 12),
				AD799X_CHANNEL_WITH_EVENTS(1, 12),
				IIO_CHAN_SOFT_TIMESTAMP(3),
			},
649
			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
650 651
			.info = &ad7993_4_7_8_irq_info,
		},
652 653
	},
	[ad7993] = {
654
		.num_channels = 5,
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
		.noirq_config = {
			.channel = {
				AD799X_CHANNEL(0, 10),
				AD799X_CHANNEL(1, 10),
				AD799X_CHANNEL(2, 10),
				AD799X_CHANNEL(3, 10),
				IIO_CHAN_SOFT_TIMESTAMP(4),
			},
			.info = &ad7993_4_7_8_noirq_info,
		},
		.irq_config = {
			.channel = {
				AD799X_CHANNEL_WITH_EVENTS(0, 10),
				AD799X_CHANNEL_WITH_EVENTS(1, 10),
				AD799X_CHANNEL_WITH_EVENTS(2, 10),
				AD799X_CHANNEL_WITH_EVENTS(3, 10),
				IIO_CHAN_SOFT_TIMESTAMP(4),
			},
673
			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
674 675
			.info = &ad7993_4_7_8_irq_info,
		},
676 677
	},
	[ad7994] = {
678
		.num_channels = 5,
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
		.noirq_config = {
			.channel = {
				AD799X_CHANNEL(0, 12),
				AD799X_CHANNEL(1, 12),
				AD799X_CHANNEL(2, 12),
				AD799X_CHANNEL(3, 12),
				IIO_CHAN_SOFT_TIMESTAMP(4),
			},
			.info = &ad7993_4_7_8_noirq_info,
		},
		.irq_config = {
			.channel = {
				AD799X_CHANNEL_WITH_EVENTS(0, 12),
				AD799X_CHANNEL_WITH_EVENTS(1, 12),
				AD799X_CHANNEL_WITH_EVENTS(2, 12),
				AD799X_CHANNEL_WITH_EVENTS(3, 12),
				IIO_CHAN_SOFT_TIMESTAMP(4),
			},
697
			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
698 699
			.info = &ad7993_4_7_8_irq_info,
		},
700 701
	},
	[ad7997] = {
702
		.num_channels = 9,
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
		.noirq_config = {
			.channel = {
				AD799X_CHANNEL(0, 10),
				AD799X_CHANNEL(1, 10),
				AD799X_CHANNEL(2, 10),
				AD799X_CHANNEL(3, 10),
				AD799X_CHANNEL(4, 10),
				AD799X_CHANNEL(5, 10),
				AD799X_CHANNEL(6, 10),
				AD799X_CHANNEL(7, 10),
				IIO_CHAN_SOFT_TIMESTAMP(8),
			},
			.info = &ad7993_4_7_8_noirq_info,
		},
		.irq_config = {
			.channel = {
				AD799X_CHANNEL_WITH_EVENTS(0, 10),
				AD799X_CHANNEL_WITH_EVENTS(1, 10),
				AD799X_CHANNEL_WITH_EVENTS(2, 10),
				AD799X_CHANNEL_WITH_EVENTS(3, 10),
				AD799X_CHANNEL(4, 10),
				AD799X_CHANNEL(5, 10),
				AD799X_CHANNEL(6, 10),
				AD799X_CHANNEL(7, 10),
				IIO_CHAN_SOFT_TIMESTAMP(8),
			},
729
			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
730 731
			.info = &ad7993_4_7_8_irq_info,
		},
732 733
	},
	[ad7998] = {
734
		.num_channels = 9,
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
		.noirq_config = {
			.channel = {
				AD799X_CHANNEL(0, 12),
				AD799X_CHANNEL(1, 12),
				AD799X_CHANNEL(2, 12),
				AD799X_CHANNEL(3, 12),
				AD799X_CHANNEL(4, 12),
				AD799X_CHANNEL(5, 12),
				AD799X_CHANNEL(6, 12),
				AD799X_CHANNEL(7, 12),
				IIO_CHAN_SOFT_TIMESTAMP(8),
			},
			.info = &ad7993_4_7_8_noirq_info,
		},
		.irq_config = {
			.channel = {
				AD799X_CHANNEL_WITH_EVENTS(0, 12),
				AD799X_CHANNEL_WITH_EVENTS(1, 12),
				AD799X_CHANNEL_WITH_EVENTS(2, 12),
				AD799X_CHANNEL_WITH_EVENTS(3, 12),
				AD799X_CHANNEL(4, 12),
				AD799X_CHANNEL(5, 12),
				AD799X_CHANNEL(6, 12),
				AD799X_CHANNEL(7, 12),
				IIO_CHAN_SOFT_TIMESTAMP(8),
			},
761
			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
762 763
			.info = &ad7993_4_7_8_irq_info,
		},
764 765 766
	},
};

767
static int ad799x_probe(struct i2c_client *client,
768 769
				   const struct i2c_device_id *id)
{
770
	int ret;
771
	struct ad799x_state *st;
772
	struct iio_dev *indio_dev;
773 774
	const struct ad799x_chip_info *chip_info =
		&ad799x_chip_info_tbl[id->driver_data];
775

776
	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
777 778
	if (indio_dev == NULL)
		return -ENOMEM;
779

780
	st = iio_priv(indio_dev);
781
	/* this is only used for device removal purposes */
782
	i2c_set_clientdata(client, indio_dev);
783 784

	st->id = id->driver_data;
785 786 787 788
	if (client->irq > 0 && chip_info->irq_config.info)
		st->chip_config = &chip_info->irq_config;
	else
		st->chip_config = &chip_info->noirq_config;
789 790 791

	/* TODO: Add pdata options for filtering and bit delay */

792
	st->reg = devm_regulator_get(&client->dev, "vcc");
793 794 795 796 797 798 799 800 801
	if (IS_ERR(st->reg))
		return PTR_ERR(st->reg);
	ret = regulator_enable(st->reg);
	if (ret)
		return ret;
	st->vref = devm_regulator_get(&client->dev, "vref");
	if (IS_ERR(st->vref)) {
		ret = PTR_ERR(st->vref);
		goto error_disable_reg;
802
	}
803 804 805 806
	ret = regulator_enable(st->vref);
	if (ret)
		goto error_disable_reg;

807 808
	st->client = client;

809
	indio_dev->dev.parent = &client->dev;
810
	indio_dev->dev.of_node = client->dev.of_node;
811
	indio_dev->name = id->name;
812
	indio_dev->info = st->chip_config->info;
813

814
	indio_dev->modes = INDIO_DIRECT_MODE;
815 816
	indio_dev->channels = st->chip_config->channel;
	indio_dev->num_channels = chip_info->num_channels;
817

818 819 820 821 822 823 824 825
	ret = ad799x_write_config(st, st->chip_config->default_config);
	if (ret < 0)
		goto error_disable_reg;
	ret = ad799x_read_config(st);
	if (ret < 0)
		goto error_disable_reg;
	st->config = ret;

826 827
	ret = iio_triggered_buffer_setup(indio_dev, NULL,
		&ad799x_trigger_handler, NULL);
828
	if (ret)
829
		goto error_disable_vref;
830

831
	if (client->irq > 0) {
832 833 834 835 836 837 838 839
		ret = devm_request_threaded_irq(&client->dev,
						client->irq,
						NULL,
						ad799x_event_handler,
						IRQF_TRIGGER_FALLING |
						IRQF_ONESHOT,
						client->name,
						indio_dev);
840 841 842
		if (ret)
			goto error_cleanup_ring;
	}
843 844
	ret = iio_device_register(indio_dev);
	if (ret)
845
		goto error_cleanup_ring;
846 847

	return 0;
848

849
error_cleanup_ring:
850
	iio_triggered_buffer_cleanup(indio_dev);
851 852
error_disable_vref:
	regulator_disable(st->vref);
853
error_disable_reg:
854
	regulator_disable(st->reg);
855

856 857 858
	return ret;
}

859
static int ad799x_remove(struct i2c_client *client)
860
{
861 862
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct ad799x_state *st = iio_priv(indio_dev);
863

864
	iio_device_unregister(indio_dev);
865

866
	iio_triggered_buffer_cleanup(indio_dev);
867 868
	regulator_disable(st->vref);
	regulator_disable(st->reg);
869
	kfree(st->rx_buf);
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

	return 0;
}

static const struct i2c_device_id ad799x_id[] = {
	{ "ad7991", ad7991 },
	{ "ad7995", ad7995 },
	{ "ad7999", ad7999 },
	{ "ad7992", ad7992 },
	{ "ad7993", ad7993 },
	{ "ad7994", ad7994 },
	{ "ad7997", ad7997 },
	{ "ad7998", ad7998 },
	{}
};

MODULE_DEVICE_TABLE(i2c, ad799x_id);

static struct i2c_driver ad799x_driver = {
	.driver = {
		.name = "ad799x",
	},
	.probe = ad799x_probe,
893
	.remove = ad799x_remove,
894 895
	.id_table = ad799x_id,
};
896
module_i2c_driver(ad799x_driver);
897 898 899 900

MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
MODULE_DESCRIPTION("Analog Devices AD799x ADC");
MODULE_LICENSE("GPL v2");