Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 1 | #include <linux/interrupt.h> |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 2 | #include <linux/mutex.h> |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 3 | #include <linux/kernel.h> |
| 4 | #include <linux/spi/spi.h> |
Mike Frysinger | 1cb6c1f | 2010-05-23 03:10:35 -0400 | [diff] [blame] | 5 | #include <linux/slab.h> |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 6 | #include <linux/bitops.h> |
Paul Gortmaker | 8e336a7 | 2011-07-10 13:09:12 -0400 | [diff] [blame^] | 7 | #include <linux/export.h> |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 8 | |
| 9 | #include "../iio.h" |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 10 | #include "../ring_sw.h" |
Jonathan Cameron | 3f72395 | 2011-08-24 17:28:39 +0100 | [diff] [blame] | 11 | #include "../trigger_consumer.h" |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 12 | #include "adis16400.h" |
| 13 | |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 14 | /** |
| 15 | * adis16400_spi_read_burst() - read all data registers |
| 16 | * @dev: device associated with child of actual device (iio_dev or iio_trig) |
| 17 | * @rx: somewhere to pass back the value read (min size is 24 bytes) |
| 18 | **/ |
| 19 | static int adis16400_spi_read_burst(struct device *dev, u8 *rx) |
| 20 | { |
| 21 | struct spi_message msg; |
| 22 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 23 | struct adis16400_state *st = iio_priv(indio_dev); |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 24 | u32 old_speed_hz = st->us->max_speed_hz; |
| 25 | int ret; |
| 26 | |
| 27 | struct spi_transfer xfers[] = { |
| 28 | { |
| 29 | .tx_buf = st->tx, |
| 30 | .bits_per_word = 8, |
| 31 | .len = 2, |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 32 | }, { |
| 33 | .rx_buf = rx, |
| 34 | .bits_per_word = 8, |
| 35 | .len = 24, |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 36 | }, |
| 37 | }; |
| 38 | |
| 39 | mutex_lock(&st->buf_lock); |
| 40 | st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD); |
| 41 | st->tx[1] = 0; |
| 42 | |
| 43 | spi_message_init(&msg); |
| 44 | spi_message_add_tail(&xfers[0], &msg); |
| 45 | spi_message_add_tail(&xfers[1], &msg); |
| 46 | |
| 47 | st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz); |
| 48 | spi_setup(st->us); |
| 49 | |
| 50 | ret = spi_sync(st->us, &msg); |
| 51 | if (ret) |
| 52 | dev_err(&st->us->dev, "problem when burst reading"); |
| 53 | |
| 54 | st->us->max_speed_hz = old_speed_hz; |
| 55 | spi_setup(st->us); |
| 56 | mutex_unlock(&st->buf_lock); |
| 57 | return ret; |
| 58 | } |
| 59 | |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 60 | static const u16 read_all_tx_array[] = { |
| 61 | cpu_to_be16(ADIS16400_READ_REG(ADIS16400_SUPPLY_OUT)), |
| 62 | cpu_to_be16(ADIS16400_READ_REG(ADIS16400_XGYRO_OUT)), |
| 63 | cpu_to_be16(ADIS16400_READ_REG(ADIS16400_YGYRO_OUT)), |
| 64 | cpu_to_be16(ADIS16400_READ_REG(ADIS16400_ZGYRO_OUT)), |
| 65 | cpu_to_be16(ADIS16400_READ_REG(ADIS16400_XACCL_OUT)), |
| 66 | cpu_to_be16(ADIS16400_READ_REG(ADIS16400_YACCL_OUT)), |
| 67 | cpu_to_be16(ADIS16400_READ_REG(ADIS16400_ZACCL_OUT)), |
| 68 | cpu_to_be16(ADIS16400_READ_REG(ADIS16350_XTEMP_OUT)), |
| 69 | cpu_to_be16(ADIS16400_READ_REG(ADIS16350_YTEMP_OUT)), |
| 70 | cpu_to_be16(ADIS16400_READ_REG(ADIS16350_ZTEMP_OUT)), |
| 71 | cpu_to_be16(ADIS16400_READ_REG(ADIS16400_AUX_ADC)), |
| 72 | }; |
| 73 | |
| 74 | static int adis16350_spi_read_all(struct device *dev, u8 *rx) |
| 75 | { |
| 76 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 77 | struct adis16400_state *st = iio_priv(indio_dev); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 78 | |
| 79 | struct spi_message msg; |
| 80 | int i, j = 0, ret; |
| 81 | struct spi_transfer *xfers; |
| 82 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 83 | xfers = kzalloc(sizeof(*xfers)*indio_dev->buffer->scan_count + 1, |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 84 | GFP_KERNEL); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 85 | if (xfers == NULL) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | for (i = 0; i < ARRAY_SIZE(read_all_tx_array); i++) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 89 | if (test_bit(i, indio_dev->buffer->scan_mask)) { |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 90 | xfers[j].tx_buf = &read_all_tx_array[i]; |
| 91 | xfers[j].bits_per_word = 16; |
| 92 | xfers[j].len = 2; |
| 93 | xfers[j + 1].rx_buf = rx + j*2; |
| 94 | j++; |
| 95 | } |
| 96 | xfers[j].bits_per_word = 16; |
| 97 | xfers[j].len = 2; |
| 98 | |
| 99 | spi_message_init(&msg); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 100 | for (j = 0; j < indio_dev->buffer->scan_count + 1; j++) |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 101 | spi_message_add_tail(&xfers[j], &msg); |
| 102 | |
| 103 | ret = spi_sync(st->us, &msg); |
| 104 | kfree(xfers); |
| 105 | |
| 106 | return ret; |
| 107 | } |
| 108 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 109 | /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device |
| 110 | * specific to be rolled into the core. |
| 111 | */ |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 112 | static irqreturn_t adis16400_trigger_handler(int irq, void *p) |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 113 | { |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 114 | struct iio_poll_func *pf = p; |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 115 | struct iio_dev *indio_dev = pf->indio_dev; |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 116 | struct adis16400_state *st = iio_priv(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 117 | struct iio_buffer *ring = indio_dev->buffer; |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 118 | int i = 0, j, ret = 0; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 119 | s16 *data; |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 120 | size_t datasize = ring->access->get_bytes_per_datum(ring); |
Jonathan Cameron | 32b5eec | 2011-09-02 17:14:38 +0100 | [diff] [blame] | 121 | /* Asumption that long is enough for maximum channels */ |
| 122 | unsigned long mask = *ring->scan_mask; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 123 | |
| 124 | data = kmalloc(datasize , GFP_KERNEL); |
| 125 | if (data == NULL) { |
| 126 | dev_err(&st->us->dev, "memory alloc failed in ring bh"); |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 127 | return -ENOMEM; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 130 | if (ring->scan_count) { |
| 131 | if (st->variant->flags & ADIS16400_NO_BURST) { |
| 132 | ret = adis16350_spi_read_all(&indio_dev->dev, st->rx); |
| 133 | if (ret < 0) |
Andre Bartke | 53aebb5 | 2011-06-02 00:21:45 +0200 | [diff] [blame] | 134 | goto err; |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 135 | for (; i < ring->scan_count; i++) |
| 136 | data[i] = *(s16 *)(st->rx + i*2); |
| 137 | } else { |
| 138 | ret = adis16400_spi_read_burst(&indio_dev->dev, st->rx); |
| 139 | if (ret < 0) |
Andre Bartke | 53aebb5 | 2011-06-02 00:21:45 +0200 | [diff] [blame] | 140 | goto err; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 141 | for (; i < indio_dev->buffer->scan_count; i++) { |
Michael Hennerich | 0fea4d6 | 2011-03-21 16:44:38 +0100 | [diff] [blame] | 142 | j = __ffs(mask); |
| 143 | mask &= ~(1 << j); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 144 | data[i] = be16_to_cpup( |
Michael Hennerich | 0fea4d6 | 2011-03-21 16:44:38 +0100 | [diff] [blame] | 145 | (__be16 *)&(st->rx[j*2])); |
| 146 | } |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 149 | /* Guaranteed to be aligned with 8 byte boundary */ |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 150 | if (ring->scan_timestamp) |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 151 | *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 152 | ring->access->store_to(indio_dev->buffer, (u8 *) data, pf->timestamp); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 153 | |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 154 | iio_trigger_notify_done(indio_dev->trig); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 155 | |
Andre Bartke | 53aebb5 | 2011-06-02 00:21:45 +0200 | [diff] [blame] | 156 | kfree(data); |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 157 | return IRQ_HANDLED; |
Andre Bartke | 53aebb5 | 2011-06-02 00:21:45 +0200 | [diff] [blame] | 158 | |
| 159 | err: |
| 160 | kfree(data); |
| 161 | return ret; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 162 | } |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 163 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 164 | void adis16400_unconfigure_ring(struct iio_dev *indio_dev) |
| 165 | { |
Jonathan Cameron | 21b185f | 2011-05-18 14:42:32 +0100 | [diff] [blame] | 166 | iio_dealloc_pollfunc(indio_dev->pollfunc); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 167 | iio_sw_rb_free(indio_dev->buffer); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 170 | static const struct iio_buffer_setup_ops adis16400_ring_setup_ops = { |
| 171 | .preenable = &iio_sw_buffer_preenable, |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 172 | .postenable = &iio_triggered_buffer_postenable, |
| 173 | .predisable = &iio_triggered_buffer_predisable, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 174 | }; |
| 175 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 176 | int adis16400_configure_ring(struct iio_dev *indio_dev) |
| 177 | { |
| 178 | int ret = 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 179 | struct iio_buffer *ring; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 180 | |
| 181 | ring = iio_sw_rb_allocate(indio_dev); |
| 182 | if (!ring) { |
| 183 | ret = -ENOMEM; |
| 184 | return ret; |
| 185 | } |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 186 | indio_dev->buffer = ring; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 187 | /* Effectively select the ring buffer implementation */ |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 188 | ring->access = &ring_sw_access_funcs; |
Jonathan Cameron | 43c11b4 | 2010-07-11 16:39:17 +0100 | [diff] [blame] | 189 | ring->bpe = 2; |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 190 | ring->scan_timestamp = true; |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 191 | ring->setup_ops = &adis16400_ring_setup_ops; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 192 | ring->owner = THIS_MODULE; |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 193 | |
Jonathan Cameron | 21b185f | 2011-05-18 14:42:32 +0100 | [diff] [blame] | 194 | indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time, |
| 195 | &adis16400_trigger_handler, |
| 196 | IRQF_ONESHOT, |
| 197 | indio_dev, |
| 198 | "%s_consumer%d", |
| 199 | indio_dev->name, |
| 200 | indio_dev->id); |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 201 | if (indio_dev->pollfunc == NULL) { |
| 202 | ret = -ENOMEM; |
Jonathan Cameron | 1574409 | 2010-07-11 16:39:09 +0100 | [diff] [blame] | 203 | goto error_iio_sw_rb_free; |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 204 | } |
Jonathan Cameron | 1574409 | 2010-07-11 16:39:09 +0100 | [diff] [blame] | 205 | |
Jonathan Cameron | ec3afa4 | 2011-09-21 11:15:54 +0100 | [diff] [blame] | 206 | indio_dev->modes |= INDIO_BUFFER_TRIGGERED; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 207 | return 0; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 208 | error_iio_sw_rb_free: |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 209 | iio_sw_rb_free(indio_dev->buffer); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 210 | return ret; |
| 211 | } |