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