Commit | Line | Data |
---|---|---|
574fb258 JC |
1 | /* |
2 | * sca3000_ring.c -- support VTI sca3000 series accelerometers via SPI | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License version 2 as published by | |
6 | * the Free Software Foundation. | |
7 | * | |
0f8c9620 | 8 | * Copyright (c) 2009 Jonathan Cameron <jic23@kernel.org> |
574fb258 JC |
9 | * |
10 | */ | |
11 | ||
12 | #include <linux/interrupt.h> | |
574fb258 | 13 | #include <linux/fs.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
574fb258 JC |
15 | #include <linux/kernel.h> |
16 | #include <linux/spi/spi.h> | |
17 | #include <linux/sysfs.h> | |
25888dc5 JC |
18 | #include <linux/sched.h> |
19 | #include <linux/poll.h> | |
574fb258 | 20 | |
06458e27 JC |
21 | #include <linux/iio/iio.h> |
22 | #include <linux/iio/sysfs.h> | |
23 | #include <linux/iio/buffer.h> | |
574fb258 | 24 | #include "../ring_hw.h" |
574fb258 JC |
25 | #include "sca3000.h" |
26 | ||
27 | /* RFC / future work | |
28 | * | |
29 | * The internal ring buffer doesn't actually change what it holds depending | |
30 | * on which signals are enabled etc, merely whether you can read them. | |
31 | * As such the scan mode selection is somewhat different than for a software | |
32 | * ring buffer and changing it actually covers any data already in the buffer. | |
33 | * Currently scan elements aren't configured so it doesn't matter. | |
34 | */ | |
35 | ||
25888dc5 JC |
36 | static int sca3000_read_data(struct sca3000_state *st, |
37 | uint8_t reg_address_high, | |
38 | u8 **rx_p, | |
39 | int len) | |
40 | { | |
41 | int ret; | |
25888dc5 JC |
42 | struct spi_transfer xfer[2] = { |
43 | { | |
44 | .len = 1, | |
45 | .tx_buf = st->tx, | |
46 | }, { | |
47 | .len = len, | |
48 | } | |
49 | }; | |
50 | *rx_p = kmalloc(len, GFP_KERNEL); | |
51 | if (*rx_p == NULL) { | |
52 | ret = -ENOMEM; | |
53 | goto error_ret; | |
54 | } | |
55 | xfer[1].rx_buf = *rx_p; | |
56 | st->tx[0] = SCA3000_READ_REG(reg_address_high); | |
ad6c46b0 | 57 | ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); |
25888dc5 JC |
58 | if (ret) { |
59 | dev_err(get_device(&st->us->dev), "problem reading register"); | |
60 | goto error_free_rx; | |
61 | } | |
62 | ||
63 | return 0; | |
64 | error_free_rx: | |
65 | kfree(*rx_p); | |
66 | error_ret: | |
67 | return ret; | |
68 | } | |
69 | ||
574fb258 | 70 | /** |
b4281733 | 71 | * sca3000_read_first_n_hw_rb() - main ring access, pulls data from ring |
574fb258 JC |
72 | * @r: the ring |
73 | * @count: number of samples to try and pull | |
74 | * @data: output the actual samples pulled from the hw ring | |
574fb258 JC |
75 | * |
76 | * Currently does not provide timestamps. As the hardware doesn't add them they | |
25985edc | 77 | * can only be inferred approximately from ring buffer events such as 50% full |
574fb258 JC |
78 | * and knowledge of when buffer was last emptied. This is left to userspace. |
79 | **/ | |
14555b14 | 80 | static int sca3000_read_first_n_hw_rb(struct iio_buffer *r, |
b26a2188 | 81 | size_t count, char __user *buf) |
574fb258 | 82 | { |
14555b14 | 83 | struct iio_hw_buffer *hw_ring = iio_to_hw_buf(r); |
574fb258 | 84 | struct iio_dev *indio_dev = hw_ring->private; |
83f0422d | 85 | struct sca3000_state *st = iio_priv(indio_dev); |
574fb258 | 86 | u8 *rx; |
6267ea65 | 87 | int ret, i, num_available, num_read = 0; |
574fb258 JC |
88 | int bytes_per_sample = 1; |
89 | ||
90 | if (st->bpse == 11) | |
91 | bytes_per_sample = 2; | |
92 | ||
93 | mutex_lock(&st->lock); | |
25888dc5 JC |
94 | if (count % bytes_per_sample) { |
95 | ret = -EINVAL; | |
96 | goto error_ret; | |
97 | } | |
98 | ||
99 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_BUF_COUNT, 1); | |
574fb258 JC |
100 | if (ret) |
101 | goto error_ret; | |
102 | else | |
25888dc5 JC |
103 | num_available = st->rx[0]; |
104 | /* | |
105 | * num_available is the total number of samples available | |
574fb258 JC |
106 | * i.e. number of time points * number of channels. |
107 | */ | |
574fb258 JC |
108 | if (count > num_available * bytes_per_sample) |
109 | num_read = num_available*bytes_per_sample; | |
110 | else | |
25888dc5 | 111 | num_read = count; |
574fb258 | 112 | |
574fb258 JC |
113 | ret = sca3000_read_data(st, |
114 | SCA3000_REG_ADDR_RING_OUT, | |
25888dc5 JC |
115 | &rx, num_read); |
116 | if (ret) | |
117 | goto error_ret; | |
118 | ||
119 | for (i = 0; i < num_read; i++) | |
03c6eaa3 | 120 | *(((u16 *)rx) + i) = be16_to_cpup((__be16 *)rx + i); |
6267ea65 | 121 | |
25888dc5 JC |
122 | if (copy_to_user(buf, rx, num_read)) |
123 | ret = -EFAULT; | |
124 | kfree(rx); | |
125 | r->stufftoread = 0; | |
574fb258 JC |
126 | error_ret: |
127 | mutex_unlock(&st->lock); | |
128 | ||
129 | return ret ? ret : num_read; | |
130 | } | |
131 | ||
37d34556 | 132 | static size_t sca3000_ring_buf_data_available(struct iio_buffer *r) |
9dd4694d | 133 | { |
37d34556 | 134 | return r->stufftoread ? r->watermark : 0; |
9dd4694d JC |
135 | } |
136 | ||
25888dc5 JC |
137 | /** |
138 | * sca3000_query_ring_int() is the hardware ring status interrupt enabled | |
139 | **/ | |
140 | static ssize_t sca3000_query_ring_int(struct device *dev, | |
141 | struct device_attribute *attr, | |
142 | char *buf) | |
143 | { | |
144 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
145 | int ret, val; | |
4b522ce7 | 146 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
83f0422d | 147 | struct sca3000_state *st = iio_priv(indio_dev); |
25888dc5 JC |
148 | |
149 | mutex_lock(&st->lock); | |
150 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1); | |
151 | val = st->rx[0]; | |
152 | mutex_unlock(&st->lock); | |
153 | if (ret) | |
154 | return ret; | |
155 | ||
156 | return sprintf(buf, "%d\n", !!(val & this_attr->address)); | |
157 | } | |
158 | ||
159 | /** | |
160 | * sca3000_set_ring_int() set state of ring status interrupt | |
161 | **/ | |
162 | static ssize_t sca3000_set_ring_int(struct device *dev, | |
163 | struct device_attribute *attr, | |
164 | const char *buf, | |
165 | size_t len) | |
166 | { | |
4b522ce7 | 167 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
83f0422d | 168 | struct sca3000_state *st = iio_priv(indio_dev); |
25888dc5 | 169 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
e5e26dd5 | 170 | u8 val; |
25888dc5 JC |
171 | int ret; |
172 | ||
173 | mutex_lock(&st->lock); | |
e5e26dd5 | 174 | ret = kstrtou8(buf, 10, &val); |
25888dc5 JC |
175 | if (ret) |
176 | goto error_ret; | |
177 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1); | |
178 | if (ret) | |
179 | goto error_ret; | |
180 | if (val) | |
181 | ret = sca3000_write_reg(st, | |
182 | SCA3000_REG_ADDR_INT_MASK, | |
183 | st->rx[0] | this_attr->address); | |
184 | else | |
185 | ret = sca3000_write_reg(st, | |
186 | SCA3000_REG_ADDR_INT_MASK, | |
187 | st->rx[0] & ~this_attr->address); | |
188 | error_ret: | |
189 | mutex_unlock(&st->lock); | |
190 | ||
191 | return ret ? ret : len; | |
192 | } | |
193 | ||
194 | static IIO_DEVICE_ATTR(50_percent, S_IRUGO | S_IWUSR, | |
195 | sca3000_query_ring_int, | |
196 | sca3000_set_ring_int, | |
197 | SCA3000_INT_MASK_RING_HALF); | |
198 | ||
199 | static IIO_DEVICE_ATTR(75_percent, S_IRUGO | S_IWUSR, | |
200 | sca3000_query_ring_int, | |
201 | sca3000_set_ring_int, | |
202 | SCA3000_INT_MASK_RING_THREE_QUARTER); | |
203 | ||
25888dc5 JC |
204 | static ssize_t sca3000_show_buffer_scale(struct device *dev, |
205 | struct device_attribute *attr, | |
206 | char *buf) | |
207 | { | |
4b522ce7 | 208 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
83f0422d | 209 | struct sca3000_state *st = iio_priv(indio_dev); |
574fb258 | 210 | |
25888dc5 JC |
211 | return sprintf(buf, "0.%06d\n", 4*st->info->scale); |
212 | } | |
f3fb0011 | 213 | |
322c9563 | 214 | static IIO_DEVICE_ATTR(in_accel_scale, |
25888dc5 JC |
215 | S_IRUGO, |
216 | sca3000_show_buffer_scale, | |
217 | NULL, | |
218 | 0); | |
574fb258 JC |
219 | |
220 | /* | |
221 | * Ring buffer attributes | |
222 | * This device is a bit unusual in that the sampling frequency and bpse | |
223 | * only apply to the ring buffer. At all times full rate and accuracy | |
224 | * is available via direct reading from registers. | |
225 | */ | |
08e7e0ad | 226 | static const struct attribute *sca3000_ring_attributes[] = { |
25888dc5 JC |
227 | &iio_dev_attr_50_percent.dev_attr.attr, |
228 | &iio_dev_attr_75_percent.dev_attr.attr, | |
322c9563 | 229 | &iio_dev_attr_in_accel_scale.dev_attr.attr, |
574fb258 JC |
230 | NULL, |
231 | }; | |
232 | ||
14555b14 | 233 | static struct iio_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev) |
574fb258 | 234 | { |
14555b14 JC |
235 | struct iio_buffer *buf; |
236 | struct iio_hw_buffer *ring; | |
574fb258 | 237 | |
c92cb53d | 238 | ring = kzalloc(sizeof(*ring), GFP_KERNEL); |
574fb258 | 239 | if (!ring) |
7cfce527 | 240 | return NULL; |
25888dc5 | 241 | |
574fb258 JC |
242 | ring->private = indio_dev; |
243 | buf = &ring->buf; | |
25888dc5 | 244 | buf->stufftoread = 0; |
37495660 | 245 | buf->length = 64; |
08e7e0ad | 246 | buf->attrs = sca3000_ring_attributes; |
f79a9098 | 247 | iio_buffer_init(buf); |
574fb258 JC |
248 | |
249 | return buf; | |
250 | } | |
251 | ||
9e69c935 | 252 | static void sca3000_ring_release(struct iio_buffer *r) |
574fb258 | 253 | { |
14555b14 | 254 | kfree(iio_to_hw_buf(r)); |
574fb258 JC |
255 | } |
256 | ||
14555b14 | 257 | static const struct iio_buffer_access_funcs sca3000_ring_access_funcs = { |
5565a450 | 258 | .read_first_n = &sca3000_read_first_n_hw_rb, |
9dd4694d | 259 | .data_available = sca3000_ring_buf_data_available, |
9e69c935 | 260 | .release = sca3000_ring_release, |
225d59ad LPC |
261 | |
262 | .modes = INDIO_BUFFER_HARDWARE, | |
5565a450 JC |
263 | }; |
264 | ||
574fb258 JC |
265 | int sca3000_configure_ring(struct iio_dev *indio_dev) |
266 | { | |
9e69c935 LPC |
267 | struct iio_buffer *buffer; |
268 | ||
269 | buffer = sca3000_rb_allocate(indio_dev); | |
eee587ea | 270 | if (!buffer) |
574fb258 | 271 | return -ENOMEM; |
ec3afa40 | 272 | indio_dev->modes |= INDIO_BUFFER_HARDWARE; |
574fb258 | 273 | |
14555b14 | 274 | indio_dev->buffer->access = &sca3000_ring_access_funcs; |
25888dc5 | 275 | |
9e69c935 LPC |
276 | iio_device_attach_buffer(indio_dev, buffer); |
277 | ||
574fb258 JC |
278 | return 0; |
279 | } | |
280 | ||
281 | void sca3000_unconfigure_ring(struct iio_dev *indio_dev) | |
282 | { | |
9e69c935 | 283 | iio_buffer_put(indio_dev->buffer); |
574fb258 JC |
284 | } |
285 | ||
286 | static inline | |
287 | int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state) | |
288 | { | |
83f0422d | 289 | struct sca3000_state *st = iio_priv(indio_dev); |
574fb258 | 290 | int ret; |
574fb258 JC |
291 | |
292 | mutex_lock(&st->lock); | |
25888dc5 | 293 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); |
574fb258 JC |
294 | if (ret) |
295 | goto error_ret; | |
296 | if (state) { | |
941060b4 | 297 | dev_info(&indio_dev->dev, "supposedly enabling ring buffer\n"); |
574fb258 JC |
298 | ret = sca3000_write_reg(st, |
299 | SCA3000_REG_ADDR_MODE, | |
25888dc5 | 300 | (st->rx[0] | SCA3000_RING_BUF_ENABLE)); |
574fb258 JC |
301 | } else |
302 | ret = sca3000_write_reg(st, | |
303 | SCA3000_REG_ADDR_MODE, | |
25888dc5 | 304 | (st->rx[0] & ~SCA3000_RING_BUF_ENABLE)); |
574fb258 JC |
305 | error_ret: |
306 | mutex_unlock(&st->lock); | |
307 | ||
308 | return ret; | |
309 | } | |
310 | /** | |
311 | * sca3000_hw_ring_preenable() hw ring buffer preenable function | |
312 | * | |
313 | * Very simple enable function as the chip will allows normal reads | |
314 | * during ring buffer operation so as long as it is indeed running | |
315 | * before we notify the core, the precise ordering does not matter. | |
316 | **/ | |
317 | static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev) | |
318 | { | |
319 | return __sca3000_hw_ring_state_set(indio_dev, 1); | |
320 | } | |
321 | ||
322 | static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev) | |
323 | { | |
324 | return __sca3000_hw_ring_state_set(indio_dev, 0); | |
325 | } | |
326 | ||
14555b14 | 327 | static const struct iio_buffer_setup_ops sca3000_ring_setup_ops = { |
5565a450 JC |
328 | .preenable = &sca3000_hw_ring_preenable, |
329 | .postdisable = &sca3000_hw_ring_postdisable, | |
330 | }; | |
331 | ||
574fb258 JC |
332 | void sca3000_register_ring_funcs(struct iio_dev *indio_dev) |
333 | { | |
1612244f | 334 | indio_dev->setup_ops = &sca3000_ring_setup_ops; |
574fb258 JC |
335 | } |
336 | ||
337 | /** | |
338 | * sca3000_ring_int_process() ring specific interrupt handling. | |
339 | * | |
340 | * This is only split from the main interrupt handler so as to | |
341 | * reduce the amount of code if the ring buffer is not enabled. | |
342 | **/ | |
14555b14 | 343 | void sca3000_ring_int_process(u8 val, struct iio_buffer *ring) |
574fb258 | 344 | { |
25888dc5 JC |
345 | if (val & (SCA3000_INT_STATUS_THREE_QUARTERS | |
346 | SCA3000_INT_STATUS_HALF)) { | |
347 | ring->stufftoread = true; | |
348 | wake_up_interruptible(&ring->pollq); | |
349 | } | |
574fb258 | 350 | } |