1#include <linux/interrupt.h> 2#include <linux/irq.h> 3#include <linux/gpio.h> 4#include <linux/workqueue.h> 5#include <linux/mutex.h> 6#include <linux/device.h> 7#include <linux/kernel.h> 8#include <linux/spi/spi.h> 9#include <linux/slab.h> 10#include <linux/sysfs.h> 11#include <linux/list.h> 12 13#include "../iio.h" 14#include "../sysfs.h" 15#include "../ring_sw.h" 16#include "../accel/accel.h" 17#include "../trigger.h" 18#include "adis16260.h" 19 20static IIO_SCAN_EL_C(supply, ADIS16260_SCAN_SUPPLY, IIO_UNSIGNED(12), 21 ADIS16260_SUPPLY_OUT, NULL); 22static IIO_SCAN_EL_C(gyro, ADIS16260_SCAN_GYRO, IIO_SIGNED(14), 23 ADIS16260_GYRO_OUT, NULL); 24static IIO_SCAN_EL_C(aux_adc, ADIS16260_SCAN_AUX_ADC, IIO_SIGNED(14), 25 ADIS16260_AUX_ADC, NULL); 26static IIO_SCAN_EL_C(temp, ADIS16260_SCAN_TEMP, IIO_UNSIGNED(12), 27 ADIS16260_TEMP_OUT, NULL); 28static IIO_SCAN_EL_C(angl, ADIS16260_SCAN_ANGL, IIO_UNSIGNED(12), 29 ADIS16260_ANGL_OUT, NULL); 30 31static IIO_SCAN_EL_TIMESTAMP(5); 32 33static struct attribute *adis16260_scan_el_attrs[] = { 34 &iio_scan_el_supply.dev_attr.attr, 35 &iio_scan_el_gyro.dev_attr.attr, 36 &iio_scan_el_aux_adc.dev_attr.attr, 37 &iio_scan_el_temp.dev_attr.attr, 38 &iio_scan_el_angl.dev_attr.attr, 39 &iio_scan_el_timestamp.dev_attr.attr, 40 NULL, 41}; 42 43static struct attribute_group adis16260_scan_el_group = { 44 .attrs = adis16260_scan_el_attrs, 45 .name = "scan_elements", 46}; 47 48/** 49 * adis16260_poll_func_th() top half interrupt handler called by trigger 50 * @private_data: iio_dev 51 **/ 52static void adis16260_poll_func_th(struct iio_dev *indio_dev, s64 time) 53{ 54 struct adis16260_state *st = iio_dev_get_devdata(indio_dev); 55 st->last_timestamp = time; 56 schedule_work(&st->work_trigger_to_ring); 57} 58 59/** 60 * adis16260_read_ring_data() read data registers which will be placed into ring 61 * @dev: device associated with child of actual device (iio_dev or iio_trig) 62 * @rx: somewhere to pass back the value read 63 **/ 64static int adis16260_read_ring_data(struct device *dev, u8 *rx) 65{ 66 struct spi_message msg; 67 struct iio_dev *indio_dev = dev_get_drvdata(dev); 68 struct adis16260_state *st = iio_dev_get_devdata(indio_dev); 69 struct spi_transfer xfers[ADIS16260_OUTPUTS + 1]; 70 int ret; 71 int i; 72 73 mutex_lock(&st->buf_lock); 74 75 spi_message_init(&msg); 76 77 memset(xfers, 0, sizeof(xfers)); 78 for (i = 0; i <= ADIS16260_OUTPUTS; i++) { 79 xfers[i].bits_per_word = 8; 80 xfers[i].cs_change = 1; 81 xfers[i].len = 2; 82 xfers[i].delay_usecs = 30; 83 xfers[i].tx_buf = st->tx + 2 * i; 84 if (i < 2) /* SUPPLY_OUT:0x02 GYRO_OUT:0x04 */ 85 st->tx[2 * i] 86 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT 87 + 2 * i); 88 else /* 0x06 to 0x09 is reserved */ 89 st->tx[2 * i] 90 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT 91 + 2 * i + 4); 92 st->tx[2 * i + 1] = 0; 93 if (i >= 1) 94 xfers[i].rx_buf = rx + 2 * (i - 1); 95 spi_message_add_tail(&xfers[i], &msg); 96 } 97 98 ret = spi_sync(st->us, &msg); 99 if (ret) 100 dev_err(&st->us->dev, "problem when burst reading"); 101 102 mutex_unlock(&st->buf_lock); 103 104 return ret; 105} 106 107 108static void adis16260_trigger_bh_to_ring(struct work_struct *work_s) 109{ 110 struct adis16260_state *st 111 = container_of(work_s, struct adis16260_state, 112 work_trigger_to_ring); 113 114 int i = 0; 115 s16 *data; 116 size_t datasize = st->indio_dev 117 ->ring->access.get_bpd(st->indio_dev->ring); 118 119 data = kmalloc(datasize , GFP_KERNEL); 120 if (data == NULL) { 121 dev_err(&st->us->dev, "memory alloc failed in ring bh"); 122 return; 123 } 124 125 if (st->indio_dev->scan_count) 126 if (adis16260_read_ring_data(&st->indio_dev->dev, st->rx) >= 0) 127 for (; i < st->indio_dev->scan_count; i++) 128 data[i] = be16_to_cpup( 129 (__be16 *)&(st->rx[i*2])); 130 131 /* Guaranteed to be aligned with 8 byte boundary */ 132 if (st->indio_dev->scan_timestamp) 133 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp; 134 135 st->indio_dev->ring->access.store_to(st->indio_dev->ring, 136 (u8 *)data, 137 st->last_timestamp); 138 139 iio_trigger_notify_done(st->indio_dev->trig); 140 kfree(data); 141 142 return; 143} 144 145void adis16260_unconfigure_ring(struct iio_dev *indio_dev) 146{ 147 kfree(indio_dev->pollfunc); 148 iio_sw_rb_free(indio_dev->ring); 149} 150 151int adis16260_configure_ring(struct iio_dev *indio_dev) 152{ 153 int ret = 0; 154 struct adis16260_state *st = indio_dev->dev_data; 155 struct iio_ring_buffer *ring; 156 INIT_WORK(&st->work_trigger_to_ring, adis16260_trigger_bh_to_ring); 157 /* Set default scan mode */ 158 159 iio_scan_mask_set(indio_dev, iio_scan_el_supply.number); 160 iio_scan_mask_set(indio_dev, iio_scan_el_gyro.number); 161 iio_scan_mask_set(indio_dev, iio_scan_el_aux_adc.number); 162 iio_scan_mask_set(indio_dev, iio_scan_el_temp.number); 163 iio_scan_mask_set(indio_dev, iio_scan_el_angl.number); 164 indio_dev->scan_timestamp = true; 165 166 indio_dev->scan_el_attrs = &adis16260_scan_el_group; 167 168 ring = iio_sw_rb_allocate(indio_dev); 169 if (!ring) { 170 ret = -ENOMEM; 171 return ret; 172 } 173 indio_dev->ring = ring; 174 /* Effectively select the ring buffer implementation */ 175 iio_ring_sw_register_funcs(&ring->access); 176 ring->bpe = 2; 177 ring->preenable = &iio_sw_ring_preenable; 178 ring->postenable = &iio_triggered_ring_postenable; 179 ring->predisable = &iio_triggered_ring_predisable; 180 ring->owner = THIS_MODULE; 181 182 ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16260_poll_func_th); 183 if (ret) 184 goto error_iio_sw_rb_free; 185 186 indio_dev->modes |= INDIO_RING_TRIGGERED; 187 return 0; 188 189error_iio_sw_rb_free: 190 iio_sw_rb_free(indio_dev->ring); 191 return ret; 192} 193