xref: /openbmc/linux/drivers/iio/adc/ad_sigma_delta.c (revision d7955ce4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Support code for Analog Devices Sigma-Delta ADCs
4  *
5  * Copyright 2012 Analog Devices Inc.
6  *  Author: Lars-Peter Clausen <lars@metafoo.de>
7  */
8 
9 #include <linux/align.h>
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
15 #include <linux/err.h>
16 #include <linux/module.h>
17 
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/adc/ad_sigma_delta.h>
25 
26 #include <asm/unaligned.h>
27 
28 
29 #define AD_SD_COMM_CHAN_MASK	0x3
30 
31 #define AD_SD_REG_COMM		0x00
32 #define AD_SD_REG_DATA		0x03
33 
34 /**
35  * ad_sd_set_comm() - Set communications register
36  *
37  * @sigma_delta: The sigma delta device
38  * @comm: New value for the communications register
39  */
40 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm)
41 {
42 	/* Some variants use the lower two bits of the communications register
43 	 * to select the channel */
44 	sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK;
45 }
46 EXPORT_SYMBOL_NS_GPL(ad_sd_set_comm, IIO_AD_SIGMA_DELTA);
47 
48 /**
49  * ad_sd_write_reg() - Write a register
50  *
51  * @sigma_delta: The sigma delta device
52  * @reg: Address of the register
53  * @size: Size of the register (0-3)
54  * @val: Value to write to the register
55  *
56  * Returns 0 on success, an error code otherwise.
57  **/
58 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
59 	unsigned int size, unsigned int val)
60 {
61 	uint8_t *data = sigma_delta->tx_buf;
62 	struct spi_transfer t = {
63 		.tx_buf		= data,
64 		.len		= size + 1,
65 		.cs_change	= sigma_delta->keep_cs_asserted,
66 	};
67 	struct spi_message m;
68 	int ret;
69 
70 	data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm;
71 
72 	switch (size) {
73 	case 3:
74 		put_unaligned_be24(val, &data[1]);
75 		break;
76 	case 2:
77 		put_unaligned_be16(val, &data[1]);
78 		break;
79 	case 1:
80 		data[1] = val;
81 		break;
82 	case 0:
83 		break;
84 	default:
85 		return -EINVAL;
86 	}
87 
88 	spi_message_init(&m);
89 	spi_message_add_tail(&t, &m);
90 
91 	if (sigma_delta->bus_locked)
92 		ret = spi_sync_locked(sigma_delta->spi, &m);
93 	else
94 		ret = spi_sync(sigma_delta->spi, &m);
95 
96 	return ret;
97 }
98 EXPORT_SYMBOL_NS_GPL(ad_sd_write_reg, IIO_AD_SIGMA_DELTA);
99 
100 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
101 	unsigned int reg, unsigned int size, uint8_t *val)
102 {
103 	uint8_t *data = sigma_delta->tx_buf;
104 	int ret;
105 	struct spi_transfer t[] = {
106 		{
107 			.tx_buf = data,
108 			.len = 1,
109 		}, {
110 			.rx_buf = val,
111 			.len = size,
112 			.cs_change = sigma_delta->bus_locked,
113 		},
114 	};
115 	struct spi_message m;
116 
117 	spi_message_init(&m);
118 
119 	if (sigma_delta->info->has_registers) {
120 		data[0] = reg << sigma_delta->info->addr_shift;
121 		data[0] |= sigma_delta->info->read_mask;
122 		data[0] |= sigma_delta->comm;
123 		spi_message_add_tail(&t[0], &m);
124 	}
125 	spi_message_add_tail(&t[1], &m);
126 
127 	if (sigma_delta->bus_locked)
128 		ret = spi_sync_locked(sigma_delta->spi, &m);
129 	else
130 		ret = spi_sync(sigma_delta->spi, &m);
131 
132 	return ret;
133 }
134 
135 /**
136  * ad_sd_read_reg() - Read a register
137  *
138  * @sigma_delta: The sigma delta device
139  * @reg: Address of the register
140  * @size: Size of the register (1-4)
141  * @val: Read value
142  *
143  * Returns 0 on success, an error code otherwise.
144  **/
145 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
146 	unsigned int reg, unsigned int size, unsigned int *val)
147 {
148 	int ret;
149 
150 	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->rx_buf);
151 	if (ret < 0)
152 		goto out;
153 
154 	switch (size) {
155 	case 4:
156 		*val = get_unaligned_be32(sigma_delta->rx_buf);
157 		break;
158 	case 3:
159 		*val = get_unaligned_be24(sigma_delta->rx_buf);
160 		break;
161 	case 2:
162 		*val = get_unaligned_be16(sigma_delta->rx_buf);
163 		break;
164 	case 1:
165 		*val = sigma_delta->rx_buf[0];
166 		break;
167 	default:
168 		ret = -EINVAL;
169 		break;
170 	}
171 
172 out:
173 	return ret;
174 }
175 EXPORT_SYMBOL_NS_GPL(ad_sd_read_reg, IIO_AD_SIGMA_DELTA);
176 
177 /**
178  * ad_sd_reset() - Reset the serial interface
179  *
180  * @sigma_delta: The sigma delta device
181  * @reset_length: Number of SCLKs with DIN = 1
182  *
183  * Returns 0 on success, an error code otherwise.
184  **/
185 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
186 	unsigned int reset_length)
187 {
188 	uint8_t *buf;
189 	unsigned int size;
190 	int ret;
191 
192 	size = DIV_ROUND_UP(reset_length, 8);
193 	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
194 	if (!buf)
195 		return -ENOMEM;
196 
197 	memset(buf, 0xff, size);
198 	ret = spi_write(sigma_delta->spi, buf, size);
199 	kfree(buf);
200 
201 	return ret;
202 }
203 EXPORT_SYMBOL_NS_GPL(ad_sd_reset, IIO_AD_SIGMA_DELTA);
204 
205 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
206 	unsigned int mode, unsigned int channel)
207 {
208 	int ret;
209 	unsigned long timeout;
210 
211 	ret = ad_sigma_delta_set_channel(sigma_delta, channel);
212 	if (ret)
213 		return ret;
214 
215 	spi_bus_lock(sigma_delta->spi->master);
216 	sigma_delta->bus_locked = true;
217 	sigma_delta->keep_cs_asserted = true;
218 	reinit_completion(&sigma_delta->completion);
219 
220 	ret = ad_sigma_delta_set_mode(sigma_delta, mode);
221 	if (ret < 0)
222 		goto out;
223 
224 	sigma_delta->irq_dis = false;
225 	enable_irq(sigma_delta->spi->irq);
226 	timeout = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ);
227 	if (timeout == 0) {
228 		sigma_delta->irq_dis = true;
229 		disable_irq_nosync(sigma_delta->spi->irq);
230 		ret = -EIO;
231 	} else {
232 		ret = 0;
233 	}
234 out:
235 	sigma_delta->keep_cs_asserted = false;
236 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
237 	sigma_delta->bus_locked = false;
238 	spi_bus_unlock(sigma_delta->spi->master);
239 
240 	return ret;
241 }
242 EXPORT_SYMBOL_NS_GPL(ad_sd_calibrate, IIO_AD_SIGMA_DELTA);
243 
244 /**
245  * ad_sd_calibrate_all() - Performs channel calibration
246  * @sigma_delta: The sigma delta device
247  * @cb: Array of channels and calibration type to perform
248  * @n: Number of items in cb
249  *
250  * Returns 0 on success, an error code otherwise.
251  **/
252 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
253 	const struct ad_sd_calib_data *cb, unsigned int n)
254 {
255 	unsigned int i;
256 	int ret;
257 
258 	for (i = 0; i < n; i++) {
259 		ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
260 		if (ret)
261 			return ret;
262 	}
263 
264 	return 0;
265 }
266 EXPORT_SYMBOL_NS_GPL(ad_sd_calibrate_all, IIO_AD_SIGMA_DELTA);
267 
268 /**
269  * ad_sigma_delta_single_conversion() - Performs a single data conversion
270  * @indio_dev: The IIO device
271  * @chan: The conversion is done for this channel
272  * @val: Pointer to the location where to store the read value
273  *
274  * Returns: 0 on success, an error value otherwise.
275  */
276 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
277 	const struct iio_chan_spec *chan, int *val)
278 {
279 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
280 	unsigned int sample, raw_sample;
281 	unsigned int data_reg;
282 	int ret = 0;
283 
284 	ret = iio_device_claim_direct_mode(indio_dev);
285 	if (ret)
286 		return ret;
287 
288 	ad_sigma_delta_set_channel(sigma_delta, chan->address);
289 
290 	spi_bus_lock(sigma_delta->spi->master);
291 	sigma_delta->bus_locked = true;
292 	sigma_delta->keep_cs_asserted = true;
293 	reinit_completion(&sigma_delta->completion);
294 
295 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
296 
297 	sigma_delta->irq_dis = false;
298 	enable_irq(sigma_delta->spi->irq);
299 	ret = wait_for_completion_interruptible_timeout(
300 			&sigma_delta->completion, HZ);
301 
302 	if (ret == 0)
303 		ret = -EIO;
304 	if (ret < 0)
305 		goto out;
306 
307 	if (sigma_delta->info->data_reg != 0)
308 		data_reg = sigma_delta->info->data_reg;
309 	else
310 		data_reg = AD_SD_REG_DATA;
311 
312 	ret = ad_sd_read_reg(sigma_delta, data_reg,
313 		DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
314 		&raw_sample);
315 
316 out:
317 	if (!sigma_delta->irq_dis) {
318 		disable_irq_nosync(sigma_delta->spi->irq);
319 		sigma_delta->irq_dis = true;
320 	}
321 
322 	sigma_delta->keep_cs_asserted = false;
323 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
324 	sigma_delta->bus_locked = false;
325 	spi_bus_unlock(sigma_delta->spi->master);
326 	iio_device_release_direct_mode(indio_dev);
327 
328 	if (ret)
329 		return ret;
330 
331 	sample = raw_sample >> chan->scan_type.shift;
332 	sample &= (1 << chan->scan_type.realbits) - 1;
333 	*val = sample;
334 
335 	ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
336 	if (ret)
337 		return ret;
338 
339 	return IIO_VAL_INT;
340 }
341 EXPORT_SYMBOL_NS_GPL(ad_sigma_delta_single_conversion, IIO_AD_SIGMA_DELTA);
342 
343 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
344 {
345 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
346 	unsigned int i, slot, samples_buf_size;
347 	unsigned int channel;
348 	uint8_t *samples_buf;
349 	int ret;
350 
351 	if (sigma_delta->num_slots == 1) {
352 		channel = find_first_bit(indio_dev->active_scan_mask,
353 					 indio_dev->masklength);
354 		ret = ad_sigma_delta_set_channel(sigma_delta,
355 						 indio_dev->channels[channel].address);
356 		if (ret)
357 			return ret;
358 		slot = 1;
359 	} else {
360 		/*
361 		 * At this point update_scan_mode already enabled the required channels.
362 		 * For sigma-delta sequencer drivers with multiple slots, an update_scan_mode
363 		 * implementation is mandatory.
364 		 */
365 		slot = 0;
366 		for_each_set_bit(i, indio_dev->active_scan_mask, indio_dev->masklength) {
367 			sigma_delta->slots[slot] = indio_dev->channels[i].address;
368 			slot++;
369 		}
370 	}
371 
372 	sigma_delta->active_slots = slot;
373 	sigma_delta->current_slot = 0;
374 
375 	if (sigma_delta->active_slots > 1) {
376 		ret = ad_sigma_delta_append_status(sigma_delta, true);
377 		if (ret)
378 			return ret;
379 	}
380 
381 	samples_buf_size = ALIGN(slot * indio_dev->channels[0].scan_type.storagebits, 8);
382 	samples_buf_size += sizeof(int64_t);
383 	samples_buf = devm_krealloc(&sigma_delta->spi->dev, sigma_delta->samples_buf,
384 				    samples_buf_size, GFP_KERNEL);
385 	if (!samples_buf)
386 		return -ENOMEM;
387 
388 	sigma_delta->samples_buf = samples_buf;
389 
390 	spi_bus_lock(sigma_delta->spi->master);
391 	sigma_delta->bus_locked = true;
392 	sigma_delta->keep_cs_asserted = true;
393 
394 	ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
395 	if (ret)
396 		goto err_unlock;
397 
398 	sigma_delta->irq_dis = false;
399 	enable_irq(sigma_delta->spi->irq);
400 
401 	return 0;
402 
403 err_unlock:
404 	spi_bus_unlock(sigma_delta->spi->master);
405 
406 	return ret;
407 }
408 
409 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
410 {
411 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
412 
413 	reinit_completion(&sigma_delta->completion);
414 	wait_for_completion_timeout(&sigma_delta->completion, HZ);
415 
416 	if (!sigma_delta->irq_dis) {
417 		disable_irq_nosync(sigma_delta->spi->irq);
418 		sigma_delta->irq_dis = true;
419 	}
420 
421 	sigma_delta->keep_cs_asserted = false;
422 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
423 
424 	if (sigma_delta->status_appended)
425 		ad_sigma_delta_append_status(sigma_delta, false);
426 
427 	ad_sigma_delta_disable_all(sigma_delta);
428 	sigma_delta->bus_locked = false;
429 	return spi_bus_unlock(sigma_delta->spi->master);
430 }
431 
432 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
433 {
434 	struct iio_poll_func *pf = p;
435 	struct iio_dev *indio_dev = pf->indio_dev;
436 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
437 	uint8_t *data = sigma_delta->rx_buf;
438 	unsigned int transfer_size;
439 	unsigned int sample_size;
440 	unsigned int sample_pos;
441 	unsigned int status_pos;
442 	unsigned int reg_size;
443 	unsigned int data_reg;
444 
445 	reg_size = indio_dev->channels[0].scan_type.realbits +
446 			indio_dev->channels[0].scan_type.shift;
447 	reg_size = DIV_ROUND_UP(reg_size, 8);
448 
449 	if (sigma_delta->info->data_reg != 0)
450 		data_reg = sigma_delta->info->data_reg;
451 	else
452 		data_reg = AD_SD_REG_DATA;
453 
454 	/* Status word will be appended to the sample during transfer */
455 	if (sigma_delta->status_appended)
456 		transfer_size = reg_size + 1;
457 	else
458 		transfer_size = reg_size;
459 
460 	switch (reg_size) {
461 	case 4:
462 	case 2:
463 	case 1:
464 		status_pos = reg_size;
465 		ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[0]);
466 		break;
467 	case 3:
468 		/*
469 		 * Data array after transfer will look like (if status is appended):
470 		 * data[] = { [0][sample][sample][sample][status] }
471 		 * Keeping the first byte 0 shifts the status postion by 1 byte to the right.
472 		 */
473 		status_pos = reg_size + 1;
474 
475 		/* We store 24 bit samples in a 32 bit word. Keep the upper
476 		 * byte set to zero. */
477 		ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[1]);
478 		break;
479 	}
480 
481 	/*
482 	 * For devices sampling only one channel at
483 	 * once, there is no need for sample number tracking.
484 	 */
485 	if (sigma_delta->active_slots == 1) {
486 		iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
487 		goto irq_handled;
488 	}
489 
490 	if (sigma_delta->status_appended) {
491 		u8 converted_channel;
492 
493 		converted_channel = data[status_pos] & sigma_delta->info->status_ch_mask;
494 		if (converted_channel != sigma_delta->slots[sigma_delta->current_slot]) {
495 			/*
496 			 * Desync occurred during continuous sampling of multiple channels.
497 			 * Drop this incomplete sample and start from first channel again.
498 			 */
499 
500 			sigma_delta->current_slot = 0;
501 			goto irq_handled;
502 		}
503 	}
504 
505 	sample_size = indio_dev->channels[0].scan_type.storagebits / 8;
506 	sample_pos = sample_size * sigma_delta->current_slot;
507 	memcpy(&sigma_delta->samples_buf[sample_pos], data, sample_size);
508 	sigma_delta->current_slot++;
509 
510 	if (sigma_delta->current_slot == sigma_delta->active_slots) {
511 		sigma_delta->current_slot = 0;
512 		iio_push_to_buffers_with_timestamp(indio_dev, sigma_delta->samples_buf,
513 						   pf->timestamp);
514 	}
515 
516 irq_handled:
517 	iio_trigger_notify_done(indio_dev->trig);
518 	sigma_delta->irq_dis = false;
519 	enable_irq(sigma_delta->spi->irq);
520 
521 	return IRQ_HANDLED;
522 }
523 
524 static bool ad_sd_validate_scan_mask(struct iio_dev *indio_dev, const unsigned long *mask)
525 {
526 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
527 
528 	return bitmap_weight(mask, indio_dev->masklength) <= sigma_delta->num_slots;
529 }
530 
531 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
532 	.postenable = &ad_sd_buffer_postenable,
533 	.postdisable = &ad_sd_buffer_postdisable,
534 	.validate_scan_mask = &ad_sd_validate_scan_mask,
535 };
536 
537 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
538 {
539 	struct ad_sigma_delta *sigma_delta = private;
540 
541 	complete(&sigma_delta->completion);
542 	disable_irq_nosync(irq);
543 	sigma_delta->irq_dis = true;
544 	iio_trigger_poll(sigma_delta->trig);
545 
546 	return IRQ_HANDLED;
547 }
548 
549 /**
550  * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
551  * @indio_dev: The IIO device
552  * @trig: The new trigger
553  *
554  * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
555  * device, -EINVAL otherwise.
556  */
557 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
558 {
559 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
560 
561 	if (sigma_delta->trig != trig)
562 		return -EINVAL;
563 
564 	return 0;
565 }
566 EXPORT_SYMBOL_NS_GPL(ad_sd_validate_trigger, IIO_AD_SIGMA_DELTA);
567 
568 static int devm_ad_sd_probe_trigger(struct device *dev, struct iio_dev *indio_dev)
569 {
570 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
571 	int ret;
572 
573 	if (dev != &sigma_delta->spi->dev) {
574 		dev_err(dev, "Trigger parent should be '%s', got '%s'\n",
575 			dev_name(dev), dev_name(&sigma_delta->spi->dev));
576 		return -EFAULT;
577 	}
578 
579 	sigma_delta->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
580 						   iio_device_id(indio_dev));
581 	if (sigma_delta->trig == NULL)
582 		return -ENOMEM;
583 
584 	init_completion(&sigma_delta->completion);
585 
586 	sigma_delta->irq_dis = true;
587 
588 	/* the IRQ core clears IRQ_DISABLE_UNLAZY flag when freeing an IRQ */
589 	irq_set_status_flags(sigma_delta->spi->irq, IRQ_DISABLE_UNLAZY);
590 
591 	ret = devm_request_irq(dev, sigma_delta->spi->irq,
592 			       ad_sd_data_rdy_trig_poll,
593 			       sigma_delta->info->irq_flags | IRQF_NO_AUTOEN,
594 			       indio_dev->name,
595 			       sigma_delta);
596 	if (ret)
597 		return ret;
598 
599 	iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
600 
601 	ret = devm_iio_trigger_register(dev, sigma_delta->trig);
602 	if (ret)
603 		return ret;
604 
605 	/* select default trigger */
606 	indio_dev->trig = iio_trigger_get(sigma_delta->trig);
607 
608 	return 0;
609 }
610 
611 /**
612  * devm_ad_sd_setup_buffer_and_trigger() - Device-managed buffer & trigger setup
613  * @dev: Device object to which to bind the life-time of the resources attached
614  * @indio_dev: The IIO device
615  */
616 int devm_ad_sd_setup_buffer_and_trigger(struct device *dev, struct iio_dev *indio_dev)
617 {
618 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
619 	int ret;
620 
621 	sigma_delta->slots = devm_kcalloc(dev, sigma_delta->num_slots,
622 					  sizeof(*sigma_delta->slots), GFP_KERNEL);
623 	if (!sigma_delta->slots)
624 		return -ENOMEM;
625 
626 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
627 					      &iio_pollfunc_store_time,
628 					      &ad_sd_trigger_handler,
629 					      &ad_sd_buffer_setup_ops);
630 	if (ret)
631 		return ret;
632 
633 	return devm_ad_sd_probe_trigger(dev, indio_dev);
634 }
635 EXPORT_SYMBOL_NS_GPL(devm_ad_sd_setup_buffer_and_trigger, IIO_AD_SIGMA_DELTA);
636 
637 /**
638  * ad_sd_init() - Initializes a ad_sigma_delta struct
639  * @sigma_delta: The ad_sigma_delta device
640  * @indio_dev: The IIO device which the Sigma Delta device is used for
641  * @spi: The SPI device for the ad_sigma_delta device
642  * @info: Device specific callbacks and options
643  *
644  * This function needs to be called before any other operations are performed on
645  * the ad_sigma_delta struct.
646  */
647 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
648 	struct spi_device *spi, const struct ad_sigma_delta_info *info)
649 {
650 	sigma_delta->spi = spi;
651 	sigma_delta->info = info;
652 
653 	/* If the field is unset in ad_sigma_delta_info, asume there can only be 1 slot. */
654 	if (!info->num_slots)
655 		sigma_delta->num_slots = 1;
656 	else
657 		sigma_delta->num_slots = info->num_slots;
658 
659 	if (sigma_delta->num_slots > 1) {
660 		if (!indio_dev->info->update_scan_mode) {
661 			dev_err(&spi->dev, "iio_dev lacks update_scan_mode().\n");
662 			return -EINVAL;
663 		}
664 
665 		if (!info->disable_all) {
666 			dev_err(&spi->dev, "ad_sigma_delta_info lacks disable_all().\n");
667 			return -EINVAL;
668 		}
669 	}
670 
671 	iio_device_set_drvdata(indio_dev, sigma_delta);
672 
673 	return 0;
674 }
675 EXPORT_SYMBOL_NS_GPL(ad_sd_init, IIO_AD_SIGMA_DELTA);
676 
677 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
678 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
679 MODULE_LICENSE("GPL v2");
680