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