xref: /linux/drivers/iio/adc/ad799x.c (revision 23c996fc2bc1978a02c64eddb90b4ab5d309c8df)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * iio/adc/ad799x.c
4  * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc.
5  *
6  * based on iio/adc/max1363
7  * Copyright (C) 2008-2010 Jonathan Cameron
8  *
9  * based on linux/drivers/i2c/chips/max123x
10  * Copyright (C) 2002-2004 Stefan Eletzhofer
11  *
12  * based on linux/drivers/acron/char/pcf8583.c
13  * Copyright (C) 2000 Russell King
14  *
15  * ad799x.c
16  *
17  * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
18  * ad7998 and similar chips.
19  */
20 
21 #include <linux/interrupt.h>
22 #include <linux/device.h>
23 #include <linux/kernel.h>
24 #include <linux/sysfs.h>
25 #include <linux/i2c.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/err.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/bitops.h>
33 
34 #include <linux/iio/iio.h>
35 #include <linux/iio/sysfs.h>
36 #include <linux/iio/events.h>
37 #include <linux/iio/buffer.h>
38 #include <linux/iio/trigger_consumer.h>
39 #include <linux/iio/triggered_buffer.h>
40 
41 #define AD799X_CHANNEL_SHIFT			4
42 
43 /*
44  * AD7991, AD7995 and AD7999 defines
45  */
46 
47 #define AD7991_REF_SEL				0x08
48 #define AD7991_FLTR				0x04
49 #define AD7991_BIT_TRIAL_DELAY			0x02
50 #define AD7991_SAMPLE_DELAY			0x01
51 
52 /*
53  * AD7992, AD7993, AD7994, AD7997 and AD7998 defines
54  */
55 
56 #define AD7998_FLTR				BIT(3)
57 #define AD7998_ALERT_EN				BIT(2)
58 #define AD7998_BUSY_ALERT			BIT(1)
59 #define AD7998_BUSY_ALERT_POL			BIT(0)
60 
61 #define AD7998_CONV_RES_REG			0x0
62 #define AD7998_ALERT_STAT_REG			0x1
63 #define AD7998_CONF_REG				0x2
64 #define AD7998_CYCLE_TMR_REG			0x3
65 
66 #define AD7998_DATALOW_REG(x)			((x) * 3 + 0x4)
67 #define AD7998_DATAHIGH_REG(x)			((x) * 3 + 0x5)
68 #define AD7998_HYST_REG(x)			((x) * 3 + 0x6)
69 
70 #define AD7998_CYC_MASK				GENMASK(2, 0)
71 #define AD7998_CYC_DIS				0x0
72 #define AD7998_CYC_TCONF_32			0x1
73 #define AD7998_CYC_TCONF_64			0x2
74 #define AD7998_CYC_TCONF_128			0x3
75 #define AD7998_CYC_TCONF_256			0x4
76 #define AD7998_CYC_TCONF_512			0x5
77 #define AD7998_CYC_TCONF_1024			0x6
78 #define AD7998_CYC_TCONF_2048			0x7
79 
80 #define AD7998_ALERT_STAT_CLEAR			0xFF
81 
82 /*
83  * AD7997 and AD7997 defines
84  */
85 
86 #define AD7997_8_READ_SINGLE			BIT(7)
87 #define AD7997_8_READ_SEQUENCE			(BIT(6) | BIT(5) | BIT(4))
88 
89 enum {
90 	ad7991,
91 	ad7995,
92 	ad7999,
93 	ad7992,
94 	ad7993,
95 	ad7994,
96 	ad7997,
97 	ad7998
98 };
99 
100 /**
101  * struct ad799x_chip_config - chip specific information
102  * @channel:		channel specification
103  * @default_config:	device default configuration
104  * @info:		pointer to iio_info struct
105  */
106 struct ad799x_chip_config {
107 	const struct iio_chan_spec	channel[9];
108 	u16				default_config;
109 	const struct iio_info		*info;
110 };
111 
112 /**
113  * struct ad799x_chip_info - chip specific information
114  * @num_channels:	number of channels
115  * @noirq_config:	device configuration w/o IRQ
116  * @irq_config:		device configuration w/IRQ
117  */
118 struct ad799x_chip_info {
119 	int				num_channels;
120 	const struct ad799x_chip_config	noirq_config;
121 	const struct ad799x_chip_config	irq_config;
122 };
123 
124 struct ad799x_state {
125 	struct i2c_client		*client;
126 	const struct ad799x_chip_config	*chip_config;
127 	struct regulator		*reg;
128 	struct regulator		*vref;
129 	/* lock to protect against multiple access to the device */
130 	struct mutex			lock;
131 	unsigned int			id;
132 	u16				config;
133 
134 	u8				*rx_buf;
135 	unsigned int			transfer_size;
136 };
137 
138 static int ad799x_write_config(struct ad799x_state *st, u16 val)
139 {
140 	switch (st->id) {
141 	case ad7997:
142 	case ad7998:
143 		return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG,
144 			val);
145 	case ad7992:
146 	case ad7993:
147 	case ad7994:
148 		return i2c_smbus_write_byte_data(st->client, AD7998_CONF_REG,
149 			val);
150 	default:
151 		/* Will be written when doing a conversion */
152 		st->config = val;
153 		return 0;
154 	}
155 }
156 
157 static int ad799x_read_config(struct ad799x_state *st)
158 {
159 	switch (st->id) {
160 	case ad7997:
161 	case ad7998:
162 		return i2c_smbus_read_word_swapped(st->client, AD7998_CONF_REG);
163 	case ad7992:
164 	case ad7993:
165 	case ad7994:
166 		return i2c_smbus_read_byte_data(st->client, AD7998_CONF_REG);
167 	default:
168 		/* No readback support */
169 		return st->config;
170 	}
171 }
172 
173 static int ad799x_update_config(struct ad799x_state *st, u16 config)
174 {
175 	int ret;
176 
177 	ret = ad799x_write_config(st, config);
178 	if (ret < 0)
179 		return ret;
180 	ret = ad799x_read_config(st);
181 	if (ret < 0)
182 		return ret;
183 	st->config = ret;
184 
185 	return 0;
186 }
187 
188 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
189 {
190 	struct iio_poll_func *pf = p;
191 	struct iio_dev *indio_dev = pf->indio_dev;
192 	struct ad799x_state *st = iio_priv(indio_dev);
193 	int b_sent;
194 	u8 cmd;
195 
196 	switch (st->id) {
197 	case ad7991:
198 	case ad7995:
199 	case ad7999:
200 		cmd = st->config |
201 			(*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
202 		break;
203 	case ad7992:
204 	case ad7993:
205 	case ad7994:
206 		cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
207 			AD7998_CONV_RES_REG;
208 		break;
209 	case ad7997:
210 	case ad7998:
211 		cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
212 		break;
213 	default:
214 		cmd = 0;
215 	}
216 
217 	b_sent = i2c_smbus_read_i2c_block_data(st->client,
218 			cmd, st->transfer_size, st->rx_buf);
219 	if (b_sent < 0)
220 		goto out;
221 
222 	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
223 			iio_get_time_ns(indio_dev));
224 out:
225 	iio_trigger_notify_done(indio_dev->trig);
226 
227 	return IRQ_HANDLED;
228 }
229 
230 static int ad799x_update_scan_mode(struct iio_dev *indio_dev,
231 	const unsigned long *scan_mask)
232 {
233 	struct ad799x_state *st = iio_priv(indio_dev);
234 
235 	kfree(st->rx_buf);
236 	st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
237 	if (!st->rx_buf)
238 		return -ENOMEM;
239 
240 	st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
241 
242 	switch (st->id) {
243 	case ad7992:
244 	case ad7993:
245 	case ad7994:
246 	case ad7997:
247 	case ad7998:
248 		st->config &= ~(GENMASK(7, 0) << AD799X_CHANNEL_SHIFT);
249 		st->config |= (*scan_mask << AD799X_CHANNEL_SHIFT);
250 		return ad799x_write_config(st, st->config);
251 	default:
252 		return 0;
253 	}
254 }
255 
256 static int ad799x_scan_direct(struct ad799x_state *st, unsigned int ch)
257 {
258 	u8 cmd;
259 
260 	switch (st->id) {
261 	case ad7991:
262 	case ad7995:
263 	case ad7999:
264 		cmd = st->config | (BIT(ch) << AD799X_CHANNEL_SHIFT);
265 		break;
266 	case ad7992:
267 	case ad7993:
268 	case ad7994:
269 		cmd = BIT(ch) << AD799X_CHANNEL_SHIFT;
270 		break;
271 	case ad7997:
272 	case ad7998:
273 		cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
274 		break;
275 	default:
276 		return -EINVAL;
277 	}
278 
279 	return i2c_smbus_read_word_swapped(st->client, cmd);
280 }
281 
282 static int ad799x_read_raw(struct iio_dev *indio_dev,
283 			   struct iio_chan_spec const *chan,
284 			   int *val,
285 			   int *val2,
286 			   long m)
287 {
288 	int ret;
289 	struct ad799x_state *st = iio_priv(indio_dev);
290 
291 	switch (m) {
292 	case IIO_CHAN_INFO_RAW:
293 		ret = iio_device_claim_direct_mode(indio_dev);
294 		if (ret)
295 			return ret;
296 		mutex_lock(&st->lock);
297 		ret = ad799x_scan_direct(st, chan->scan_index);
298 		mutex_unlock(&st->lock);
299 		iio_device_release_direct_mode(indio_dev);
300 
301 		if (ret < 0)
302 			return ret;
303 		*val = (ret >> chan->scan_type.shift) &
304 			GENMASK(chan->scan_type.realbits - 1, 0);
305 		return IIO_VAL_INT;
306 	case IIO_CHAN_INFO_SCALE:
307 		if (st->vref)
308 			ret = regulator_get_voltage(st->vref);
309 		else
310 			ret = regulator_get_voltage(st->reg);
311 
312 		if (ret < 0)
313 			return ret;
314 		*val = ret / 1000;
315 		*val2 = chan->scan_type.realbits;
316 		return IIO_VAL_FRACTIONAL_LOG2;
317 	}
318 	return -EINVAL;
319 }
320 static const unsigned int ad7998_frequencies[] = {
321 	[AD7998_CYC_DIS]	= 0,
322 	[AD7998_CYC_TCONF_32]	= 15625,
323 	[AD7998_CYC_TCONF_64]	= 7812,
324 	[AD7998_CYC_TCONF_128]	= 3906,
325 	[AD7998_CYC_TCONF_512]	= 976,
326 	[AD7998_CYC_TCONF_1024]	= 488,
327 	[AD7998_CYC_TCONF_2048]	= 244,
328 };
329 
330 static ssize_t ad799x_read_frequency(struct device *dev,
331 					struct device_attribute *attr,
332 					char *buf)
333 {
334 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
335 	struct ad799x_state *st = iio_priv(indio_dev);
336 
337 	int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
338 
339 	if (ret < 0)
340 		return ret;
341 
342 	return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]);
343 }
344 
345 static ssize_t ad799x_write_frequency(struct device *dev,
346 					 struct device_attribute *attr,
347 					 const char *buf,
348 					 size_t len)
349 {
350 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
351 	struct ad799x_state *st = iio_priv(indio_dev);
352 
353 	long val;
354 	int ret, i;
355 
356 	ret = kstrtol(buf, 10, &val);
357 	if (ret)
358 		return ret;
359 
360 	mutex_lock(&st->lock);
361 
362 	ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
363 	if (ret < 0)
364 		goto error_ret_mutex;
365 	/* Wipe the bits clean */
366 	ret &= ~AD7998_CYC_MASK;
367 
368 	for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
369 		if (val == ad7998_frequencies[i])
370 			break;
371 	if (i == ARRAY_SIZE(ad7998_frequencies)) {
372 		ret = -EINVAL;
373 		goto error_ret_mutex;
374 	}
375 
376 	ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG,
377 		ret | i);
378 	if (ret < 0)
379 		goto error_ret_mutex;
380 	ret = len;
381 
382 error_ret_mutex:
383 	mutex_unlock(&st->lock);
384 
385 	return ret;
386 }
387 
388 static int ad799x_read_event_config(struct iio_dev *indio_dev,
389 				    const struct iio_chan_spec *chan,
390 				    enum iio_event_type type,
391 				    enum iio_event_direction dir)
392 {
393 	struct ad799x_state *st = iio_priv(indio_dev);
394 
395 	if (!(st->config & AD7998_ALERT_EN))
396 		return 0;
397 
398 	if ((st->config >> AD799X_CHANNEL_SHIFT) & BIT(chan->scan_index))
399 		return 1;
400 
401 	return 0;
402 }
403 
404 static int ad799x_write_event_config(struct iio_dev *indio_dev,
405 				     const struct iio_chan_spec *chan,
406 				     enum iio_event_type type,
407 				     enum iio_event_direction dir,
408 				     int state)
409 {
410 	struct ad799x_state *st = iio_priv(indio_dev);
411 	int ret;
412 
413 	ret = iio_device_claim_direct_mode(indio_dev);
414 	if (ret)
415 		return ret;
416 
417 	mutex_lock(&st->lock);
418 
419 	if (state)
420 		st->config |= BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT;
421 	else
422 		st->config &= ~(BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT);
423 
424 	if (st->config >> AD799X_CHANNEL_SHIFT)
425 		st->config |= AD7998_ALERT_EN;
426 	else
427 		st->config &= ~AD7998_ALERT_EN;
428 
429 	ret = ad799x_write_config(st, st->config);
430 	mutex_unlock(&st->lock);
431 	iio_device_release_direct_mode(indio_dev);
432 	return ret;
433 }
434 
435 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
436 					 enum iio_event_direction dir,
437 					 enum iio_event_info info)
438 {
439 	switch (info) {
440 	case IIO_EV_INFO_VALUE:
441 		if (dir == IIO_EV_DIR_FALLING)
442 			return AD7998_DATALOW_REG(chan->channel);
443 		else
444 			return AD7998_DATAHIGH_REG(chan->channel);
445 	case IIO_EV_INFO_HYSTERESIS:
446 		return AD7998_HYST_REG(chan->channel);
447 	default:
448 		return -EINVAL;
449 	}
450 
451 	return 0;
452 }
453 
454 static int ad799x_write_event_value(struct iio_dev *indio_dev,
455 				    const struct iio_chan_spec *chan,
456 				    enum iio_event_type type,
457 				    enum iio_event_direction dir,
458 				    enum iio_event_info info,
459 				    int val, int val2)
460 {
461 	int ret;
462 	struct ad799x_state *st = iio_priv(indio_dev);
463 
464 	if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0))
465 		return -EINVAL;
466 
467 	ret = i2c_smbus_write_word_swapped(st->client,
468 		ad799x_threshold_reg(chan, dir, info),
469 		val << chan->scan_type.shift);
470 
471 	return ret;
472 }
473 
474 static int ad799x_read_event_value(struct iio_dev *indio_dev,
475 				    const struct iio_chan_spec *chan,
476 				    enum iio_event_type type,
477 				    enum iio_event_direction dir,
478 				    enum iio_event_info info,
479 				    int *val, int *val2)
480 {
481 	int ret;
482 	struct ad799x_state *st = iio_priv(indio_dev);
483 
484 	ret = i2c_smbus_read_word_swapped(st->client,
485 		ad799x_threshold_reg(chan, dir, info));
486 	if (ret < 0)
487 		return ret;
488 	*val = (ret >> chan->scan_type.shift) &
489 		GENMASK(chan->scan_type.realbits - 1, 0);
490 
491 	return IIO_VAL_INT;
492 }
493 
494 static irqreturn_t ad799x_event_handler(int irq, void *private)
495 {
496 	struct iio_dev *indio_dev = private;
497 	struct ad799x_state *st = iio_priv(private);
498 	int i, ret;
499 
500 	ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG);
501 	if (ret <= 0)
502 		goto done;
503 
504 	if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG,
505 		AD7998_ALERT_STAT_CLEAR) < 0)
506 		goto done;
507 
508 	for (i = 0; i < 8; i++) {
509 		if (ret & BIT(i))
510 			iio_push_event(indio_dev,
511 				       i & 0x1 ?
512 				       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
513 							    (i >> 1),
514 							    IIO_EV_TYPE_THRESH,
515 							    IIO_EV_DIR_RISING) :
516 				       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
517 							    (i >> 1),
518 							    IIO_EV_TYPE_THRESH,
519 							    IIO_EV_DIR_FALLING),
520 				       iio_get_time_ns(indio_dev));
521 	}
522 
523 done:
524 	return IRQ_HANDLED;
525 }
526 
527 static IIO_DEV_ATTR_SAMP_FREQ(0644,
528 			      ad799x_read_frequency,
529 			      ad799x_write_frequency);
530 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
531 
532 static struct attribute *ad799x_event_attributes[] = {
533 	&iio_dev_attr_sampling_frequency.dev_attr.attr,
534 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
535 	NULL,
536 };
537 
538 static const struct attribute_group ad799x_event_attrs_group = {
539 	.attrs = ad799x_event_attributes,
540 };
541 
542 static const struct iio_info ad7991_info = {
543 	.read_raw = &ad799x_read_raw,
544 	.update_scan_mode = ad799x_update_scan_mode,
545 };
546 
547 static const struct iio_info ad7993_4_7_8_noirq_info = {
548 	.read_raw = &ad799x_read_raw,
549 	.update_scan_mode = ad799x_update_scan_mode,
550 };
551 
552 static const struct iio_info ad7993_4_7_8_irq_info = {
553 	.read_raw = &ad799x_read_raw,
554 	.event_attrs = &ad799x_event_attrs_group,
555 	.read_event_config = &ad799x_read_event_config,
556 	.write_event_config = &ad799x_write_event_config,
557 	.read_event_value = &ad799x_read_event_value,
558 	.write_event_value = &ad799x_write_event_value,
559 	.update_scan_mode = ad799x_update_scan_mode,
560 };
561 
562 static const struct iio_event_spec ad799x_events[] = {
563 	{
564 		.type = IIO_EV_TYPE_THRESH,
565 		.dir = IIO_EV_DIR_RISING,
566 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
567 			BIT(IIO_EV_INFO_ENABLE),
568 	}, {
569 		.type = IIO_EV_TYPE_THRESH,
570 		.dir = IIO_EV_DIR_FALLING,
571 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
572 			BIT(IIO_EV_INFO_ENABLE),
573 	}, {
574 		.type = IIO_EV_TYPE_THRESH,
575 		.dir = IIO_EV_DIR_EITHER,
576 		.mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
577 	},
578 };
579 
580 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
581 	.type = IIO_VOLTAGE, \
582 	.indexed = 1, \
583 	.channel = (_index), \
584 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
585 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
586 	.scan_index = (_index), \
587 	.scan_type = { \
588 		.sign = 'u', \
589 		.realbits = (_realbits), \
590 		.storagebits = 16, \
591 		.shift = 12 - (_realbits), \
592 		.endianness = IIO_BE, \
593 	}, \
594 	.event_spec = _ev_spec, \
595 	.num_event_specs = _num_ev_spec, \
596 }
597 
598 #define AD799X_CHANNEL(_index, _realbits) \
599 	_AD799X_CHANNEL(_index, _realbits, NULL, 0)
600 
601 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
602 	_AD799X_CHANNEL(_index, _realbits, ad799x_events, \
603 		ARRAY_SIZE(ad799x_events))
604 
605 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
606 	[ad7991] = {
607 		.num_channels = 5,
608 		.noirq_config = {
609 			.channel = {
610 				AD799X_CHANNEL(0, 12),
611 				AD799X_CHANNEL(1, 12),
612 				AD799X_CHANNEL(2, 12),
613 				AD799X_CHANNEL(3, 12),
614 				IIO_CHAN_SOFT_TIMESTAMP(4),
615 			},
616 			.info = &ad7991_info,
617 		},
618 	},
619 	[ad7995] = {
620 		.num_channels = 5,
621 		.noirq_config = {
622 			.channel = {
623 				AD799X_CHANNEL(0, 10),
624 				AD799X_CHANNEL(1, 10),
625 				AD799X_CHANNEL(2, 10),
626 				AD799X_CHANNEL(3, 10),
627 				IIO_CHAN_SOFT_TIMESTAMP(4),
628 			},
629 			.info = &ad7991_info,
630 		},
631 	},
632 	[ad7999] = {
633 		.num_channels = 5,
634 		.noirq_config = {
635 			.channel = {
636 				AD799X_CHANNEL(0, 8),
637 				AD799X_CHANNEL(1, 8),
638 				AD799X_CHANNEL(2, 8),
639 				AD799X_CHANNEL(3, 8),
640 				IIO_CHAN_SOFT_TIMESTAMP(4),
641 			},
642 			.info = &ad7991_info,
643 		},
644 	},
645 	[ad7992] = {
646 		.num_channels = 3,
647 		.noirq_config = {
648 			.channel = {
649 				AD799X_CHANNEL(0, 12),
650 				AD799X_CHANNEL(1, 12),
651 				IIO_CHAN_SOFT_TIMESTAMP(3),
652 			},
653 			.info = &ad7993_4_7_8_noirq_info,
654 		},
655 		.irq_config = {
656 			.channel = {
657 				AD799X_CHANNEL_WITH_EVENTS(0, 12),
658 				AD799X_CHANNEL_WITH_EVENTS(1, 12),
659 				IIO_CHAN_SOFT_TIMESTAMP(3),
660 			},
661 			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
662 			.info = &ad7993_4_7_8_irq_info,
663 		},
664 	},
665 	[ad7993] = {
666 		.num_channels = 5,
667 		.noirq_config = {
668 			.channel = {
669 				AD799X_CHANNEL(0, 10),
670 				AD799X_CHANNEL(1, 10),
671 				AD799X_CHANNEL(2, 10),
672 				AD799X_CHANNEL(3, 10),
673 				IIO_CHAN_SOFT_TIMESTAMP(4),
674 			},
675 			.info = &ad7993_4_7_8_noirq_info,
676 		},
677 		.irq_config = {
678 			.channel = {
679 				AD799X_CHANNEL_WITH_EVENTS(0, 10),
680 				AD799X_CHANNEL_WITH_EVENTS(1, 10),
681 				AD799X_CHANNEL_WITH_EVENTS(2, 10),
682 				AD799X_CHANNEL_WITH_EVENTS(3, 10),
683 				IIO_CHAN_SOFT_TIMESTAMP(4),
684 			},
685 			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
686 			.info = &ad7993_4_7_8_irq_info,
687 		},
688 	},
689 	[ad7994] = {
690 		.num_channels = 5,
691 		.noirq_config = {
692 			.channel = {
693 				AD799X_CHANNEL(0, 12),
694 				AD799X_CHANNEL(1, 12),
695 				AD799X_CHANNEL(2, 12),
696 				AD799X_CHANNEL(3, 12),
697 				IIO_CHAN_SOFT_TIMESTAMP(4),
698 			},
699 			.info = &ad7993_4_7_8_noirq_info,
700 		},
701 		.irq_config = {
702 			.channel = {
703 				AD799X_CHANNEL_WITH_EVENTS(0, 12),
704 				AD799X_CHANNEL_WITH_EVENTS(1, 12),
705 				AD799X_CHANNEL_WITH_EVENTS(2, 12),
706 				AD799X_CHANNEL_WITH_EVENTS(3, 12),
707 				IIO_CHAN_SOFT_TIMESTAMP(4),
708 			},
709 			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
710 			.info = &ad7993_4_7_8_irq_info,
711 		},
712 	},
713 	[ad7997] = {
714 		.num_channels = 9,
715 		.noirq_config = {
716 			.channel = {
717 				AD799X_CHANNEL(0, 10),
718 				AD799X_CHANNEL(1, 10),
719 				AD799X_CHANNEL(2, 10),
720 				AD799X_CHANNEL(3, 10),
721 				AD799X_CHANNEL(4, 10),
722 				AD799X_CHANNEL(5, 10),
723 				AD799X_CHANNEL(6, 10),
724 				AD799X_CHANNEL(7, 10),
725 				IIO_CHAN_SOFT_TIMESTAMP(8),
726 			},
727 			.info = &ad7993_4_7_8_noirq_info,
728 		},
729 		.irq_config = {
730 			.channel = {
731 				AD799X_CHANNEL_WITH_EVENTS(0, 10),
732 				AD799X_CHANNEL_WITH_EVENTS(1, 10),
733 				AD799X_CHANNEL_WITH_EVENTS(2, 10),
734 				AD799X_CHANNEL_WITH_EVENTS(3, 10),
735 				AD799X_CHANNEL(4, 10),
736 				AD799X_CHANNEL(5, 10),
737 				AD799X_CHANNEL(6, 10),
738 				AD799X_CHANNEL(7, 10),
739 				IIO_CHAN_SOFT_TIMESTAMP(8),
740 			},
741 			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
742 			.info = &ad7993_4_7_8_irq_info,
743 		},
744 	},
745 	[ad7998] = {
746 		.num_channels = 9,
747 		.noirq_config = {
748 			.channel = {
749 				AD799X_CHANNEL(0, 12),
750 				AD799X_CHANNEL(1, 12),
751 				AD799X_CHANNEL(2, 12),
752 				AD799X_CHANNEL(3, 12),
753 				AD799X_CHANNEL(4, 12),
754 				AD799X_CHANNEL(5, 12),
755 				AD799X_CHANNEL(6, 12),
756 				AD799X_CHANNEL(7, 12),
757 				IIO_CHAN_SOFT_TIMESTAMP(8),
758 			},
759 			.info = &ad7993_4_7_8_noirq_info,
760 		},
761 		.irq_config = {
762 			.channel = {
763 				AD799X_CHANNEL_WITH_EVENTS(0, 12),
764 				AD799X_CHANNEL_WITH_EVENTS(1, 12),
765 				AD799X_CHANNEL_WITH_EVENTS(2, 12),
766 				AD799X_CHANNEL_WITH_EVENTS(3, 12),
767 				AD799X_CHANNEL(4, 12),
768 				AD799X_CHANNEL(5, 12),
769 				AD799X_CHANNEL(6, 12),
770 				AD799X_CHANNEL(7, 12),
771 				IIO_CHAN_SOFT_TIMESTAMP(8),
772 			},
773 			.default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
774 			.info = &ad7993_4_7_8_irq_info,
775 		},
776 	},
777 };
778 
779 static int ad799x_probe(struct i2c_client *client)
780 {
781 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
782 	int ret;
783 	int extra_config = 0;
784 	struct ad799x_state *st;
785 	struct iio_dev *indio_dev;
786 	const struct ad799x_chip_info *chip_info =
787 		&ad799x_chip_info_tbl[id->driver_data];
788 
789 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
790 	if (indio_dev == NULL)
791 		return -ENOMEM;
792 
793 	st = iio_priv(indio_dev);
794 	/* this is only used for device removal purposes */
795 	i2c_set_clientdata(client, indio_dev);
796 
797 	st->id = id->driver_data;
798 	if (client->irq > 0 && chip_info->irq_config.info)
799 		st->chip_config = &chip_info->irq_config;
800 	else
801 		st->chip_config = &chip_info->noirq_config;
802 
803 	/* TODO: Add pdata options for filtering and bit delay */
804 
805 	st->reg = devm_regulator_get(&client->dev, "vcc");
806 	if (IS_ERR(st->reg))
807 		return PTR_ERR(st->reg);
808 	ret = regulator_enable(st->reg);
809 	if (ret)
810 		return ret;
811 
812 	/* check if an external reference is supplied */
813 	st->vref = devm_regulator_get_optional(&client->dev, "vref");
814 
815 	if (IS_ERR(st->vref)) {
816 		if (PTR_ERR(st->vref) == -ENODEV) {
817 			st->vref = NULL;
818 			dev_info(&client->dev, "Using VCC reference voltage\n");
819 		} else {
820 			ret = PTR_ERR(st->vref);
821 			goto error_disable_reg;
822 		}
823 	}
824 
825 	if (st->vref) {
826 		/*
827 		 * Use external reference voltage if supported by hardware.
828 		 * This is optional if voltage / regulator present, use VCC otherwise.
829 		 */
830 		if ((st->id == ad7991) || (st->id == ad7995) || (st->id == ad7999)) {
831 			dev_info(&client->dev, "Using external reference voltage\n");
832 			extra_config |= AD7991_REF_SEL;
833 			ret = regulator_enable(st->vref);
834 			if (ret)
835 				goto error_disable_reg;
836 		} else {
837 			st->vref = NULL;
838 			dev_warn(&client->dev, "Supplied reference not supported\n");
839 		}
840 	}
841 
842 	st->client = client;
843 
844 	indio_dev->name = id->name;
845 	indio_dev->info = st->chip_config->info;
846 
847 	indio_dev->modes = INDIO_DIRECT_MODE;
848 	indio_dev->channels = st->chip_config->channel;
849 	indio_dev->num_channels = chip_info->num_channels;
850 
851 	ret = ad799x_update_config(st, st->chip_config->default_config | extra_config);
852 	if (ret)
853 		goto error_disable_vref;
854 
855 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
856 		&ad799x_trigger_handler, NULL);
857 	if (ret)
858 		goto error_disable_vref;
859 
860 	if (client->irq > 0) {
861 		ret = devm_request_threaded_irq(&client->dev,
862 						client->irq,
863 						NULL,
864 						ad799x_event_handler,
865 						IRQF_TRIGGER_FALLING |
866 						IRQF_ONESHOT,
867 						client->name,
868 						indio_dev);
869 		if (ret)
870 			goto error_cleanup_ring;
871 	}
872 
873 	mutex_init(&st->lock);
874 
875 	ret = iio_device_register(indio_dev);
876 	if (ret)
877 		goto error_cleanup_ring;
878 
879 	return 0;
880 
881 error_cleanup_ring:
882 	iio_triggered_buffer_cleanup(indio_dev);
883 error_disable_vref:
884 	if (st->vref)
885 		regulator_disable(st->vref);
886 error_disable_reg:
887 	regulator_disable(st->reg);
888 
889 	return ret;
890 }
891 
892 static void ad799x_remove(struct i2c_client *client)
893 {
894 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
895 	struct ad799x_state *st = iio_priv(indio_dev);
896 
897 	iio_device_unregister(indio_dev);
898 
899 	iio_triggered_buffer_cleanup(indio_dev);
900 	if (st->vref)
901 		regulator_disable(st->vref);
902 	regulator_disable(st->reg);
903 	kfree(st->rx_buf);
904 }
905 
906 static int ad799x_suspend(struct device *dev)
907 {
908 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
909 	struct ad799x_state *st = iio_priv(indio_dev);
910 
911 	if (st->vref)
912 		regulator_disable(st->vref);
913 	regulator_disable(st->reg);
914 
915 	return 0;
916 }
917 
918 static int ad799x_resume(struct device *dev)
919 {
920 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
921 	struct ad799x_state *st = iio_priv(indio_dev);
922 	int ret;
923 
924 	ret = regulator_enable(st->reg);
925 	if (ret) {
926 		dev_err(dev, "Unable to enable vcc regulator\n");
927 		return ret;
928 	}
929 
930 	if (st->vref) {
931 		ret = regulator_enable(st->vref);
932 		if (ret) {
933 			regulator_disable(st->reg);
934 			dev_err(dev, "Unable to enable vref regulator\n");
935 			return ret;
936 		}
937 	}
938 
939 	/* resync config */
940 	ret = ad799x_update_config(st, st->config);
941 	if (ret) {
942 		if (st->vref)
943 			regulator_disable(st->vref);
944 		regulator_disable(st->reg);
945 		return ret;
946 	}
947 
948 	return 0;
949 }
950 
951 static DEFINE_SIMPLE_DEV_PM_OPS(ad799x_pm_ops, ad799x_suspend, ad799x_resume);
952 
953 static const struct i2c_device_id ad799x_id[] = {
954 	{ "ad7991", ad7991 },
955 	{ "ad7995", ad7995 },
956 	{ "ad7999", ad7999 },
957 	{ "ad7992", ad7992 },
958 	{ "ad7993", ad7993 },
959 	{ "ad7994", ad7994 },
960 	{ "ad7997", ad7997 },
961 	{ "ad7998", ad7998 },
962 	{}
963 };
964 
965 MODULE_DEVICE_TABLE(i2c, ad799x_id);
966 
967 static struct i2c_driver ad799x_driver = {
968 	.driver = {
969 		.name = "ad799x",
970 		.pm = pm_sleep_ptr(&ad799x_pm_ops),
971 	},
972 	.probe = ad799x_probe,
973 	.remove = ad799x_remove,
974 	.id_table = ad799x_id,
975 };
976 module_i2c_driver(ad799x_driver);
977 
978 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
979 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
980 MODULE_LICENSE("GPL v2");
981