xref: /linux/drivers/iio/adc/ad_sigma_delta.c (revision f879306834818ebd1722a4372079610cdd466fec)
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 time_left;
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->controller);
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->irq_line);
226 	time_left = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ);
227 	if (time_left == 0) {
228 		sigma_delta->irq_dis = true;
229 		disable_irq_nosync(sigma_delta->irq_line);
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->controller);
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->controller);
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->irq_line);
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->irq_line);
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 	ad_sigma_delta_disable_one(sigma_delta, chan->address);
325 	sigma_delta->bus_locked = false;
326 	spi_bus_unlock(sigma_delta->spi->controller);
327 	iio_device_release_direct_mode(indio_dev);
328 
329 	if (ret)
330 		return ret;
331 
332 	sample = raw_sample >> chan->scan_type.shift;
333 	sample &= (1 << chan->scan_type.realbits) - 1;
334 	*val = sample;
335 
336 	ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
337 	if (ret)
338 		return ret;
339 
340 	return IIO_VAL_INT;
341 }
342 EXPORT_SYMBOL_NS_GPL(ad_sigma_delta_single_conversion, IIO_AD_SIGMA_DELTA);
343 
344 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
345 {
346 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
347 	unsigned int i, slot, samples_buf_size;
348 	unsigned int channel;
349 	uint8_t *samples_buf;
350 	int ret;
351 
352 	if (sigma_delta->num_slots == 1) {
353 		channel = find_first_bit(indio_dev->active_scan_mask,
354 					 indio_dev->masklength);
355 		ret = ad_sigma_delta_set_channel(sigma_delta,
356 						 indio_dev->channels[channel].address);
357 		if (ret)
358 			return ret;
359 		slot = 1;
360 	} else {
361 		/*
362 		 * At this point update_scan_mode already enabled the required channels.
363 		 * For sigma-delta sequencer drivers with multiple slots, an update_scan_mode
364 		 * implementation is mandatory.
365 		 */
366 		slot = 0;
367 		for_each_set_bit(i, indio_dev->active_scan_mask, indio_dev->masklength) {
368 			sigma_delta->slots[slot] = indio_dev->channels[i].address;
369 			slot++;
370 		}
371 	}
372 
373 	sigma_delta->active_slots = slot;
374 	sigma_delta->current_slot = 0;
375 
376 	if (sigma_delta->active_slots > 1) {
377 		ret = ad_sigma_delta_append_status(sigma_delta, true);
378 		if (ret)
379 			return ret;
380 	}
381 
382 	samples_buf_size = ALIGN(slot * indio_dev->channels[0].scan_type.storagebits, 8);
383 	samples_buf_size += sizeof(int64_t);
384 	samples_buf = devm_krealloc(&sigma_delta->spi->dev, sigma_delta->samples_buf,
385 				    samples_buf_size, GFP_KERNEL);
386 	if (!samples_buf)
387 		return -ENOMEM;
388 
389 	sigma_delta->samples_buf = samples_buf;
390 
391 	spi_bus_lock(sigma_delta->spi->controller);
392 	sigma_delta->bus_locked = true;
393 	sigma_delta->keep_cs_asserted = true;
394 
395 	ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
396 	if (ret)
397 		goto err_unlock;
398 
399 	sigma_delta->irq_dis = false;
400 	enable_irq(sigma_delta->irq_line);
401 
402 	return 0;
403 
404 err_unlock:
405 	spi_bus_unlock(sigma_delta->spi->controller);
406 
407 	return ret;
408 }
409 
410 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
411 {
412 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
413 
414 	reinit_completion(&sigma_delta->completion);
415 	wait_for_completion_timeout(&sigma_delta->completion, HZ);
416 
417 	if (!sigma_delta->irq_dis) {
418 		disable_irq_nosync(sigma_delta->irq_line);
419 		sigma_delta->irq_dis = true;
420 	}
421 
422 	sigma_delta->keep_cs_asserted = false;
423 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
424 
425 	if (sigma_delta->status_appended)
426 		ad_sigma_delta_append_status(sigma_delta, false);
427 
428 	ad_sigma_delta_disable_all(sigma_delta);
429 	sigma_delta->bus_locked = false;
430 	return spi_bus_unlock(sigma_delta->spi->controller);
431 }
432 
433 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
434 {
435 	struct iio_poll_func *pf = p;
436 	struct iio_dev *indio_dev = pf->indio_dev;
437 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
438 	uint8_t *data = sigma_delta->rx_buf;
439 	unsigned int transfer_size;
440 	unsigned int sample_size;
441 	unsigned int sample_pos;
442 	unsigned int status_pos;
443 	unsigned int reg_size;
444 	unsigned int data_reg;
445 
446 	reg_size = indio_dev->channels[0].scan_type.realbits +
447 			indio_dev->channels[0].scan_type.shift;
448 	reg_size = DIV_ROUND_UP(reg_size, 8);
449 
450 	if (sigma_delta->info->data_reg != 0)
451 		data_reg = sigma_delta->info->data_reg;
452 	else
453 		data_reg = AD_SD_REG_DATA;
454 
455 	/* Status word will be appended to the sample during transfer */
456 	if (sigma_delta->status_appended)
457 		transfer_size = reg_size + 1;
458 	else
459 		transfer_size = reg_size;
460 
461 	switch (reg_size) {
462 	case 4:
463 	case 2:
464 	case 1:
465 		status_pos = reg_size;
466 		ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[0]);
467 		break;
468 	case 3:
469 		/*
470 		 * Data array after transfer will look like (if status is appended):
471 		 * data[] = { [0][sample][sample][sample][status] }
472 		 * Keeping the first byte 0 shifts the status postion by 1 byte to the right.
473 		 */
474 		status_pos = reg_size + 1;
475 
476 		/* We store 24 bit samples in a 32 bit word. Keep the upper
477 		 * byte set to zero. */
478 		ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[1]);
479 		break;
480 	}
481 
482 	/*
483 	 * For devices sampling only one channel at
484 	 * once, there is no need for sample number tracking.
485 	 */
486 	if (sigma_delta->active_slots == 1) {
487 		iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
488 		goto irq_handled;
489 	}
490 
491 	if (sigma_delta->status_appended) {
492 		u8 converted_channel;
493 
494 		converted_channel = data[status_pos] & sigma_delta->info->status_ch_mask;
495 		if (converted_channel != sigma_delta->slots[sigma_delta->current_slot]) {
496 			/*
497 			 * Desync occurred during continuous sampling of multiple channels.
498 			 * Drop this incomplete sample and start from first channel again.
499 			 */
500 
501 			sigma_delta->current_slot = 0;
502 			goto irq_handled;
503 		}
504 	}
505 
506 	sample_size = indio_dev->channels[0].scan_type.storagebits / 8;
507 	sample_pos = sample_size * sigma_delta->current_slot;
508 	memcpy(&sigma_delta->samples_buf[sample_pos], data, sample_size);
509 	sigma_delta->current_slot++;
510 
511 	if (sigma_delta->current_slot == sigma_delta->active_slots) {
512 		sigma_delta->current_slot = 0;
513 		iio_push_to_buffers_with_timestamp(indio_dev, sigma_delta->samples_buf,
514 						   pf->timestamp);
515 	}
516 
517 irq_handled:
518 	iio_trigger_notify_done(indio_dev->trig);
519 	sigma_delta->irq_dis = false;
520 	enable_irq(sigma_delta->irq_line);
521 
522 	return IRQ_HANDLED;
523 }
524 
525 static bool ad_sd_validate_scan_mask(struct iio_dev *indio_dev, const unsigned long *mask)
526 {
527 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
528 
529 	return bitmap_weight(mask, indio_dev->masklength) <= sigma_delta->num_slots;
530 }
531 
532 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
533 	.postenable = &ad_sd_buffer_postenable,
534 	.postdisable = &ad_sd_buffer_postdisable,
535 	.validate_scan_mask = &ad_sd_validate_scan_mask,
536 };
537 
538 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
539 {
540 	struct ad_sigma_delta *sigma_delta = private;
541 
542 	complete(&sigma_delta->completion);
543 	disable_irq_nosync(irq);
544 	sigma_delta->irq_dis = true;
545 	iio_trigger_poll(sigma_delta->trig);
546 
547 	return IRQ_HANDLED;
548 }
549 
550 /**
551  * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
552  * @indio_dev: The IIO device
553  * @trig: The new trigger
554  *
555  * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
556  * device, -EINVAL otherwise.
557  */
558 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
559 {
560 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
561 
562 	if (sigma_delta->trig != trig)
563 		return -EINVAL;
564 
565 	return 0;
566 }
567 EXPORT_SYMBOL_NS_GPL(ad_sd_validate_trigger, IIO_AD_SIGMA_DELTA);
568 
569 static int devm_ad_sd_probe_trigger(struct device *dev, struct iio_dev *indio_dev)
570 {
571 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
572 	unsigned long irq_flags = irq_get_trigger_type(sigma_delta->spi->irq);
573 	int ret;
574 
575 	if (dev != &sigma_delta->spi->dev) {
576 		dev_err(dev, "Trigger parent should be '%s', got '%s'\n",
577 			dev_name(dev), dev_name(&sigma_delta->spi->dev));
578 		return -EFAULT;
579 	}
580 
581 	sigma_delta->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
582 						   iio_device_id(indio_dev));
583 	if (sigma_delta->trig == NULL)
584 		return -ENOMEM;
585 
586 	init_completion(&sigma_delta->completion);
587 
588 	sigma_delta->irq_dis = true;
589 
590 	/* the IRQ core clears IRQ_DISABLE_UNLAZY flag when freeing an IRQ */
591 	irq_set_status_flags(sigma_delta->irq_line, IRQ_DISABLE_UNLAZY);
592 
593 	/* Allow overwriting the flags from firmware */
594 	if (!irq_flags)
595 		irq_flags = sigma_delta->info->irq_flags;
596 
597 	ret = devm_request_irq(dev, sigma_delta->irq_line,
598 			       ad_sd_data_rdy_trig_poll,
599 			       irq_flags | IRQF_NO_AUTOEN,
600 			       indio_dev->name,
601 			       sigma_delta);
602 	if (ret)
603 		return ret;
604 
605 	iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
606 
607 	ret = devm_iio_trigger_register(dev, sigma_delta->trig);
608 	if (ret)
609 		return ret;
610 
611 	/* select default trigger */
612 	indio_dev->trig = iio_trigger_get(sigma_delta->trig);
613 
614 	return 0;
615 }
616 
617 /**
618  * devm_ad_sd_setup_buffer_and_trigger() - Device-managed buffer & trigger setup
619  * @dev: Device object to which to bind the life-time of the resources attached
620  * @indio_dev: The IIO device
621  */
622 int devm_ad_sd_setup_buffer_and_trigger(struct device *dev, struct iio_dev *indio_dev)
623 {
624 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
625 	int ret;
626 
627 	sigma_delta->slots = devm_kcalloc(dev, sigma_delta->num_slots,
628 					  sizeof(*sigma_delta->slots), GFP_KERNEL);
629 	if (!sigma_delta->slots)
630 		return -ENOMEM;
631 
632 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
633 					      &iio_pollfunc_store_time,
634 					      &ad_sd_trigger_handler,
635 					      &ad_sd_buffer_setup_ops);
636 	if (ret)
637 		return ret;
638 
639 	return devm_ad_sd_probe_trigger(dev, indio_dev);
640 }
641 EXPORT_SYMBOL_NS_GPL(devm_ad_sd_setup_buffer_and_trigger, IIO_AD_SIGMA_DELTA);
642 
643 /**
644  * ad_sd_init() - Initializes a ad_sigma_delta struct
645  * @sigma_delta: The ad_sigma_delta device
646  * @indio_dev: The IIO device which the Sigma Delta device is used for
647  * @spi: The SPI device for the ad_sigma_delta device
648  * @info: Device specific callbacks and options
649  *
650  * This function needs to be called before any other operations are performed on
651  * the ad_sigma_delta struct.
652  */
653 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
654 	struct spi_device *spi, const struct ad_sigma_delta_info *info)
655 {
656 	sigma_delta->spi = spi;
657 	sigma_delta->info = info;
658 
659 	/* If the field is unset in ad_sigma_delta_info, asume there can only be 1 slot. */
660 	if (!info->num_slots)
661 		sigma_delta->num_slots = 1;
662 	else
663 		sigma_delta->num_slots = info->num_slots;
664 
665 	if (sigma_delta->num_slots > 1) {
666 		if (!indio_dev->info->update_scan_mode) {
667 			dev_err(&spi->dev, "iio_dev lacks update_scan_mode().\n");
668 			return -EINVAL;
669 		}
670 
671 		if (!info->disable_all) {
672 			dev_err(&spi->dev, "ad_sigma_delta_info lacks disable_all().\n");
673 			return -EINVAL;
674 		}
675 	}
676 
677 	if (info->irq_line)
678 		sigma_delta->irq_line = info->irq_line;
679 	else
680 		sigma_delta->irq_line = spi->irq;
681 
682 	iio_device_set_drvdata(indio_dev, sigma_delta);
683 
684 	return 0;
685 }
686 EXPORT_SYMBOL_NS_GPL(ad_sd_init, IIO_AD_SIGMA_DELTA);
687 
688 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
689 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
690 MODULE_LICENSE("GPL v2");
691