1#include <linux/export.h> 2#include <linux/interrupt.h> 3#include <linux/mutex.h> 4#include <linux/kernel.h> 5#include <linux/spi/spi.h> 6#include <linux/slab.h> 7 8#include "../iio.h" 9#include "../ring_sw.h" 10#include "../trigger_consumer.h" 11#include "adis16240.h" 12 13/** 14 * adis16240_read_ring_data() read data registers which will be placed into ring 15 * @dev: device associated with child of actual device (iio_dev or iio_trig) 16 * @rx: somewhere to pass back the value read 17 **/ 18static int adis16240_read_ring_data(struct device *dev, u8 *rx) 19{ 20 struct spi_message msg; 21 struct iio_dev *indio_dev = dev_get_drvdata(dev); 22 struct adis16240_state *st = iio_priv(indio_dev); 23 struct spi_transfer xfers[ADIS16240_OUTPUTS + 1]; 24 int ret; 25 int i; 26 27 mutex_lock(&st->buf_lock); 28 29 spi_message_init(&msg); 30 31 memset(xfers, 0, sizeof(xfers)); 32 for (i = 0; i <= ADIS16240_OUTPUTS; i++) { 33 xfers[i].bits_per_word = 8; 34 xfers[i].cs_change = 1; 35 xfers[i].len = 2; 36 xfers[i].delay_usecs = 30; 37 xfers[i].tx_buf = st->tx + 2 * i; 38 st->tx[2 * i] 39 = ADIS16240_READ_REG(ADIS16240_SUPPLY_OUT + 2 * i); 40 st->tx[2 * i + 1] = 0; 41 if (i >= 1) 42 xfers[i].rx_buf = rx + 2 * (i - 1); 43 spi_message_add_tail(&xfers[i], &msg); 44 } 45 46 ret = spi_sync(st->us, &msg); 47 if (ret) 48 dev_err(&st->us->dev, "problem when burst reading"); 49 50 mutex_unlock(&st->buf_lock); 51 52 return ret; 53} 54 55static irqreturn_t adis16240_trigger_handler(int irq, void *p) 56{ 57 struct iio_poll_func *pf = p; 58 struct iio_dev *indio_dev = pf->indio_dev; 59 struct adis16240_state *st = iio_priv(indio_dev); 60 struct iio_buffer *ring = indio_dev->buffer; 61 62 int i = 0; 63 s16 *data; 64 size_t datasize = ring->access->get_bytes_per_datum(ring); 65 66 data = kmalloc(datasize, GFP_KERNEL); 67 if (data == NULL) { 68 dev_err(&st->us->dev, "memory alloc failed in ring bh"); 69 return -ENOMEM; 70 } 71 72 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength) && 73 adis16240_read_ring_data(&indio_dev->dev, st->rx) >= 0) 74 for (; i < bitmap_weight(indio_dev->active_scan_mask, 75 indio_dev->masklength); i++) 76 data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2])); 77 78 /* Guaranteed to be aligned with 8 byte boundary */ 79 if (ring->scan_timestamp) 80 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp; 81 82 ring->access->store_to(ring, (u8 *)data, pf->timestamp); 83 84 iio_trigger_notify_done(indio_dev->trig); 85 kfree(data); 86 87 return IRQ_HANDLED; 88} 89 90void adis16240_unconfigure_ring(struct iio_dev *indio_dev) 91{ 92 iio_dealloc_pollfunc(indio_dev->pollfunc); 93 iio_sw_rb_free(indio_dev->buffer); 94} 95 96static const struct iio_buffer_setup_ops adis16240_ring_setup_ops = { 97 .preenable = &iio_sw_buffer_preenable, 98 .postenable = &iio_triggered_buffer_postenable, 99 .predisable = &iio_triggered_buffer_predisable, 100}; 101 102int adis16240_configure_ring(struct iio_dev *indio_dev) 103{ 104 int ret = 0; 105 struct iio_buffer *ring; 106 107 ring = iio_sw_rb_allocate(indio_dev); 108 if (!ring) { 109 ret = -ENOMEM; 110 return ret; 111 } 112 indio_dev->buffer = ring; 113 ring->scan_timestamp = true; 114 indio_dev->setup_ops = &adis16240_ring_setup_ops; 115 116 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time, 117 &adis16240_trigger_handler, 118 IRQF_ONESHOT, 119 indio_dev, 120 "%s_consumer%d", 121 indio_dev->name, 122 indio_dev->id); 123 if (indio_dev->pollfunc == NULL) { 124 ret = -ENOMEM; 125 goto error_iio_sw_rb_free; 126 } 127 128 indio_dev->modes |= INDIO_BUFFER_TRIGGERED; 129 return 0; 130 131error_iio_sw_rb_free: 132 iio_sw_rb_free(indio_dev->buffer); 133 return ret; 134} 135