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 <linux/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_STATUS	0x00
33 #define AD_SD_REG_DATA		0x03
34 
35 #define AD_SD_REG_STATUS_RDY	0x80
36 
37 /**
38  * ad_sd_set_comm() - Set communications register
39  *
40  * @sigma_delta: The sigma delta device
41  * @comm: New value for the communications register
42  */
ad_sd_set_comm(struct ad_sigma_delta * sigma_delta,uint8_t comm)43 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm)
44 {
45 	/* Some variants use the lower two bits of the communications register
46 	 * to select the channel */
47 	sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK;
48 }
49 EXPORT_SYMBOL_NS_GPL(ad_sd_set_comm, "IIO_AD_SIGMA_DELTA");
50 
51 /**
52  * ad_sd_write_reg() - Write a register
53  *
54  * @sigma_delta: The sigma delta device
55  * @reg: Address of the register
56  * @size: Size of the register (0-3)
57  * @val: Value to write to the register
58  *
59  * Returns 0 on success, an error code otherwise.
60  **/
ad_sd_write_reg(struct ad_sigma_delta * sigma_delta,unsigned int reg,unsigned int size,unsigned int val)61 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
62 	unsigned int size, unsigned int val)
63 {
64 	uint8_t *data = sigma_delta->tx_buf;
65 	struct spi_transfer t = {
66 		.tx_buf		= data,
67 		.len		= size + 1,
68 		.cs_change	= sigma_delta->keep_cs_asserted,
69 	};
70 	struct spi_message m;
71 	int ret;
72 
73 	data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm;
74 
75 	switch (size) {
76 	case 3:
77 		put_unaligned_be24(val, &data[1]);
78 		break;
79 	case 2:
80 		put_unaligned_be16(val, &data[1]);
81 		break;
82 	case 1:
83 		data[1] = val;
84 		break;
85 	case 0:
86 		break;
87 	default:
88 		return -EINVAL;
89 	}
90 
91 	spi_message_init(&m);
92 	spi_message_add_tail(&t, &m);
93 
94 	if (sigma_delta->bus_locked)
95 		ret = spi_sync_locked(sigma_delta->spi, &m);
96 	else
97 		ret = spi_sync(sigma_delta->spi, &m);
98 
99 	return ret;
100 }
101 EXPORT_SYMBOL_NS_GPL(ad_sd_write_reg, "IIO_AD_SIGMA_DELTA");
102 
ad_sd_read_reg_raw(struct ad_sigma_delta * sigma_delta,unsigned int reg,unsigned int size,uint8_t * val)103 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
104 	unsigned int reg, unsigned int size, uint8_t *val)
105 {
106 	uint8_t *data = sigma_delta->tx_buf;
107 	int ret;
108 	struct spi_transfer t[] = {
109 		{
110 			.tx_buf = data,
111 			.len = 1,
112 		}, {
113 			.rx_buf = val,
114 			.len = size,
115 			.cs_change = sigma_delta->keep_cs_asserted,
116 		},
117 	};
118 	struct spi_message m;
119 
120 	spi_message_init(&m);
121 
122 	if (sigma_delta->info->has_registers) {
123 		data[0] = reg << sigma_delta->info->addr_shift;
124 		data[0] |= sigma_delta->info->read_mask;
125 		data[0] |= sigma_delta->comm;
126 		spi_message_add_tail(&t[0], &m);
127 	}
128 	spi_message_add_tail(&t[1], &m);
129 
130 	if (sigma_delta->bus_locked)
131 		ret = spi_sync_locked(sigma_delta->spi, &m);
132 	else
133 		ret = spi_sync(sigma_delta->spi, &m);
134 
135 	return ret;
136 }
137 
138 /**
139  * ad_sd_read_reg() - Read a register
140  *
141  * @sigma_delta: The sigma delta device
142  * @reg: Address of the register
143  * @size: Size of the register (1-4)
144  * @val: Read value
145  *
146  * Returns 0 on success, an error code otherwise.
147  **/
ad_sd_read_reg(struct ad_sigma_delta * sigma_delta,unsigned int reg,unsigned int size,unsigned int * val)148 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
149 	unsigned int reg, unsigned int size, unsigned int *val)
150 {
151 	int ret;
152 
153 	ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->rx_buf);
154 	if (ret < 0)
155 		goto out;
156 
157 	switch (size) {
158 	case 4:
159 		*val = get_unaligned_be32(sigma_delta->rx_buf);
160 		break;
161 	case 3:
162 		*val = get_unaligned_be24(sigma_delta->rx_buf);
163 		break;
164 	case 2:
165 		*val = get_unaligned_be16(sigma_delta->rx_buf);
166 		break;
167 	case 1:
168 		*val = sigma_delta->rx_buf[0];
169 		break;
170 	default:
171 		ret = -EINVAL;
172 		break;
173 	}
174 
175 out:
176 	return ret;
177 }
178 EXPORT_SYMBOL_NS_GPL(ad_sd_read_reg, "IIO_AD_SIGMA_DELTA");
179 
180 /**
181  * ad_sd_reset() - Reset the serial interface
182  *
183  * @sigma_delta: The sigma delta device
184  *
185  * Returns 0 on success, an error code otherwise.
186  **/
ad_sd_reset(struct ad_sigma_delta * sigma_delta)187 int ad_sd_reset(struct ad_sigma_delta *sigma_delta)
188 {
189 	unsigned int reset_length = sigma_delta->info->num_resetclks;
190 	uint8_t *buf;
191 	unsigned int size;
192 	int ret;
193 
194 	size = DIV_ROUND_UP(reset_length, 8);
195 	buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
196 	if (!buf)
197 		return -ENOMEM;
198 
199 	memset(buf, 0xff, size);
200 	ret = spi_write(sigma_delta->spi, buf, size);
201 	kfree(buf);
202 
203 	return ret;
204 }
205 EXPORT_SYMBOL_NS_GPL(ad_sd_reset, "IIO_AD_SIGMA_DELTA");
206 
ad_sd_disable_irq(struct ad_sigma_delta * sigma_delta)207 static bool ad_sd_disable_irq(struct ad_sigma_delta *sigma_delta)
208 {
209 	guard(spinlock_irqsave)(&sigma_delta->irq_lock);
210 
211 	/* It's already off, return false to indicate nothing was changed */
212 	if (sigma_delta->irq_dis)
213 		return false;
214 
215 	sigma_delta->irq_dis = true;
216 	disable_irq_nosync(sigma_delta->irq_line);
217 	return true;
218 }
219 
ad_sd_enable_irq(struct ad_sigma_delta * sigma_delta)220 static void ad_sd_enable_irq(struct ad_sigma_delta *sigma_delta)
221 {
222 	guard(spinlock_irqsave)(&sigma_delta->irq_lock);
223 
224 	sigma_delta->irq_dis = false;
225 	enable_irq(sigma_delta->irq_line);
226 }
227 
228 #define AD_SD_CLEAR_DATA_BUFLEN 9
229 
230 /* Called with `sigma_delta->bus_locked == true` only. */
ad_sigma_delta_clear_pending_event(struct ad_sigma_delta * sigma_delta)231 static int ad_sigma_delta_clear_pending_event(struct ad_sigma_delta *sigma_delta)
232 {
233 	bool pending_event;
234 	unsigned int data_read_len = BITS_TO_BYTES(sigma_delta->info->num_resetclks);
235 	u8 *data;
236 	struct spi_transfer t[] = {
237 		{
238 			.len = 1,
239 		}, {
240 			.len = data_read_len,
241 		}
242 	};
243 	struct spi_message m;
244 	int ret;
245 
246 	/*
247 	 * Read R̅D̅Y̅ pin (if possible) or status register to check if there is an
248 	 * old event.
249 	 */
250 	if (sigma_delta->rdy_gpiod) {
251 		pending_event = gpiod_get_value(sigma_delta->rdy_gpiod);
252 	} else {
253 		unsigned int status_reg;
254 
255 		ret = ad_sd_read_reg(sigma_delta, AD_SD_REG_STATUS, 1, &status_reg);
256 		if (ret)
257 			return ret;
258 
259 		pending_event = !(status_reg & AD_SD_REG_STATUS_RDY);
260 	}
261 
262 	if (!pending_event)
263 		return 0;
264 
265 	/*
266 	 * In general the size of the data register is unknown. It varies from
267 	 * device to device, might be one byte longer if CONTROL.DATA_STATUS is
268 	 * set and even varies on some devices depending on which input is
269 	 * selected. So send one byte to start reading the data register and
270 	 * then just clock for some bytes with DIN (aka MOSI) high to not
271 	 * confuse the register access state machine after the data register was
272 	 * completely read. Note however that the sequence length must be
273 	 * shorter than the reset procedure.
274 	 */
275 
276 	data = kzalloc(data_read_len + 1, GFP_KERNEL);
277 	if (!data)
278 		return -ENOMEM;
279 
280 	spi_message_init(&m);
281 	if (sigma_delta->info->has_registers) {
282 		unsigned int data_reg = sigma_delta->info->data_reg ?: AD_SD_REG_DATA;
283 
284 		data[0] = data_reg << sigma_delta->info->addr_shift;
285 		data[0] |= sigma_delta->info->read_mask;
286 		data[0] |= sigma_delta->comm;
287 		t[0].tx_buf = data;
288 		spi_message_add_tail(&t[0], &m);
289 	}
290 
291 	/*
292 	 * The first transferred byte is part of the real data register,
293 	 * so this doesn't need to be 0xff. In the remaining
294 	 * `data_read_len - 1` bytes are less than $num_resetclks ones.
295 	 */
296 	t[1].tx_buf = data + 1;
297 	data[1] = 0x00;
298 	memset(data + 2, 0xff, data_read_len - 1);
299 	spi_message_add_tail(&t[1], &m);
300 
301 	ret = spi_sync_locked(sigma_delta->spi, &m);
302 
303 	kfree(data);
304 
305 	return ret;
306 }
307 
ad_sd_calibrate(struct ad_sigma_delta * sigma_delta,unsigned int mode,unsigned int channel)308 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
309 	unsigned int mode, unsigned int channel)
310 {
311 	int ret;
312 	unsigned long time_left;
313 
314 	ret = ad_sigma_delta_set_channel(sigma_delta, channel);
315 	if (ret)
316 		return ret;
317 
318 	spi_bus_lock(sigma_delta->spi->controller);
319 	sigma_delta->bus_locked = true;
320 	sigma_delta->keep_cs_asserted = true;
321 	reinit_completion(&sigma_delta->completion);
322 
323 	ret = ad_sigma_delta_clear_pending_event(sigma_delta);
324 	if (ret)
325 		goto out;
326 
327 	ret = ad_sigma_delta_set_mode(sigma_delta, mode);
328 	if (ret < 0)
329 		goto out;
330 
331 	ad_sd_enable_irq(sigma_delta);
332 	time_left = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ);
333 	if (time_left == 0) {
334 		ad_sd_disable_irq(sigma_delta);
335 		ret = -EIO;
336 	} else {
337 		ret = 0;
338 	}
339 out:
340 	sigma_delta->keep_cs_asserted = false;
341 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
342 	ad_sigma_delta_disable_one(sigma_delta, channel);
343 	sigma_delta->bus_locked = false;
344 	spi_bus_unlock(sigma_delta->spi->controller);
345 
346 	return ret;
347 }
348 EXPORT_SYMBOL_NS_GPL(ad_sd_calibrate, "IIO_AD_SIGMA_DELTA");
349 
350 /**
351  * ad_sd_calibrate_all() - Performs channel calibration
352  * @sigma_delta: The sigma delta device
353  * @cb: Array of channels and calibration type to perform
354  * @n: Number of items in cb
355  *
356  * Returns 0 on success, an error code otherwise.
357  **/
ad_sd_calibrate_all(struct ad_sigma_delta * sigma_delta,const struct ad_sd_calib_data * cb,unsigned int n)358 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
359 	const struct ad_sd_calib_data *cb, unsigned int n)
360 {
361 	unsigned int i;
362 	int ret;
363 
364 	for (i = 0; i < n; i++) {
365 		ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
366 		if (ret)
367 			return ret;
368 	}
369 
370 	return 0;
371 }
372 EXPORT_SYMBOL_NS_GPL(ad_sd_calibrate_all, "IIO_AD_SIGMA_DELTA");
373 
374 /**
375  * ad_sigma_delta_single_conversion() - Performs a single data conversion
376  * @indio_dev: The IIO device
377  * @chan: The conversion is done for this channel
378  * @val: Pointer to the location where to store the read value
379  *
380  * Returns: 0 on success, an error value otherwise.
381  */
ad_sigma_delta_single_conversion(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val)382 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
383 	const struct iio_chan_spec *chan, int *val)
384 {
385 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
386 	unsigned int sample, raw_sample;
387 	unsigned int data_reg;
388 	int ret = 0;
389 
390 	if (!iio_device_claim_direct(indio_dev))
391 		return -EBUSY;
392 
393 	ret = ad_sigma_delta_set_channel(sigma_delta, chan->address);
394 	if (ret)
395 		goto out_release;
396 
397 	spi_bus_lock(sigma_delta->spi->controller);
398 	sigma_delta->bus_locked = true;
399 	sigma_delta->keep_cs_asserted = true;
400 	reinit_completion(&sigma_delta->completion);
401 
402 	ret = ad_sigma_delta_clear_pending_event(sigma_delta);
403 	if (ret)
404 		goto out_unlock;
405 
406 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
407 
408 	ad_sd_enable_irq(sigma_delta);
409 	ret = wait_for_completion_interruptible_timeout(
410 			&sigma_delta->completion, HZ);
411 
412 	if (ret == 0)
413 		ret = -EIO;
414 	if (ret < 0)
415 		goto out;
416 
417 	if (sigma_delta->info->data_reg != 0)
418 		data_reg = sigma_delta->info->data_reg;
419 	else
420 		data_reg = AD_SD_REG_DATA;
421 
422 	ret = ad_sd_read_reg(sigma_delta, data_reg,
423 		DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
424 		&raw_sample);
425 
426 out:
427 	ad_sd_disable_irq(sigma_delta);
428 
429 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
430 	ad_sigma_delta_disable_one(sigma_delta, chan->address);
431 
432 out_unlock:
433 	sigma_delta->keep_cs_asserted = false;
434 	sigma_delta->bus_locked = false;
435 	spi_bus_unlock(sigma_delta->spi->controller);
436 out_release:
437 	iio_device_release_direct(indio_dev);
438 
439 	if (ret)
440 		return ret;
441 
442 	sample = raw_sample >> chan->scan_type.shift;
443 	sample &= (1 << chan->scan_type.realbits) - 1;
444 	*val = sample;
445 
446 	ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
447 	if (ret)
448 		return ret;
449 
450 	return IIO_VAL_INT;
451 }
452 EXPORT_SYMBOL_NS_GPL(ad_sigma_delta_single_conversion, "IIO_AD_SIGMA_DELTA");
453 
ad_sd_buffer_postenable(struct iio_dev * indio_dev)454 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
455 {
456 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
457 	unsigned int i, slot, samples_buf_size;
458 	unsigned int channel;
459 	uint8_t *samples_buf;
460 	int ret;
461 
462 	if (sigma_delta->num_slots == 1) {
463 		channel = find_first_bit(indio_dev->active_scan_mask,
464 					 iio_get_masklength(indio_dev));
465 		ret = ad_sigma_delta_set_channel(sigma_delta,
466 						 indio_dev->channels[channel].address);
467 		if (ret)
468 			return ret;
469 		slot = 1;
470 	} else {
471 		/*
472 		 * At this point update_scan_mode already enabled the required channels.
473 		 * For sigma-delta sequencer drivers with multiple slots, an update_scan_mode
474 		 * implementation is mandatory.
475 		 */
476 		slot = 0;
477 		iio_for_each_active_channel(indio_dev, i) {
478 			sigma_delta->slots[slot] = indio_dev->channels[i].address;
479 			slot++;
480 		}
481 	}
482 
483 	sigma_delta->active_slots = slot;
484 	sigma_delta->current_slot = 0;
485 
486 	if (sigma_delta->active_slots > 1) {
487 		ret = ad_sigma_delta_append_status(sigma_delta, true);
488 		if (ret)
489 			return ret;
490 	}
491 
492 	samples_buf_size = ALIGN(slot * indio_dev->channels[0].scan_type.storagebits, 8);
493 	samples_buf_size += sizeof(int64_t);
494 	samples_buf = devm_krealloc(&sigma_delta->spi->dev, sigma_delta->samples_buf,
495 				    samples_buf_size, GFP_KERNEL);
496 	if (!samples_buf)
497 		return -ENOMEM;
498 
499 	sigma_delta->samples_buf = samples_buf;
500 
501 	spi_bus_lock(sigma_delta->spi->controller);
502 	sigma_delta->bus_locked = true;
503 	sigma_delta->keep_cs_asserted = true;
504 
505 	ret = ad_sigma_delta_clear_pending_event(sigma_delta);
506 	if (ret)
507 		goto err_unlock;
508 
509 	ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
510 	if (ret)
511 		goto err_unlock;
512 
513 	ad_sd_enable_irq(sigma_delta);
514 
515 	return 0;
516 
517 err_unlock:
518 	spi_bus_unlock(sigma_delta->spi->controller);
519 
520 	return ret;
521 }
522 
ad_sd_buffer_postdisable(struct iio_dev * indio_dev)523 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
524 {
525 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
526 
527 	reinit_completion(&sigma_delta->completion);
528 	wait_for_completion_timeout(&sigma_delta->completion, HZ);
529 
530 	ad_sd_disable_irq(sigma_delta);
531 
532 	sigma_delta->keep_cs_asserted = false;
533 	ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
534 
535 	if (sigma_delta->status_appended)
536 		ad_sigma_delta_append_status(sigma_delta, false);
537 
538 	ad_sigma_delta_disable_all(sigma_delta);
539 	sigma_delta->bus_locked = false;
540 	return spi_bus_unlock(sigma_delta->spi->controller);
541 }
542 
ad_sd_trigger_handler(int irq,void * p)543 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
544 {
545 	struct iio_poll_func *pf = p;
546 	struct iio_dev *indio_dev = pf->indio_dev;
547 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
548 	uint8_t *data = sigma_delta->rx_buf;
549 	unsigned int transfer_size;
550 	unsigned int sample_size;
551 	unsigned int sample_pos;
552 	unsigned int status_pos;
553 	unsigned int reg_size;
554 	unsigned int data_reg;
555 
556 	reg_size = indio_dev->channels[0].scan_type.realbits +
557 			indio_dev->channels[0].scan_type.shift;
558 	reg_size = DIV_ROUND_UP(reg_size, 8);
559 
560 	if (sigma_delta->info->data_reg != 0)
561 		data_reg = sigma_delta->info->data_reg;
562 	else
563 		data_reg = AD_SD_REG_DATA;
564 
565 	/* Status word will be appended to the sample during transfer */
566 	if (sigma_delta->status_appended)
567 		transfer_size = reg_size + 1;
568 	else
569 		transfer_size = reg_size;
570 
571 	switch (reg_size) {
572 	case 4:
573 	case 2:
574 	case 1:
575 		status_pos = reg_size;
576 		ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[0]);
577 		break;
578 	case 3:
579 		/*
580 		 * Data array after transfer will look like (if status is appended):
581 		 * data[] = { [0][sample][sample][sample][status] }
582 		 * Keeping the first byte 0 shifts the status position by 1 byte to the right.
583 		 */
584 		status_pos = reg_size + 1;
585 
586 		/* We store 24 bit samples in a 32 bit word. Keep the upper
587 		 * byte set to zero. */
588 		ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[1]);
589 		break;
590 	}
591 
592 	/*
593 	 * For devices sampling only one channel at
594 	 * once, there is no need for sample number tracking.
595 	 */
596 	if (sigma_delta->active_slots == 1) {
597 		iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
598 		goto irq_handled;
599 	}
600 
601 	if (sigma_delta->status_appended) {
602 		u8 converted_channel;
603 
604 		converted_channel = data[status_pos] & sigma_delta->info->status_ch_mask;
605 		if (converted_channel != sigma_delta->slots[sigma_delta->current_slot]) {
606 			/*
607 			 * Desync occurred during continuous sampling of multiple channels.
608 			 * Drop this incomplete sample and start from first channel again.
609 			 */
610 
611 			sigma_delta->current_slot = 0;
612 			goto irq_handled;
613 		}
614 	}
615 
616 	sample_size = indio_dev->channels[0].scan_type.storagebits / 8;
617 	sample_pos = sample_size * sigma_delta->current_slot;
618 	memcpy(&sigma_delta->samples_buf[sample_pos], data, sample_size);
619 	sigma_delta->current_slot++;
620 
621 	if (sigma_delta->current_slot == sigma_delta->active_slots) {
622 		sigma_delta->current_slot = 0;
623 		iio_push_to_buffers_with_timestamp(indio_dev, sigma_delta->samples_buf,
624 						   pf->timestamp);
625 	}
626 
627 irq_handled:
628 	iio_trigger_notify_done(indio_dev->trig);
629 	ad_sd_enable_irq(sigma_delta);
630 
631 	return IRQ_HANDLED;
632 }
633 
ad_sd_validate_scan_mask(struct iio_dev * indio_dev,const unsigned long * mask)634 static bool ad_sd_validate_scan_mask(struct iio_dev *indio_dev, const unsigned long *mask)
635 {
636 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
637 
638 	return bitmap_weight(mask, iio_get_masklength(indio_dev)) <= sigma_delta->num_slots;
639 }
640 
641 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
642 	.postenable = &ad_sd_buffer_postenable,
643 	.postdisable = &ad_sd_buffer_postdisable,
644 	.validate_scan_mask = &ad_sd_validate_scan_mask,
645 };
646 
ad_sd_data_rdy_trig_poll(int irq,void * private)647 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
648 {
649 	struct ad_sigma_delta *sigma_delta = private;
650 
651 	/*
652 	 * AD7124 and a few others use the same physical line for interrupt
653 	 * reporting (R̅D̅Y̅) and MISO.
654 	 * As MISO toggles when reading a register, this likely results in a
655 	 * pending interrupt. This has two consequences: a) The irq might
656 	 * trigger immediately after it's enabled even though the conversion
657 	 * isn't done yet; and b) checking the STATUS register's R̅D̅Y̅ flag is
658 	 * off-limits as reading that would trigger another irq event.
659 	 *
660 	 * So read the MOSI line as GPIO (if available) and only trigger the irq
661 	 * if the line is active. Without such a GPIO assume this is a valid
662 	 * interrupt.
663 	 *
664 	 * Also as disable_irq_nosync() is used to disable the irq, only act if
665 	 * the irq wasn't disabled before.
666 	 */
667 	if ((!sigma_delta->rdy_gpiod || gpiod_get_value(sigma_delta->rdy_gpiod)) &&
668 	    ad_sd_disable_irq(sigma_delta)) {
669 		complete(&sigma_delta->completion);
670 		iio_trigger_poll(sigma_delta->trig);
671 
672 		return IRQ_HANDLED;
673 	}
674 
675 	return IRQ_NONE;
676 }
677 
678 /**
679  * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
680  * @indio_dev: The IIO device
681  * @trig: The new trigger
682  *
683  * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
684  * device, -EINVAL otherwise.
685  */
ad_sd_validate_trigger(struct iio_dev * indio_dev,struct iio_trigger * trig)686 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
687 {
688 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
689 
690 	if (sigma_delta->trig != trig)
691 		return -EINVAL;
692 
693 	return 0;
694 }
695 EXPORT_SYMBOL_NS_GPL(ad_sd_validate_trigger, "IIO_AD_SIGMA_DELTA");
696 
devm_ad_sd_probe_trigger(struct device * dev,struct iio_dev * indio_dev)697 static int devm_ad_sd_probe_trigger(struct device *dev, struct iio_dev *indio_dev)
698 {
699 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
700 	unsigned long irq_flags = irq_get_trigger_type(sigma_delta->irq_line);
701 	int ret;
702 
703 	if (dev != &sigma_delta->spi->dev) {
704 		dev_err(dev, "Trigger parent should be '%s', got '%s'\n",
705 			dev_name(dev), dev_name(&sigma_delta->spi->dev));
706 		return -EFAULT;
707 	}
708 
709 	sigma_delta->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
710 						   iio_device_id(indio_dev));
711 	if (sigma_delta->trig == NULL)
712 		return -ENOMEM;
713 
714 	init_completion(&sigma_delta->completion);
715 
716 	sigma_delta->irq_dis = true;
717 
718 	/* the IRQ core clears IRQ_DISABLE_UNLAZY flag when freeing an IRQ */
719 	irq_set_status_flags(sigma_delta->irq_line, IRQ_DISABLE_UNLAZY);
720 
721 	/* Allow overwriting the flags from firmware */
722 	if (!irq_flags)
723 		irq_flags = sigma_delta->info->irq_flags;
724 
725 	ret = devm_request_irq(dev, sigma_delta->irq_line,
726 			       ad_sd_data_rdy_trig_poll,
727 			       irq_flags | IRQF_NO_AUTOEN,
728 			       indio_dev->name,
729 			       sigma_delta);
730 	if (ret)
731 		return ret;
732 
733 	iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
734 
735 	ret = devm_iio_trigger_register(dev, sigma_delta->trig);
736 	if (ret)
737 		return ret;
738 
739 	/* select default trigger */
740 	indio_dev->trig = iio_trigger_get(sigma_delta->trig);
741 
742 	return 0;
743 }
744 
745 /**
746  * devm_ad_sd_setup_buffer_and_trigger() - Device-managed buffer & trigger setup
747  * @dev: Device object to which to bind the life-time of the resources attached
748  * @indio_dev: The IIO device
749  */
devm_ad_sd_setup_buffer_and_trigger(struct device * dev,struct iio_dev * indio_dev)750 int devm_ad_sd_setup_buffer_and_trigger(struct device *dev, struct iio_dev *indio_dev)
751 {
752 	struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
753 	int ret;
754 
755 	sigma_delta->slots = devm_kcalloc(dev, sigma_delta->num_slots,
756 					  sizeof(*sigma_delta->slots), GFP_KERNEL);
757 	if (!sigma_delta->slots)
758 		return -ENOMEM;
759 
760 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
761 					      &iio_pollfunc_store_time,
762 					      &ad_sd_trigger_handler,
763 					      &ad_sd_buffer_setup_ops);
764 	if (ret)
765 		return ret;
766 
767 	return devm_ad_sd_probe_trigger(dev, indio_dev);
768 }
769 EXPORT_SYMBOL_NS_GPL(devm_ad_sd_setup_buffer_and_trigger, "IIO_AD_SIGMA_DELTA");
770 
771 /**
772  * ad_sd_init() - Initializes a ad_sigma_delta struct
773  * @sigma_delta: The ad_sigma_delta device
774  * @indio_dev: The IIO device which the Sigma Delta device is used for
775  * @spi: The SPI device for the ad_sigma_delta device
776  * @info: Device specific callbacks and options
777  *
778  * This function needs to be called before any other operations are performed on
779  * the ad_sigma_delta struct.
780  */
ad_sd_init(struct ad_sigma_delta * sigma_delta,struct iio_dev * indio_dev,struct spi_device * spi,const struct ad_sigma_delta_info * info)781 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
782 	struct spi_device *spi, const struct ad_sigma_delta_info *info)
783 {
784 	sigma_delta->spi = spi;
785 	sigma_delta->info = info;
786 
787 	/* If the field is unset in ad_sigma_delta_info, assume there can only be 1 slot. */
788 	if (!info->num_slots)
789 		sigma_delta->num_slots = 1;
790 	else
791 		sigma_delta->num_slots = info->num_slots;
792 
793 	if (sigma_delta->num_slots > 1) {
794 		if (!indio_dev->info->update_scan_mode) {
795 			dev_err(&spi->dev, "iio_dev lacks update_scan_mode().\n");
796 			return -EINVAL;
797 		}
798 
799 		if (!info->disable_all) {
800 			dev_err(&spi->dev, "ad_sigma_delta_info lacks disable_all().\n");
801 			return -EINVAL;
802 		}
803 	}
804 
805 	spin_lock_init(&sigma_delta->irq_lock);
806 
807 	if (info->has_named_irqs) {
808 		sigma_delta->irq_line = fwnode_irq_get_byname(dev_fwnode(&spi->dev),
809 							      "rdy");
810 		if (sigma_delta->irq_line < 0)
811 			return dev_err_probe(&spi->dev, sigma_delta->irq_line,
812 					     "Interrupt 'rdy' is required\n");
813 	} else {
814 		sigma_delta->irq_line = spi->irq;
815 	}
816 
817 	sigma_delta->rdy_gpiod = devm_gpiod_get_optional(&spi->dev, "rdy", GPIOD_IN);
818 	if (IS_ERR(sigma_delta->rdy_gpiod))
819 		return dev_err_probe(&spi->dev, PTR_ERR(sigma_delta->rdy_gpiod),
820 				     "Failed to find rdy gpio\n");
821 
822 	if (sigma_delta->rdy_gpiod && !sigma_delta->irq_line) {
823 		sigma_delta->irq_line = gpiod_to_irq(sigma_delta->rdy_gpiod);
824 		if (sigma_delta->irq_line < 0)
825 			return sigma_delta->irq_line;
826 	}
827 
828 	iio_device_set_drvdata(indio_dev, sigma_delta);
829 
830 	return 0;
831 }
832 EXPORT_SYMBOL_NS_GPL(ad_sd_init, "IIO_AD_SIGMA_DELTA");
833 
834 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
835 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
836 MODULE_LICENSE("GPL v2");
837