Commit | Line | Data |
---|---|---|
5763dcab BS |
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> | |
1cb6c1f5 | 9 | #include <linux/slab.h> |
5763dcab BS |
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 "adis16350.h" | |
19 | ||
1755b0ae | 20 | static IIO_SCAN_EL_C(in0_supply, ADIS16350_SCAN_SUPPLY, |
5763dcab | 21 | ADIS16350_SUPPLY_OUT, NULL); |
1755b0ae | 22 | static IIO_CONST_ATTR_SCAN_EL_TYPE(in0_supply, u, 12, 16); |
5763dcab | 23 | |
44f270de JC |
24 | static IIO_SCAN_EL_C(gyro_x, ADIS16350_SCAN_GYRO_X, ADIS16350_XGYRO_OUT, NULL); |
25 | static IIO_SCAN_EL_C(gyro_y, ADIS16350_SCAN_GYRO_Y, ADIS16350_YGYRO_OUT, NULL); | |
26 | static IIO_SCAN_EL_C(gyro_z, ADIS16350_SCAN_GYRO_Z, ADIS16350_ZGYRO_OUT, NULL); | |
3b401a96 | 27 | static IIO_CONST_ATTR_SCAN_EL_TYPE(gyro, s, 14, 16); |
5763dcab | 28 | |
44f270de JC |
29 | static IIO_SCAN_EL_C(accel_x, ADIS16350_SCAN_ACC_X, ADIS16350_XACCL_OUT, NULL); |
30 | static IIO_SCAN_EL_C(accel_y, ADIS16350_SCAN_ACC_Y, ADIS16350_YACCL_OUT, NULL); | |
31 | static IIO_SCAN_EL_C(accel_z, ADIS16350_SCAN_ACC_Z, ADIS16350_ZACCL_OUT, NULL); | |
3b401a96 | 32 | static IIO_CONST_ATTR_SCAN_EL_TYPE(accel, s, 14, 16); |
5763dcab | 33 | |
44f270de JC |
34 | static IIO_SCAN_EL_C(temp_x, ADIS16350_SCAN_TEMP_X, ADIS16350_XTEMP_OUT, NULL); |
35 | static IIO_SCAN_EL_C(temp_y, ADIS16350_SCAN_TEMP_Y, ADIS16350_YTEMP_OUT, NULL); | |
36 | static IIO_SCAN_EL_C(temp_z, ADIS16350_SCAN_TEMP_Z, ADIS16350_ZTEMP_OUT, NULL); | |
3b401a96 | 37 | static IIO_CONST_ATTR_SCAN_EL_TYPE(temp, s, 12, 16); |
5763dcab | 38 | |
1755b0ae JC |
39 | static IIO_SCAN_EL_C(in1, ADIS16350_SCAN_ADC_0, ADIS16350_AUX_ADC, NULL); |
40 | static IIO_CONST_ATTR_SCAN_EL_TYPE(in1, u, 12, 16); | |
5763dcab BS |
41 | |
42 | static IIO_SCAN_EL_TIMESTAMP(11); | |
3b401a96 | 43 | static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64); |
5763dcab BS |
44 | |
45 | static struct attribute *adis16350_scan_el_attrs[] = { | |
1755b0ae JC |
46 | &iio_scan_el_in0_supply.dev_attr.attr, |
47 | &iio_const_attr_in0_supply_index.dev_attr.attr, | |
48 | &iio_const_attr_in0_supply_type.dev_attr.attr, | |
5763dcab | 49 | &iio_scan_el_gyro_x.dev_attr.attr, |
de9b493e | 50 | &iio_const_attr_gyro_x_index.dev_attr.attr, |
5763dcab | 51 | &iio_scan_el_gyro_y.dev_attr.attr, |
de9b493e | 52 | &iio_const_attr_gyro_y_index.dev_attr.attr, |
5763dcab | 53 | &iio_scan_el_gyro_z.dev_attr.attr, |
de9b493e | 54 | &iio_const_attr_gyro_z_index.dev_attr.attr, |
3b401a96 | 55 | &iio_const_attr_gyro_type.dev_attr.attr, |
5763dcab | 56 | &iio_scan_el_accel_x.dev_attr.attr, |
de9b493e | 57 | &iio_const_attr_accel_x_index.dev_attr.attr, |
5763dcab | 58 | &iio_scan_el_accel_y.dev_attr.attr, |
de9b493e | 59 | &iio_const_attr_accel_y_index.dev_attr.attr, |
5763dcab | 60 | &iio_scan_el_accel_z.dev_attr.attr, |
de9b493e | 61 | &iio_const_attr_accel_z_index.dev_attr.attr, |
3b401a96 | 62 | &iio_const_attr_accel_type.dev_attr.attr, |
5763dcab | 63 | &iio_scan_el_temp_x.dev_attr.attr, |
de9b493e | 64 | &iio_const_attr_temp_x_index.dev_attr.attr, |
5763dcab | 65 | &iio_scan_el_temp_y.dev_attr.attr, |
de9b493e | 66 | &iio_const_attr_temp_y_index.dev_attr.attr, |
5763dcab | 67 | &iio_scan_el_temp_z.dev_attr.attr, |
de9b493e | 68 | &iio_const_attr_temp_z_index.dev_attr.attr, |
3b401a96 | 69 | &iio_const_attr_temp_type.dev_attr.attr, |
1755b0ae JC |
70 | &iio_scan_el_in1.dev_attr.attr, |
71 | &iio_const_attr_in1_index.dev_attr.attr, | |
72 | &iio_const_attr_in1_type.dev_attr.attr, | |
5763dcab | 73 | &iio_scan_el_timestamp.dev_attr.attr, |
de9b493e | 74 | &iio_const_attr_timestamp_index.dev_attr.attr, |
3b401a96 | 75 | &iio_const_attr_timestamp_type.dev_attr.attr, |
5763dcab BS |
76 | NULL, |
77 | }; | |
78 | ||
79 | static struct attribute_group adis16350_scan_el_group = { | |
80 | .attrs = adis16350_scan_el_attrs, | |
81 | .name = "scan_elements", | |
82 | }; | |
83 | ||
84 | /** | |
85 | * adis16350_poll_func_th() top half interrupt handler called by trigger | |
86 | * @private_data: iio_dev | |
87 | **/ | |
7b2c33b1 | 88 | static void adis16350_poll_func_th(struct iio_dev *indio_dev, s64 time) |
5763dcab BS |
89 | { |
90 | struct adis16350_state *st = iio_dev_get_devdata(indio_dev); | |
7b2c33b1 | 91 | st->last_timestamp = time; |
5763dcab BS |
92 | schedule_work(&st->work_trigger_to_ring); |
93 | } | |
94 | ||
95 | /** | |
96 | * adis16350_spi_read_burst() - read all data registers | |
97 | * @dev: device associated with child of actual device (iio_dev or iio_trig) | |
98 | * @rx: somewhere to pass back the value read (min size is 24 bytes) | |
99 | **/ | |
100 | static int adis16350_spi_read_burst(struct device *dev, u8 *rx) | |
101 | { | |
102 | struct spi_message msg; | |
103 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
104 | struct adis16350_state *st = iio_dev_get_devdata(indio_dev); | |
105 | u32 old_speed_hz = st->us->max_speed_hz; | |
106 | int ret; | |
107 | ||
108 | struct spi_transfer xfers[] = { | |
109 | { | |
110 | .tx_buf = st->tx, | |
111 | .bits_per_word = 8, | |
112 | .len = 2, | |
113 | .cs_change = 0, | |
114 | }, { | |
115 | .rx_buf = rx, | |
116 | .bits_per_word = 8, | |
117 | .len = 22, | |
118 | .cs_change = 0, | |
119 | }, | |
120 | }; | |
121 | ||
122 | mutex_lock(&st->buf_lock); | |
123 | st->tx[0] = ADIS16350_READ_REG(ADIS16350_GLOB_CMD); | |
124 | st->tx[1] = 0; | |
125 | ||
126 | spi_message_init(&msg); | |
127 | spi_message_add_tail(&xfers[0], &msg); | |
128 | spi_message_add_tail(&xfers[1], &msg); | |
129 | ||
130 | st->us->max_speed_hz = ADIS16350_SPI_BURST; | |
131 | spi_setup(st->us); | |
132 | ||
133 | ret = spi_sync(st->us, &msg); | |
134 | if (ret) | |
135 | dev_err(&st->us->dev, "problem when burst reading"); | |
136 | ||
137 | st->us->max_speed_hz = old_speed_hz; | |
138 | spi_setup(st->us); | |
139 | mutex_unlock(&st->buf_lock); | |
140 | return ret; | |
141 | } | |
142 | ||
143 | /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device | |
144 | * specific to be rolled into the core. | |
145 | */ | |
146 | static void adis16350_trigger_bh_to_ring(struct work_struct *work_s) | |
147 | { | |
148 | struct adis16350_state *st | |
149 | = container_of(work_s, struct adis16350_state, | |
150 | work_trigger_to_ring); | |
bf32963c | 151 | struct iio_ring_buffer *ring = st->indio_dev->ring; |
5763dcab BS |
152 | |
153 | int i = 0; | |
154 | s16 *data; | |
bf32963c | 155 | size_t datasize = ring->access.get_bytes_per_datum(ring); |
5763dcab BS |
156 | |
157 | data = kmalloc(datasize , GFP_KERNEL); | |
158 | if (data == NULL) { | |
159 | dev_err(&st->us->dev, "memory alloc failed in ring bh"); | |
160 | return; | |
161 | } | |
162 | ||
bf32963c | 163 | if (ring->scan_count) |
5763dcab | 164 | if (adis16350_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0) |
bf32963c | 165 | for (; i < ring->scan_count; i++) |
f4658c89 JC |
166 | data[i] = be16_to_cpup( |
167 | (__be16 *)&(st->rx[i*2])); | |
5763dcab BS |
168 | |
169 | /* Guaranteed to be aligned with 8 byte boundary */ | |
bf32963c | 170 | if (ring->scan_timestamp) |
5763dcab BS |
171 | *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp; |
172 | ||
bf32963c MS |
173 | ring->access.store_to(ring, |
174 | (u8 *)data, | |
175 | st->last_timestamp); | |
5763dcab BS |
176 | |
177 | iio_trigger_notify_done(st->indio_dev->trig); | |
178 | kfree(data); | |
179 | ||
180 | return; | |
181 | } | |
182 | ||
5763dcab BS |
183 | void adis16350_unconfigure_ring(struct iio_dev *indio_dev) |
184 | { | |
185 | kfree(indio_dev->pollfunc); | |
186 | iio_sw_rb_free(indio_dev->ring); | |
187 | } | |
188 | ||
189 | int adis16350_configure_ring(struct iio_dev *indio_dev) | |
190 | { | |
191 | int ret = 0; | |
192 | struct adis16350_state *st = indio_dev->dev_data; | |
193 | struct iio_ring_buffer *ring; | |
194 | INIT_WORK(&st->work_trigger_to_ring, adis16350_trigger_bh_to_ring); | |
5763dcab BS |
195 | |
196 | ring = iio_sw_rb_allocate(indio_dev); | |
197 | if (!ring) { | |
198 | ret = -ENOMEM; | |
199 | return ret; | |
200 | } | |
201 | indio_dev->ring = ring; | |
202 | /* Effectively select the ring buffer implementation */ | |
203 | iio_ring_sw_register_funcs(&ring->access); | |
43c11b43 | 204 | ring->bpe = 2; |
bf32963c MS |
205 | ring->scan_el_attrs = &adis16350_scan_el_group; |
206 | ring->scan_timestamp = true; | |
43c11b43 | 207 | ring->preenable = &iio_sw_ring_preenable; |
c3db00cc JC |
208 | ring->postenable = &iio_triggered_ring_postenable; |
209 | ring->predisable = &iio_triggered_ring_predisable; | |
5763dcab BS |
210 | ring->owner = THIS_MODULE; |
211 | ||
bf32963c | 212 | /* Set default scan mode */ |
1755b0ae | 213 | iio_scan_mask_set(ring, iio_scan_el_in0_supply.number); |
bf32963c MS |
214 | iio_scan_mask_set(ring, iio_scan_el_gyro_x.number); |
215 | iio_scan_mask_set(ring, iio_scan_el_gyro_y.number); | |
216 | iio_scan_mask_set(ring, iio_scan_el_gyro_z.number); | |
217 | iio_scan_mask_set(ring, iio_scan_el_accel_x.number); | |
218 | iio_scan_mask_set(ring, iio_scan_el_accel_y.number); | |
219 | iio_scan_mask_set(ring, iio_scan_el_accel_z.number); | |
220 | iio_scan_mask_set(ring, iio_scan_el_temp_x.number); | |
221 | iio_scan_mask_set(ring, iio_scan_el_temp_y.number); | |
222 | iio_scan_mask_set(ring, iio_scan_el_temp_z.number); | |
1755b0ae | 223 | iio_scan_mask_set(ring, iio_scan_el_in1.number); |
bf32963c | 224 | |
15744090 JC |
225 | ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16350_poll_func_th); |
226 | if (ret) | |
227 | goto error_iio_sw_rb_free; | |
228 | ||
5763dcab BS |
229 | indio_dev->modes |= INDIO_RING_TRIGGERED; |
230 | return 0; | |
231 | ||
232 | error_iio_sw_rb_free: | |
233 | iio_sw_rb_free(indio_dev->ring); | |
234 | return ret; | |
235 | } | |
236 |