Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * AD7887 SPI ADC driver
  4 *
  5 * Copyright 2010-2011 Analog Devices Inc.
  6 */
  7
  8#include <linux/device.h>
  9#include <linux/kernel.h>
 10#include <linux/slab.h>
 11#include <linux/sysfs.h>
 12#include <linux/spi/spi.h>
 13#include <linux/regulator/consumer.h>
 14#include <linux/err.h>
 15#include <linux/module.h>
 16#include <linux/interrupt.h>
 17#include <linux/bitops.h>
 18
 19#include <linux/iio/iio.h>
 20#include <linux/iio/sysfs.h>
 21#include <linux/iio/buffer.h>
 22
 23#include <linux/iio/trigger_consumer.h>
 24#include <linux/iio/triggered_buffer.h>
 25
 26#include <linux/platform_data/ad7887.h>
 27
 28#define AD7887_REF_DIS		BIT(5)	/* on-chip reference disable */
 29#define AD7887_DUAL		BIT(4)	/* dual-channel mode */
 30#define AD7887_CH_AIN1		BIT(3)	/* convert on channel 1, DUAL=1 */
 31#define AD7887_CH_AIN0		0	/* convert on channel 0, DUAL=0,1 */
 32#define AD7887_PM_MODE1		0	/* CS based shutdown */
 33#define AD7887_PM_MODE2		1	/* full on */
 34#define AD7887_PM_MODE3		2	/* auto shutdown after conversion */
 35#define AD7887_PM_MODE4		3	/* standby mode */
 36
 37enum ad7887_channels {
 38	AD7887_CH0,
 39	AD7887_CH0_CH1,
 40	AD7887_CH1,
 41};
 42
 43/**
 44 * struct ad7887_chip_info - chip specifc information
 45 * @int_vref_mv:	the internal reference voltage
 46 * @channel:		channel specification
 
 
 
 47 */
 48struct ad7887_chip_info {
 49	u16				int_vref_mv;
 50	struct iio_chan_spec		channel[3];
 
 
 
 51};
 52
 53struct ad7887_state {
 54	struct spi_device		*spi;
 55	const struct ad7887_chip_info	*chip_info;
 56	struct regulator		*reg;
 57	struct spi_transfer		xfer[4];
 58	struct spi_message		msg[3];
 59	struct spi_message		*ring_msg;
 60	unsigned char			tx_cmd_buf[4];
 61
 62	/*
 63	 * DMA (thus cache coherency maintenance) requires the
 64	 * transfer buffers to live in their own cache lines.
 65	 * Buffer needs to be large enough to hold two 16 bit samples and a
 66	 * 64 bit aligned 64 bit timestamp.
 67	 */
 68	unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)]
 69		____cacheline_aligned;
 70};
 71
 72enum ad7887_supported_device_ids {
 73	ID_AD7887
 74};
 75
 76static int ad7887_ring_preenable(struct iio_dev *indio_dev)
 77{
 78	struct ad7887_state *st = iio_priv(indio_dev);
 79
 80	/* We know this is a single long so can 'cheat' */
 81	switch (*indio_dev->active_scan_mask) {
 82	case (1 << 0):
 83		st->ring_msg = &st->msg[AD7887_CH0];
 84		break;
 85	case (1 << 1):
 86		st->ring_msg = &st->msg[AD7887_CH1];
 87		/* Dummy read: push CH1 setting down to hardware */
 88		spi_sync(st->spi, st->ring_msg);
 89		break;
 90	case ((1 << 1) | (1 << 0)):
 91		st->ring_msg = &st->msg[AD7887_CH0_CH1];
 92		break;
 93	}
 94
 95	return 0;
 96}
 97
 98static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
 99{
100	struct ad7887_state *st = iio_priv(indio_dev);
101
102	/* dummy read: restore default CH0 settin */
103	return spi_sync(st->spi, &st->msg[AD7887_CH0]);
104}
105
106/**
107 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
108 *
109 * Currently there is no option in this driver to disable the saving of
110 * timestamps within the ring.
111 **/
112static irqreturn_t ad7887_trigger_handler(int irq, void *p)
113{
114	struct iio_poll_func *pf = p;
115	struct iio_dev *indio_dev = pf->indio_dev;
116	struct ad7887_state *st = iio_priv(indio_dev);
117	int b_sent;
118
119	b_sent = spi_sync(st->spi, st->ring_msg);
120	if (b_sent)
121		goto done;
122
123	iio_push_to_buffers_with_timestamp(indio_dev, st->data,
124		iio_get_time_ns(indio_dev));
125done:
126	iio_trigger_notify_done(indio_dev->trig);
127
128	return IRQ_HANDLED;
129}
130
131static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = {
132	.preenable = &ad7887_ring_preenable,
133	.postenable = &iio_triggered_buffer_postenable,
134	.predisable = &iio_triggered_buffer_predisable,
135	.postdisable = &ad7887_ring_postdisable,
136};
137
138static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
139{
140	int ret = spi_sync(st->spi, &st->msg[ch]);
141	if (ret)
142		return ret;
143
144	return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
145}
146
147static int ad7887_read_raw(struct iio_dev *indio_dev,
148			   struct iio_chan_spec const *chan,
149			   int *val,
150			   int *val2,
151			   long m)
152{
153	int ret;
154	struct ad7887_state *st = iio_priv(indio_dev);
155
156	switch (m) {
157	case IIO_CHAN_INFO_RAW:
158		ret = iio_device_claim_direct_mode(indio_dev);
159		if (ret)
160			return ret;
161		ret = ad7887_scan_direct(st, chan->address);
162		iio_device_release_direct_mode(indio_dev);
163
164		if (ret < 0)
165			return ret;
166		*val = ret >> chan->scan_type.shift;
167		*val &= GENMASK(chan->scan_type.realbits - 1, 0);
168		return IIO_VAL_INT;
169	case IIO_CHAN_INFO_SCALE:
170		if (st->reg) {
171			*val = regulator_get_voltage(st->reg);
172			if (*val < 0)
173				return *val;
174			*val /= 1000;
175		} else {
176			*val = st->chip_info->int_vref_mv;
177		}
178
179		*val2 = chan->scan_type.realbits;
180
181		return IIO_VAL_FRACTIONAL_LOG2;
182	}
183	return -EINVAL;
184}
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
187static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
188	/*
189	 * More devices added in future
190	 */
191	[ID_AD7887] = {
192		.channel[0] = {
193			.type = IIO_VOLTAGE,
194			.indexed = 1,
195			.channel = 1,
196			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
197			.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
198			.address = 1,
199			.scan_index = 1,
200			.scan_type = {
201				.sign = 'u',
202				.realbits = 12,
203				.storagebits = 16,
204				.shift = 0,
205				.endianness = IIO_BE,
206			},
207		},
208		.channel[1] = {
209			.type = IIO_VOLTAGE,
210			.indexed = 1,
211			.channel = 0,
212			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
213			.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
214			.address = 0,
215			.scan_index = 0,
216			.scan_type = {
217				.sign = 'u',
218				.realbits = 12,
219				.storagebits = 16,
220				.shift = 0,
221				.endianness = IIO_BE,
222			},
223		},
224		.channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
225		.int_vref_mv = 2500,
226	},
227};
228
229static const struct iio_info ad7887_info = {
230	.read_raw = &ad7887_read_raw,
231};
232
233static int ad7887_probe(struct spi_device *spi)
234{
235	struct ad7887_platform_data *pdata = spi->dev.platform_data;
236	struct ad7887_state *st;
237	struct iio_dev *indio_dev;
238	uint8_t mode;
239	int ret;
240
241	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
242	if (indio_dev == NULL)
243		return -ENOMEM;
244
245	st = iio_priv(indio_dev);
246
247	if (!pdata || !pdata->use_onchip_ref) {
248		st->reg = devm_regulator_get(&spi->dev, "vref");
249		if (IS_ERR(st->reg))
250			return PTR_ERR(st->reg);
251
252		ret = regulator_enable(st->reg);
253		if (ret)
254			return ret;
255	}
256
257	st->chip_info =
258		&ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
259
260	spi_set_drvdata(spi, indio_dev);
261	st->spi = spi;
262
263	/* Estabilish that the iio_dev is a child of the spi device */
264	indio_dev->dev.parent = &spi->dev;
265	indio_dev->dev.of_node = spi->dev.of_node;
266	indio_dev->name = spi_get_device_id(spi)->name;
267	indio_dev->info = &ad7887_info;
268	indio_dev->modes = INDIO_DIRECT_MODE;
269
270	/* Setup default message */
271
272	mode = AD7887_PM_MODE4;
273	if (!pdata || !pdata->use_onchip_ref)
274		mode |= AD7887_REF_DIS;
275	if (pdata && pdata->en_dual)
276		mode |= AD7887_DUAL;
277
278	st->tx_cmd_buf[0] = AD7887_CH_AIN0 | mode;
279
280	st->xfer[0].rx_buf = &st->data[0];
281	st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
282	st->xfer[0].len = 2;
283
284	spi_message_init(&st->msg[AD7887_CH0]);
285	spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
286
287	if (pdata && pdata->en_dual) {
288		st->tx_cmd_buf[2] = AD7887_CH_AIN1 | mode;
289
290		st->xfer[1].rx_buf = &st->data[0];
291		st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
292		st->xfer[1].len = 2;
293
294		st->xfer[2].rx_buf = &st->data[2];
295		st->xfer[2].tx_buf = &st->tx_cmd_buf[0];
296		st->xfer[2].len = 2;
297
298		spi_message_init(&st->msg[AD7887_CH0_CH1]);
299		spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
300		spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
301
302		st->xfer[3].rx_buf = &st->data[2];
303		st->xfer[3].tx_buf = &st->tx_cmd_buf[2];
304		st->xfer[3].len = 2;
305
306		spi_message_init(&st->msg[AD7887_CH1]);
307		spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
308
309		indio_dev->channels = st->chip_info->channel;
310		indio_dev->num_channels = 3;
311	} else {
312		indio_dev->channels = &st->chip_info->channel[1];
313		indio_dev->num_channels = 2;
314	}
315
316	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
317			&ad7887_trigger_handler, &ad7887_ring_setup_ops);
318	if (ret)
319		goto error_disable_reg;
320
321	ret = iio_device_register(indio_dev);
322	if (ret)
323		goto error_unregister_ring;
324
325	return 0;
326error_unregister_ring:
327	iio_triggered_buffer_cleanup(indio_dev);
328error_disable_reg:
329	if (st->reg)
330		regulator_disable(st->reg);
331
332	return ret;
333}
334
335static int ad7887_remove(struct spi_device *spi)
336{
337	struct iio_dev *indio_dev = spi_get_drvdata(spi);
338	struct ad7887_state *st = iio_priv(indio_dev);
339
340	iio_device_unregister(indio_dev);
341	iio_triggered_buffer_cleanup(indio_dev);
342	if (st->reg)
343		regulator_disable(st->reg);
344
345	return 0;
346}
347
348static const struct spi_device_id ad7887_id[] = {
349	{"ad7887", ID_AD7887},
350	{}
351};
352MODULE_DEVICE_TABLE(spi, ad7887_id);
353
354static struct spi_driver ad7887_driver = {
355	.driver = {
356		.name	= "ad7887",
357	},
358	.probe		= ad7887_probe,
359	.remove		= ad7887_remove,
360	.id_table	= ad7887_id,
361};
362module_spi_driver(ad7887_driver);
363
364MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
365MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
366MODULE_LICENSE("GPL v2");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * AD7887 SPI ADC driver
  4 *
  5 * Copyright 2010-2011 Analog Devices Inc.
  6 */
  7
  8#include <linux/device.h>
  9#include <linux/kernel.h>
 10#include <linux/slab.h>
 11#include <linux/sysfs.h>
 12#include <linux/spi/spi.h>
 13#include <linux/regulator/consumer.h>
 14#include <linux/err.h>
 15#include <linux/module.h>
 16#include <linux/interrupt.h>
 17#include <linux/bitops.h>
 18
 19#include <linux/iio/iio.h>
 20#include <linux/iio/sysfs.h>
 21#include <linux/iio/buffer.h>
 22
 23#include <linux/iio/trigger_consumer.h>
 24#include <linux/iio/triggered_buffer.h>
 25
 26#include <linux/platform_data/ad7887.h>
 27
 28#define AD7887_REF_DIS		BIT(5)	/* on-chip reference disable */
 29#define AD7887_DUAL		BIT(4)	/* dual-channel mode */
 30#define AD7887_CH_AIN1		BIT(3)	/* convert on channel 1, DUAL=1 */
 31#define AD7887_CH_AIN0		0	/* convert on channel 0, DUAL=0,1 */
 32#define AD7887_PM_MODE1		0	/* CS based shutdown */
 33#define AD7887_PM_MODE2		1	/* full on */
 34#define AD7887_PM_MODE3		2	/* auto shutdown after conversion */
 35#define AD7887_PM_MODE4		3	/* standby mode */
 36
 37enum ad7887_channels {
 38	AD7887_CH0,
 39	AD7887_CH0_CH1,
 40	AD7887_CH1,
 41};
 42
 43/**
 44 * struct ad7887_chip_info - chip specifc information
 45 * @int_vref_mv:	the internal reference voltage
 46 * @channels:		channels specification
 47 * @num_channels:	number of channels
 48 * @dual_channels:	channels specification in dual mode
 49 * @num_dual_channels:	number of channels in dual mode
 50 */
 51struct ad7887_chip_info {
 52	u16				int_vref_mv;
 53	const struct iio_chan_spec	*channels;
 54	unsigned int			num_channels;
 55	const struct iio_chan_spec	*dual_channels;
 56	unsigned int			num_dual_channels;
 57};
 58
 59struct ad7887_state {
 60	struct spi_device		*spi;
 61	const struct ad7887_chip_info	*chip_info;
 62	struct regulator		*reg;
 63	struct spi_transfer		xfer[4];
 64	struct spi_message		msg[3];
 65	struct spi_message		*ring_msg;
 66	unsigned char			tx_cmd_buf[4];
 67
 68	/*
 69	 * DMA (thus cache coherency maintenance) requires the
 70	 * transfer buffers to live in their own cache lines.
 71	 * Buffer needs to be large enough to hold two 16 bit samples and a
 72	 * 64 bit aligned 64 bit timestamp.
 73	 */
 74	unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)]
 75		____cacheline_aligned;
 76};
 77
 78enum ad7887_supported_device_ids {
 79	ID_AD7887
 80};
 81
 82static int ad7887_ring_preenable(struct iio_dev *indio_dev)
 83{
 84	struct ad7887_state *st = iio_priv(indio_dev);
 85
 86	/* We know this is a single long so can 'cheat' */
 87	switch (*indio_dev->active_scan_mask) {
 88	case (1 << 0):
 89		st->ring_msg = &st->msg[AD7887_CH0];
 90		break;
 91	case (1 << 1):
 92		st->ring_msg = &st->msg[AD7887_CH1];
 93		/* Dummy read: push CH1 setting down to hardware */
 94		spi_sync(st->spi, st->ring_msg);
 95		break;
 96	case ((1 << 1) | (1 << 0)):
 97		st->ring_msg = &st->msg[AD7887_CH0_CH1];
 98		break;
 99	}
100
101	return 0;
102}
103
104static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
105{
106	struct ad7887_state *st = iio_priv(indio_dev);
107
108	/* dummy read: restore default CH0 settin */
109	return spi_sync(st->spi, &st->msg[AD7887_CH0]);
110}
111
112/*
113 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
114 *
115 * Currently there is no option in this driver to disable the saving of
116 * timestamps within the ring.
117 **/
118static irqreturn_t ad7887_trigger_handler(int irq, void *p)
119{
120	struct iio_poll_func *pf = p;
121	struct iio_dev *indio_dev = pf->indio_dev;
122	struct ad7887_state *st = iio_priv(indio_dev);
123	int b_sent;
124
125	b_sent = spi_sync(st->spi, st->ring_msg);
126	if (b_sent)
127		goto done;
128
129	iio_push_to_buffers_with_timestamp(indio_dev, st->data,
130		iio_get_time_ns(indio_dev));
131done:
132	iio_trigger_notify_done(indio_dev->trig);
133
134	return IRQ_HANDLED;
135}
136
137static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = {
138	.preenable = &ad7887_ring_preenable,
 
 
139	.postdisable = &ad7887_ring_postdisable,
140};
141
142static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch)
143{
144	int ret = spi_sync(st->spi, &st->msg[ch]);
145	if (ret)
146		return ret;
147
148	return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1];
149}
150
151static int ad7887_read_raw(struct iio_dev *indio_dev,
152			   struct iio_chan_spec const *chan,
153			   int *val,
154			   int *val2,
155			   long m)
156{
157	int ret;
158	struct ad7887_state *st = iio_priv(indio_dev);
159
160	switch (m) {
161	case IIO_CHAN_INFO_RAW:
162		ret = iio_device_claim_direct_mode(indio_dev);
163		if (ret)
164			return ret;
165		ret = ad7887_scan_direct(st, chan->address);
166		iio_device_release_direct_mode(indio_dev);
167
168		if (ret < 0)
169			return ret;
170		*val = ret >> chan->scan_type.shift;
171		*val &= GENMASK(chan->scan_type.realbits - 1, 0);
172		return IIO_VAL_INT;
173	case IIO_CHAN_INFO_SCALE:
174		if (st->reg) {
175			*val = regulator_get_voltage(st->reg);
176			if (*val < 0)
177				return *val;
178			*val /= 1000;
179		} else {
180			*val = st->chip_info->int_vref_mv;
181		}
182
183		*val2 = chan->scan_type.realbits;
184
185		return IIO_VAL_FRACTIONAL_LOG2;
186	}
187	return -EINVAL;
188}
189
190#define AD7887_CHANNEL(x) { \
191	.type = IIO_VOLTAGE, \
192	.indexed = 1, \
193	.channel = (x), \
194	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
195	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
196	.address = (x), \
197	.scan_index = (x), \
198	.scan_type = { \
199		.sign = 'u', \
200		.realbits = 12, \
201		.storagebits = 16, \
202		.shift = 0, \
203		.endianness = IIO_BE, \
204	}, \
205}
206
207static const struct iio_chan_spec ad7887_channels[] = {
208	AD7887_CHANNEL(0),
209	IIO_CHAN_SOFT_TIMESTAMP(1),
210};
211
212static const struct iio_chan_spec ad7887_dual_channels[] = {
213	AD7887_CHANNEL(0),
214	AD7887_CHANNEL(1),
215	IIO_CHAN_SOFT_TIMESTAMP(2),
216};
217
218static const struct ad7887_chip_info ad7887_chip_info_tbl[] = {
219	/*
220	 * More devices added in future
221	 */
222	[ID_AD7887] = {
223		.channels = ad7887_channels,
224		.num_channels = ARRAY_SIZE(ad7887_channels),
225		.dual_channels = ad7887_dual_channels,
226		.num_dual_channels = ARRAY_SIZE(ad7887_dual_channels),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227		.int_vref_mv = 2500,
228	},
229};
230
231static const struct iio_info ad7887_info = {
232	.read_raw = &ad7887_read_raw,
233};
234
235static int ad7887_probe(struct spi_device *spi)
236{
237	struct ad7887_platform_data *pdata = spi->dev.platform_data;
238	struct ad7887_state *st;
239	struct iio_dev *indio_dev;
240	uint8_t mode;
241	int ret;
242
243	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
244	if (indio_dev == NULL)
245		return -ENOMEM;
246
247	st = iio_priv(indio_dev);
248
249	if (!pdata || !pdata->use_onchip_ref) {
250		st->reg = devm_regulator_get(&spi->dev, "vref");
251		if (IS_ERR(st->reg))
252			return PTR_ERR(st->reg);
253
254		ret = regulator_enable(st->reg);
255		if (ret)
256			return ret;
257	}
258
259	st->chip_info =
260		&ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data];
261
262	spi_set_drvdata(spi, indio_dev);
263	st->spi = spi;
264
 
 
 
265	indio_dev->name = spi_get_device_id(spi)->name;
266	indio_dev->info = &ad7887_info;
267	indio_dev->modes = INDIO_DIRECT_MODE;
268
269	/* Setup default message */
270
271	mode = AD7887_PM_MODE4;
272	if (!pdata || !pdata->use_onchip_ref)
273		mode |= AD7887_REF_DIS;
274	if (pdata && pdata->en_dual)
275		mode |= AD7887_DUAL;
276
277	st->tx_cmd_buf[0] = AD7887_CH_AIN0 | mode;
278
279	st->xfer[0].rx_buf = &st->data[0];
280	st->xfer[0].tx_buf = &st->tx_cmd_buf[0];
281	st->xfer[0].len = 2;
282
283	spi_message_init(&st->msg[AD7887_CH0]);
284	spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]);
285
286	if (pdata && pdata->en_dual) {
287		st->tx_cmd_buf[2] = AD7887_CH_AIN1 | mode;
288
289		st->xfer[1].rx_buf = &st->data[0];
290		st->xfer[1].tx_buf = &st->tx_cmd_buf[2];
291		st->xfer[1].len = 2;
292
293		st->xfer[2].rx_buf = &st->data[2];
294		st->xfer[2].tx_buf = &st->tx_cmd_buf[0];
295		st->xfer[2].len = 2;
296
297		spi_message_init(&st->msg[AD7887_CH0_CH1]);
298		spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]);
299		spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]);
300
301		st->xfer[3].rx_buf = &st->data[2];
302		st->xfer[3].tx_buf = &st->tx_cmd_buf[2];
303		st->xfer[3].len = 2;
304
305		spi_message_init(&st->msg[AD7887_CH1]);
306		spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]);
307
308		indio_dev->channels = st->chip_info->dual_channels;
309		indio_dev->num_channels = st->chip_info->num_dual_channels;
310	} else {
311		indio_dev->channels = st->chip_info->channels;
312		indio_dev->num_channels = st->chip_info->num_channels;
313	}
314
315	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
316			&ad7887_trigger_handler, &ad7887_ring_setup_ops);
317	if (ret)
318		goto error_disable_reg;
319
320	ret = iio_device_register(indio_dev);
321	if (ret)
322		goto error_unregister_ring;
323
324	return 0;
325error_unregister_ring:
326	iio_triggered_buffer_cleanup(indio_dev);
327error_disable_reg:
328	if (st->reg)
329		regulator_disable(st->reg);
330
331	return ret;
332}
333
334static int ad7887_remove(struct spi_device *spi)
335{
336	struct iio_dev *indio_dev = spi_get_drvdata(spi);
337	struct ad7887_state *st = iio_priv(indio_dev);
338
339	iio_device_unregister(indio_dev);
340	iio_triggered_buffer_cleanup(indio_dev);
341	if (st->reg)
342		regulator_disable(st->reg);
343
344	return 0;
345}
346
347static const struct spi_device_id ad7887_id[] = {
348	{"ad7887", ID_AD7887},
349	{}
350};
351MODULE_DEVICE_TABLE(spi, ad7887_id);
352
353static struct spi_driver ad7887_driver = {
354	.driver = {
355		.name	= "ad7887",
356	},
357	.probe		= ad7887_probe,
358	.remove		= ad7887_remove,
359	.id_table	= ad7887_id,
360};
361module_spi_driver(ad7887_driver);
362
363MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
364MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
365MODULE_LICENSE("GPL v2");