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 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 9 | #include <linux/iio/iio.h> |
Lars-Peter Clausen | 1ffe2e7 | 2013-01-16 12:48:00 +0000 | [diff] [blame^] | 10 | #include <linux/iio/buffer.h> |
| 11 | #include <linux/iio/triggered_buffer.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 12 | #include <linux/iio/trigger_consumer.h> |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 13 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 14 | #include "adis16400.h" |
| 15 | |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 16 | /** |
| 17 | * adis16400_spi_read_burst() - read all data registers |
Lars-Peter Clausen | ea19ed3 | 2012-05-12 15:39:41 +0200 | [diff] [blame] | 18 | * @indio_dev: the IIO device |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 19 | * @rx: somewhere to pass back the value read (min size is 24 bytes) |
| 20 | **/ |
Lars-Peter Clausen | ea19ed3 | 2012-05-12 15:39:41 +0200 | [diff] [blame] | 21 | static int adis16400_spi_read_burst(struct iio_dev *indio_dev, u8 *rx) |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 22 | { |
| 23 | struct spi_message msg; |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 24 | struct adis16400_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 25 | u32 old_speed_hz = st->adis.spi->max_speed_hz; |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 26 | int ret; |
| 27 | |
| 28 | struct spi_transfer xfers[] = { |
| 29 | { |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 30 | .tx_buf = st->adis.tx, |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 31 | .bits_per_word = 8, |
| 32 | .len = 2, |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 33 | }, { |
| 34 | .rx_buf = rx, |
| 35 | .bits_per_word = 8, |
| 36 | .len = 24, |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 37 | }, |
| 38 | }; |
| 39 | |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 40 | mutex_lock(&st->adis.txrx_lock); |
| 41 | st->adis.tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD); |
| 42 | st->adis.tx[1] = 0; |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 43 | |
| 44 | spi_message_init(&msg); |
| 45 | spi_message_add_tail(&xfers[0], &msg); |
| 46 | spi_message_add_tail(&xfers[1], &msg); |
| 47 | |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 48 | st->adis.spi->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz); |
| 49 | spi_setup(st->adis.spi); |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 50 | |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 51 | ret = spi_sync(st->adis.spi, &msg); |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 52 | if (ret) |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 53 | dev_err(&st->adis.spi->dev, "problem when burst reading"); |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 54 | |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 55 | st->adis.spi->max_speed_hz = old_speed_hz; |
| 56 | spi_setup(st->adis.spi); |
| 57 | mutex_unlock(&st->adis.txrx_lock); |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 58 | return ret; |
| 59 | } |
| 60 | |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 61 | static const u16 read_all_tx_array[] = { |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 62 | cpu_to_be16(ADIS_READ_REG(ADIS16400_SUPPLY_OUT)), |
| 63 | cpu_to_be16(ADIS_READ_REG(ADIS16400_XGYRO_OUT)), |
| 64 | cpu_to_be16(ADIS_READ_REG(ADIS16400_YGYRO_OUT)), |
| 65 | cpu_to_be16(ADIS_READ_REG(ADIS16400_ZGYRO_OUT)), |
| 66 | cpu_to_be16(ADIS_READ_REG(ADIS16400_XACCL_OUT)), |
| 67 | cpu_to_be16(ADIS_READ_REG(ADIS16400_YACCL_OUT)), |
| 68 | cpu_to_be16(ADIS_READ_REG(ADIS16400_ZACCL_OUT)), |
| 69 | cpu_to_be16(ADIS_READ_REG(ADIS16350_XTEMP_OUT)), |
| 70 | cpu_to_be16(ADIS_READ_REG(ADIS16350_YTEMP_OUT)), |
| 71 | cpu_to_be16(ADIS_READ_REG(ADIS16350_ZTEMP_OUT)), |
| 72 | cpu_to_be16(ADIS_READ_REG(ADIS16400_AUX_ADC)), |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Lars-Peter Clausen | ea19ed3 | 2012-05-12 15:39:41 +0200 | [diff] [blame] | 75 | static int adis16350_spi_read_all(struct iio_dev *indio_dev, u8 *rx) |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 76 | { |
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; |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 82 | int scan_count = bitmap_weight(indio_dev->active_scan_mask, |
| 83 | indio_dev->masklength); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 84 | |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 85 | xfers = kzalloc(sizeof(*xfers)*(scan_count + 1), |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 86 | GFP_KERNEL); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 87 | if (xfers == NULL) |
| 88 | return -ENOMEM; |
| 89 | |
| 90 | for (i = 0; i < ARRAY_SIZE(read_all_tx_array); i++) |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 91 | if (test_bit(i, indio_dev->active_scan_mask)) { |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 92 | xfers[j].tx_buf = &read_all_tx_array[i]; |
| 93 | xfers[j].bits_per_word = 16; |
| 94 | xfers[j].len = 2; |
| 95 | xfers[j + 1].rx_buf = rx + j*2; |
| 96 | j++; |
| 97 | } |
| 98 | xfers[j].bits_per_word = 16; |
| 99 | xfers[j].len = 2; |
| 100 | |
| 101 | spi_message_init(&msg); |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 102 | for (j = 0; j < scan_count + 1; j++) |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 103 | spi_message_add_tail(&xfers[j], &msg); |
| 104 | |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 105 | ret = spi_sync(st->adis.spi, &msg); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 106 | kfree(xfers); |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 111 | /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device |
| 112 | * specific to be rolled into the core. |
| 113 | */ |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 114 | static irqreturn_t adis16400_trigger_handler(int irq, void *p) |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 115 | { |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 116 | struct iio_poll_func *pf = p; |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 117 | struct iio_dev *indio_dev = pf->indio_dev; |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 118 | struct adis16400_state *st = iio_priv(indio_dev); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 119 | int i = 0, j, ret = 0; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 120 | s16 *data; |
Jonathan Cameron | 420fe2e | 2012-04-21 10:09:35 +0100 | [diff] [blame] | 121 | |
Jonathan Cameron | 32b5eec | 2011-09-02 17:14:38 +0100 | [diff] [blame] | 122 | /* Asumption that long is enough for maximum channels */ |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 123 | unsigned long mask = *indio_dev->active_scan_mask; |
| 124 | int scan_count = bitmap_weight(indio_dev->active_scan_mask, |
| 125 | indio_dev->masklength); |
Jonathan Cameron | 420fe2e | 2012-04-21 10:09:35 +0100 | [diff] [blame] | 126 | data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 127 | if (data == NULL) { |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 128 | dev_err(&st->adis.spi->dev, "memory alloc failed in ring bh"); |
Lars-Peter Clausen | 462c8d2 | 2012-07-04 17:09:00 +0100 | [diff] [blame] | 129 | goto done; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 132 | if (scan_count) { |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 133 | if (st->variant->flags & ADIS16400_NO_BURST) { |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 134 | ret = adis16350_spi_read_all(indio_dev, st->adis.rx); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 135 | if (ret < 0) |
Lars-Peter Clausen | 462c8d2 | 2012-07-04 17:09:00 +0100 | [diff] [blame] | 136 | goto done; |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 137 | for (; i < scan_count; i++) |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 138 | data[i] = *(s16 *)(st->adis.rx + i*2); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 139 | } else { |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 140 | ret = adis16400_spi_read_burst(indio_dev, st->adis.rx); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 141 | if (ret < 0) |
Lars-Peter Clausen | 462c8d2 | 2012-07-04 17:09:00 +0100 | [diff] [blame] | 142 | goto done; |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 143 | for (; i < scan_count; i++) { |
Michael Hennerich | 0fea4d6 | 2011-03-21 16:44:38 +0100 | [diff] [blame] | 144 | j = __ffs(mask); |
| 145 | mask &= ~(1 << j); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 146 | data[i] = be16_to_cpup( |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 147 | (__be16 *)&(st->adis.rx[j*2])); |
Michael Hennerich | 0fea4d6 | 2011-03-21 16:44:38 +0100 | [diff] [blame] | 148 | } |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 151 | /* Guaranteed to be aligned with 8 byte boundary */ |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 152 | if (indio_dev->scan_timestamp) |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 153 | *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp; |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 154 | iio_push_to_buffers(indio_dev, (u8 *) data); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 155 | |
Lars-Peter Clausen | 462c8d2 | 2012-07-04 17:09:00 +0100 | [diff] [blame] | 156 | done: |
| 157 | kfree(data); |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 158 | iio_trigger_notify_done(indio_dev->trig); |
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 | return IRQ_HANDLED; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 161 | } |
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 | int adis16400_configure_ring(struct iio_dev *indio_dev) |
| 164 | { |
Lars-Peter Clausen | 1ffe2e7 | 2013-01-16 12:48:00 +0000 | [diff] [blame^] | 165 | return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, |
| 166 | &adis16400_trigger_handler, NULL); |
| 167 | } |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 168 | |
Lars-Peter Clausen | 1ffe2e7 | 2013-01-16 12:48:00 +0000 | [diff] [blame^] | 169 | void adis16400_unconfigure_ring(struct iio_dev *indio_dev) |
| 170 | { |
| 171 | iio_triggered_buffer_cleanup(indio_dev); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 172 | } |