Linux Audio

Check our new training course

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