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> |
| 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 "adis16400.h" |
| 19 | |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 20 | /** |
| 21 | * adis16400_spi_read_burst() - read all data registers |
| 22 | * @dev: device associated with child of actual device (iio_dev or iio_trig) |
| 23 | * @rx: somewhere to pass back the value read (min size is 24 bytes) |
| 24 | **/ |
| 25 | static int adis16400_spi_read_burst(struct device *dev, u8 *rx) |
| 26 | { |
| 27 | struct spi_message msg; |
| 28 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 29 | struct adis16400_state *st = iio_dev_get_devdata(indio_dev); |
| 30 | u32 old_speed_hz = st->us->max_speed_hz; |
| 31 | int ret; |
| 32 | |
| 33 | struct spi_transfer xfers[] = { |
| 34 | { |
| 35 | .tx_buf = st->tx, |
| 36 | .bits_per_word = 8, |
| 37 | .len = 2, |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 38 | }, { |
| 39 | .rx_buf = rx, |
| 40 | .bits_per_word = 8, |
| 41 | .len = 24, |
Barry Song | 3fd66da | 2010-06-04 17:19:53 +0800 | [diff] [blame] | 42 | }, |
| 43 | }; |
| 44 | |
| 45 | mutex_lock(&st->buf_lock); |
| 46 | st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD); |
| 47 | st->tx[1] = 0; |
| 48 | |
| 49 | spi_message_init(&msg); |
| 50 | spi_message_add_tail(&xfers[0], &msg); |
| 51 | spi_message_add_tail(&xfers[1], &msg); |
| 52 | |
| 53 | st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz); |
| 54 | spi_setup(st->us); |
| 55 | |
| 56 | ret = spi_sync(st->us, &msg); |
| 57 | if (ret) |
| 58 | dev_err(&st->us->dev, "problem when burst reading"); |
| 59 | |
| 60 | st->us->max_speed_hz = old_speed_hz; |
| 61 | spi_setup(st->us); |
| 62 | mutex_unlock(&st->buf_lock); |
| 63 | return ret; |
| 64 | } |
| 65 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 66 | /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device |
| 67 | * specific to be rolled into the core. |
| 68 | */ |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 69 | static irqreturn_t adis16400_trigger_handler(int irq, void *p) |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 70 | { |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 71 | struct iio_poll_func *pf = p; |
| 72 | struct iio_dev *indio_dev = pf->private_data; |
| 73 | struct adis16400_state *st = iio_dev_get_devdata(indio_dev); |
| 74 | struct iio_ring_buffer *ring = indio_dev->ring; |
Michael Hennerich | 0fea4d6 | 2011-03-21 16:44:38 +0100 | [diff] [blame] | 75 | int i = 0, j; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 76 | s16 *data; |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 77 | size_t datasize = ring->access.get_bytes_per_datum(ring); |
Michael Hennerich | 0fea4d6 | 2011-03-21 16:44:38 +0100 | [diff] [blame] | 78 | unsigned long mask = ring->scan_mask; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 79 | |
| 80 | data = kmalloc(datasize , GFP_KERNEL); |
| 81 | if (data == NULL) { |
| 82 | dev_err(&st->us->dev, "memory alloc failed in ring bh"); |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 83 | return -ENOMEM; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 86 | if (ring->scan_count) |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 87 | if (adis16400_spi_read_burst(&indio_dev->dev, st->rx) >= 0) |
Michael Hennerich | 0fea4d6 | 2011-03-21 16:44:38 +0100 | [diff] [blame] | 88 | for (; i < ring->scan_count; i++) { |
| 89 | j = __ffs(mask); |
| 90 | mask &= ~(1 << j); |
Jonathan Cameron | f4658c8 | 2010-07-11 16:39:12 +0100 | [diff] [blame] | 91 | data[i] = be16_to_cpup( |
Michael Hennerich | 0fea4d6 | 2011-03-21 16:44:38 +0100 | [diff] [blame] | 92 | (__be16 *)&(st->rx[j*2])); |
| 93 | } |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 94 | |
| 95 | /* Guaranteed to be aligned with 8 byte boundary */ |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 96 | if (ring->scan_timestamp) |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 97 | *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp; |
| 98 | ring->access.store_to(indio_dev->ring, (u8 *) data, pf->timestamp); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 99 | |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 100 | iio_trigger_notify_done(indio_dev->trig); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 101 | kfree(data); |
| 102 | |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 103 | return IRQ_HANDLED; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 104 | } |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 105 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 106 | void adis16400_unconfigure_ring(struct iio_dev *indio_dev) |
| 107 | { |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 108 | kfree(indio_dev->pollfunc->name); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 109 | kfree(indio_dev->pollfunc); |
| 110 | iio_sw_rb_free(indio_dev->ring); |
| 111 | } |
| 112 | |
| 113 | int adis16400_configure_ring(struct iio_dev *indio_dev) |
| 114 | { |
| 115 | int ret = 0; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 116 | struct iio_ring_buffer *ring; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 117 | |
| 118 | ring = iio_sw_rb_allocate(indio_dev); |
| 119 | if (!ring) { |
| 120 | ret = -ENOMEM; |
| 121 | return ret; |
| 122 | } |
| 123 | indio_dev->ring = ring; |
| 124 | /* Effectively select the ring buffer implementation */ |
| 125 | iio_ring_sw_register_funcs(&ring->access); |
Jonathan Cameron | 43c11b4 | 2010-07-11 16:39:17 +0100 | [diff] [blame] | 126 | ring->bpe = 2; |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 127 | ring->scan_timestamp = true; |
Jonathan Cameron | 43c11b4 | 2010-07-11 16:39:17 +0100 | [diff] [blame] | 128 | ring->preenable = &iio_sw_ring_preenable; |
Jonathan Cameron | c3db00c | 2010-07-11 16:39:10 +0100 | [diff] [blame] | 129 | ring->postenable = &iio_triggered_ring_postenable; |
| 130 | ring->predisable = &iio_triggered_ring_predisable; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 131 | ring->owner = THIS_MODULE; |
| 132 | |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 133 | /* Set default scan mode */ |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 134 | iio_scan_mask_set(ring, ADIS16400_SCAN_SUPPLY); |
| 135 | iio_scan_mask_set(ring, ADIS16400_SCAN_GYRO_X); |
| 136 | iio_scan_mask_set(ring, ADIS16400_SCAN_GYRO_Y); |
| 137 | iio_scan_mask_set(ring, ADIS16400_SCAN_GYRO_Z); |
| 138 | iio_scan_mask_set(ring, ADIS16400_SCAN_ACC_X); |
| 139 | iio_scan_mask_set(ring, ADIS16400_SCAN_ACC_Y); |
| 140 | iio_scan_mask_set(ring, ADIS16400_SCAN_ACC_Z); |
| 141 | iio_scan_mask_set(ring, ADIS16400_SCAN_MAGN_X); |
| 142 | iio_scan_mask_set(ring, ADIS16400_SCAN_MAGN_Y); |
| 143 | iio_scan_mask_set(ring, ADIS16400_SCAN_MAGN_Z); |
| 144 | iio_scan_mask_set(ring, ADIS16400_SCAN_TEMP); |
| 145 | iio_scan_mask_set(ring, ADIS16400_SCAN_ADC_0); |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 146 | |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 147 | indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL); |
| 148 | if (indio_dev->pollfunc == NULL) { |
| 149 | ret = -ENOMEM; |
Jonathan Cameron | 1574409 | 2010-07-11 16:39:09 +0100 | [diff] [blame] | 150 | goto error_iio_sw_rb_free; |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 151 | } |
| 152 | indio_dev->pollfunc->private_data = indio_dev; |
| 153 | indio_dev->pollfunc->h = &iio_pollfunc_store_time; |
| 154 | indio_dev->pollfunc->thread = &adis16400_trigger_handler; |
| 155 | indio_dev->pollfunc->type = IRQF_ONESHOT; |
| 156 | indio_dev->pollfunc->name = |
| 157 | kasprintf(GFP_KERNEL, "adis16400_consumer%d", indio_dev->id); |
| 158 | if (ret) |
| 159 | goto error_iio_free_pollfunc; |
Jonathan Cameron | 1574409 | 2010-07-11 16:39:09 +0100 | [diff] [blame] | 160 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 161 | indio_dev->modes |= INDIO_RING_TRIGGERED; |
| 162 | return 0; |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame^] | 163 | error_iio_free_pollfunc: |
| 164 | kfree(indio_dev->pollfunc); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 165 | error_iio_sw_rb_free: |
| 166 | iio_sw_rb_free(indio_dev->ring); |
| 167 | return ret; |
| 168 | } |