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