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 | * | |
8 | * Copyright (c) 2009 Jonathan Cameron <jic23@cam.ac.uk> | |
9 | * | |
10 | */ | |
11 | ||
12 | #include <linux/interrupt.h> | |
13 | #include <linux/gpio.h> | |
14 | #include <linux/fs.h> | |
15 | #include <linux/device.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
574fb258 JC |
17 | #include <linux/kernel.h> |
18 | #include <linux/spi/spi.h> | |
19 | #include <linux/sysfs.h> | |
25888dc5 JC |
20 | #include <linux/sched.h> |
21 | #include <linux/poll.h> | |
574fb258 JC |
22 | |
23 | #include "../iio.h" | |
24 | #include "../sysfs.h" | |
25 | #include "../ring_generic.h" | |
26 | #include "../ring_hw.h" | |
27 | #include "accel.h" | |
28 | #include "sca3000.h" | |
29 | ||
30 | /* RFC / future work | |
31 | * | |
32 | * The internal ring buffer doesn't actually change what it holds depending | |
33 | * on which signals are enabled etc, merely whether you can read them. | |
34 | * As such the scan mode selection is somewhat different than for a software | |
35 | * ring buffer and changing it actually covers any data already in the buffer. | |
36 | * Currently scan elements aren't configured so it doesn't matter. | |
37 | */ | |
38 | ||
25888dc5 JC |
39 | static int sca3000_read_data(struct sca3000_state *st, |
40 | uint8_t reg_address_high, | |
41 | u8 **rx_p, | |
42 | int len) | |
43 | { | |
44 | int ret; | |
45 | struct spi_message msg; | |
46 | struct spi_transfer xfer[2] = { | |
47 | { | |
48 | .len = 1, | |
49 | .tx_buf = st->tx, | |
50 | }, { | |
51 | .len = len, | |
52 | } | |
53 | }; | |
54 | *rx_p = kmalloc(len, GFP_KERNEL); | |
55 | if (*rx_p == NULL) { | |
56 | ret = -ENOMEM; | |
57 | goto error_ret; | |
58 | } | |
59 | xfer[1].rx_buf = *rx_p; | |
60 | st->tx[0] = SCA3000_READ_REG(reg_address_high); | |
61 | spi_message_init(&msg); | |
62 | spi_message_add_tail(&xfer[0], &msg); | |
63 | spi_message_add_tail(&xfer[1], &msg); | |
64 | ret = spi_sync(st->us, &msg); | |
65 | if (ret) { | |
66 | dev_err(get_device(&st->us->dev), "problem reading register"); | |
67 | goto error_free_rx; | |
68 | } | |
69 | ||
70 | return 0; | |
71 | error_free_rx: | |
72 | kfree(*rx_p); | |
73 | error_ret: | |
74 | return ret; | |
75 | } | |
76 | ||
574fb258 | 77 | /** |
b4281733 | 78 | * sca3000_read_first_n_hw_rb() - main ring access, pulls data from ring |
574fb258 JC |
79 | * @r: the ring |
80 | * @count: number of samples to try and pull | |
81 | * @data: output the actual samples pulled from the hw ring | |
574fb258 JC |
82 | * |
83 | * Currently does not provide timestamps. As the hardware doesn't add them they | |
25985edc | 84 | * can only be inferred approximately from ring buffer events such as 50% full |
574fb258 JC |
85 | * and knowledge of when buffer was last emptied. This is left to userspace. |
86 | **/ | |
b4281733 | 87 | static int sca3000_read_first_n_hw_rb(struct iio_ring_buffer *r, |
b26a2188 | 88 | size_t count, char __user *buf) |
574fb258 JC |
89 | { |
90 | struct iio_hw_ring_buffer *hw_ring = iio_to_hw_ring_buf(r); | |
91 | struct iio_dev *indio_dev = hw_ring->private; | |
92 | struct sca3000_state *st = indio_dev->dev_data; | |
93 | u8 *rx; | |
6267ea65 | 94 | int ret, i, num_available, num_read = 0; |
574fb258 JC |
95 | int bytes_per_sample = 1; |
96 | ||
97 | if (st->bpse == 11) | |
98 | bytes_per_sample = 2; | |
99 | ||
100 | mutex_lock(&st->lock); | |
25888dc5 JC |
101 | if (count % bytes_per_sample) { |
102 | ret = -EINVAL; | |
103 | goto error_ret; | |
104 | } | |
105 | ||
106 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_BUF_COUNT, 1); | |
574fb258 JC |
107 | if (ret) |
108 | goto error_ret; | |
109 | else | |
25888dc5 JC |
110 | num_available = st->rx[0]; |
111 | /* | |
112 | * num_available is the total number of samples available | |
574fb258 JC |
113 | * i.e. number of time points * number of channels. |
114 | */ | |
574fb258 JC |
115 | if (count > num_available * bytes_per_sample) |
116 | num_read = num_available*bytes_per_sample; | |
117 | else | |
25888dc5 | 118 | num_read = count; |
574fb258 | 119 | |
574fb258 JC |
120 | ret = sca3000_read_data(st, |
121 | SCA3000_REG_ADDR_RING_OUT, | |
25888dc5 JC |
122 | &rx, num_read); |
123 | if (ret) | |
124 | goto error_ret; | |
125 | ||
126 | for (i = 0; i < num_read; i++) | |
127 | *(((u16 *)rx) + i) = be16_to_cpup((u16 *)rx + i); | |
6267ea65 | 128 | |
25888dc5 JC |
129 | if (copy_to_user(buf, rx, num_read)) |
130 | ret = -EFAULT; | |
131 | kfree(rx); | |
132 | r->stufftoread = 0; | |
574fb258 JC |
133 | error_ret: |
134 | mutex_unlock(&st->lock); | |
135 | ||
136 | return ret ? ret : num_read; | |
137 | } | |
138 | ||
139 | /* This is only valid with all 3 elements enabled */ | |
140 | static int sca3000_ring_get_length(struct iio_ring_buffer *r) | |
141 | { | |
142 | return 64; | |
143 | } | |
144 | ||
145 | /* only valid if resolution is kept at 11bits */ | |
ffcab07a | 146 | static int sca3000_ring_get_bytes_per_datum(struct iio_ring_buffer *r) |
574fb258 JC |
147 | { |
148 | return 6; | |
149 | } | |
150 | static void sca3000_ring_release(struct device *dev) | |
151 | { | |
152 | struct iio_ring_buffer *r = to_iio_ring_buffer(dev); | |
153 | kfree(iio_to_hw_ring_buf(r)); | |
154 | } | |
155 | ||
156 | static IIO_RING_ENABLE_ATTR; | |
ffcab07a | 157 | static IIO_RING_BYTES_PER_DATUM_ATTR; |
574fb258 JC |
158 | static IIO_RING_LENGTH_ATTR; |
159 | ||
25888dc5 JC |
160 | /** |
161 | * sca3000_query_ring_int() is the hardware ring status interrupt enabled | |
162 | **/ | |
163 | static ssize_t sca3000_query_ring_int(struct device *dev, | |
164 | struct device_attribute *attr, | |
165 | char *buf) | |
166 | { | |
167 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
168 | int ret, val; | |
169 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
170 | struct iio_dev *indio_dev = ring->indio_dev; | |
171 | struct sca3000_state *st = indio_dev->dev_data; | |
172 | ||
173 | mutex_lock(&st->lock); | |
174 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1); | |
175 | val = st->rx[0]; | |
176 | mutex_unlock(&st->lock); | |
177 | if (ret) | |
178 | return ret; | |
179 | ||
180 | return sprintf(buf, "%d\n", !!(val & this_attr->address)); | |
181 | } | |
182 | ||
183 | /** | |
184 | * sca3000_set_ring_int() set state of ring status interrupt | |
185 | **/ | |
186 | static ssize_t sca3000_set_ring_int(struct device *dev, | |
187 | struct device_attribute *attr, | |
188 | const char *buf, | |
189 | size_t len) | |
190 | { | |
191 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
192 | struct iio_dev *indio_dev = ring->indio_dev; | |
193 | struct sca3000_state *st = indio_dev->dev_data; | |
194 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
195 | long val; | |
196 | int ret; | |
197 | ||
198 | mutex_lock(&st->lock); | |
199 | ret = strict_strtol(buf, 10, &val); | |
200 | if (ret) | |
201 | goto error_ret; | |
202 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1); | |
203 | if (ret) | |
204 | goto error_ret; | |
205 | if (val) | |
206 | ret = sca3000_write_reg(st, | |
207 | SCA3000_REG_ADDR_INT_MASK, | |
208 | st->rx[0] | this_attr->address); | |
209 | else | |
210 | ret = sca3000_write_reg(st, | |
211 | SCA3000_REG_ADDR_INT_MASK, | |
212 | st->rx[0] & ~this_attr->address); | |
213 | error_ret: | |
214 | mutex_unlock(&st->lock); | |
215 | ||
216 | return ret ? ret : len; | |
217 | } | |
218 | ||
219 | static IIO_DEVICE_ATTR(50_percent, S_IRUGO | S_IWUSR, | |
220 | sca3000_query_ring_int, | |
221 | sca3000_set_ring_int, | |
222 | SCA3000_INT_MASK_RING_HALF); | |
223 | ||
224 | static IIO_DEVICE_ATTR(75_percent, S_IRUGO | S_IWUSR, | |
225 | sca3000_query_ring_int, | |
226 | sca3000_set_ring_int, | |
227 | SCA3000_INT_MASK_RING_THREE_QUARTER); | |
228 | ||
229 | ||
574fb258 JC |
230 | /** |
231 | * sca3000_show_ring_bpse() -sysfs function to query bits per sample from ring | |
232 | * @dev: ring buffer device | |
233 | * @attr: this device attribute | |
234 | * @buf: buffer to write to | |
235 | **/ | |
236 | static ssize_t sca3000_show_ring_bpse(struct device *dev, | |
237 | struct device_attribute *attr, | |
238 | char *buf) | |
239 | { | |
240 | int len = 0, ret; | |
7aa3baeb JC |
241 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
242 | struct iio_dev *indio_dev = ring->indio_dev; | |
b68d58a8 | 243 | struct sca3000_state *st = indio_dev->dev_data; |
574fb258 JC |
244 | |
245 | mutex_lock(&st->lock); | |
25888dc5 | 246 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); |
574fb258 JC |
247 | if (ret) |
248 | goto error_ret; | |
25888dc5 | 249 | if (st->rx[0] & SCA3000_RING_BUF_8BIT) |
ef26b830 JC |
250 | len = sprintf(buf, "s8/8\n"); |
251 | else | |
252 | len = sprintf(buf, "s11/16\n"); | |
574fb258 JC |
253 | error_ret: |
254 | mutex_unlock(&st->lock); | |
255 | ||
256 | return ret ? ret : len; | |
257 | } | |
258 | ||
259 | /** | |
260 | * sca3000_store_ring_bpse() - bits per scan element | |
261 | * @dev: ring buffer device | |
262 | * @attr: attribute called from | |
263 | * @buf: input from userspace | |
264 | * @len: length of input | |
265 | **/ | |
266 | static ssize_t sca3000_store_ring_bpse(struct device *dev, | |
267 | struct device_attribute *attr, | |
268 | const char *buf, | |
269 | size_t len) | |
270 | { | |
7aa3baeb JC |
271 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
272 | struct iio_dev *indio_dev = ring->indio_dev; | |
b68d58a8 | 273 | struct sca3000_state *st = indio_dev->dev_data; |
574fb258 | 274 | int ret; |
574fb258 JC |
275 | |
276 | mutex_lock(&st->lock); | |
277 | ||
25888dc5 | 278 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); |
ef26b830 JC |
279 | if (ret) |
280 | goto error_ret; | |
25888dc5 | 281 | if (sysfs_streq(buf, "s8/8")) { |
ef26b830 | 282 | ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, |
25888dc5 | 283 | st->rx[0] | SCA3000_RING_BUF_8BIT); |
ef26b830 | 284 | st->bpse = 8; |
25888dc5 | 285 | } else if (sysfs_streq(buf, "s11/16")) { |
ef26b830 | 286 | ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, |
25888dc5 | 287 | st->rx[0] & ~SCA3000_RING_BUF_8BIT); |
ef26b830 JC |
288 | st->bpse = 11; |
289 | } else | |
290 | ret = -EINVAL; | |
291 | error_ret: | |
574fb258 JC |
292 | mutex_unlock(&st->lock); |
293 | ||
294 | return ret ? ret : len; | |
295 | } | |
296 | ||
25888dc5 JC |
297 | static ssize_t sca3000_show_buffer_scale(struct device *dev, |
298 | struct device_attribute *attr, | |
299 | char *buf) | |
300 | { | |
301 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
302 | struct iio_dev *indio_dev = ring->indio_dev; | |
303 | struct sca3000_state *st = indio_dev->dev_data; | |
574fb258 | 304 | |
25888dc5 JC |
305 | return sprintf(buf, "0.%06d\n", 4*st->info->scale); |
306 | } | |
f3fb0011 | 307 | |
25888dc5 JC |
308 | static IIO_DEVICE_ATTR(accel_scale, |
309 | S_IRUGO, | |
310 | sca3000_show_buffer_scale, | |
311 | NULL, | |
312 | 0); | |
574fb258 JC |
313 | |
314 | /* | |
315 | * Ring buffer attributes | |
316 | * This device is a bit unusual in that the sampling frequency and bpse | |
317 | * only apply to the ring buffer. At all times full rate and accuracy | |
318 | * is available via direct reading from registers. | |
319 | */ | |
f3fb0011 | 320 | static struct attribute *sca3000_ring_attributes[] = { |
574fb258 | 321 | &dev_attr_length.attr, |
ffcab07a MS |
322 | &dev_attr_bytes_per_datum.attr, |
323 | &dev_attr_enable.attr, | |
25888dc5 JC |
324 | &iio_dev_attr_50_percent.dev_attr.attr, |
325 | &iio_dev_attr_75_percent.dev_attr.attr, | |
326 | &iio_dev_attr_accel_scale.dev_attr.attr, | |
574fb258 JC |
327 | NULL, |
328 | }; | |
329 | ||
330 | static struct attribute_group sca3000_ring_attr = { | |
f3fb0011 | 331 | .attrs = sca3000_ring_attributes, |
574fb258 JC |
332 | }; |
333 | ||
3860dc82 | 334 | static const struct attribute_group *sca3000_ring_attr_groups[] = { |
574fb258 JC |
335 | &sca3000_ring_attr, |
336 | NULL | |
337 | }; | |
338 | ||
339 | static struct device_type sca3000_ring_type = { | |
340 | .release = sca3000_ring_release, | |
341 | .groups = sca3000_ring_attr_groups, | |
342 | }; | |
343 | ||
344 | static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev) | |
345 | { | |
346 | struct iio_ring_buffer *buf; | |
347 | struct iio_hw_ring_buffer *ring; | |
348 | ||
349 | ring = kzalloc(sizeof *ring, GFP_KERNEL); | |
350 | if (!ring) | |
7cfce527 | 351 | return NULL; |
25888dc5 | 352 | |
574fb258 JC |
353 | ring->private = indio_dev; |
354 | buf = &ring->buf; | |
25888dc5 | 355 | buf->stufftoread = 0; |
574fb258 JC |
356 | iio_ring_buffer_init(buf, indio_dev); |
357 | buf->dev.type = &sca3000_ring_type; | |
574fb258 JC |
358 | buf->dev.parent = &indio_dev->dev; |
359 | dev_set_drvdata(&buf->dev, (void *)buf); | |
360 | ||
361 | return buf; | |
362 | } | |
363 | ||
364 | static inline void sca3000_rb_free(struct iio_ring_buffer *r) | |
365 | { | |
366 | if (r) | |
367 | iio_put_ring_buffer(r); | |
368 | } | |
369 | ||
5565a450 JC |
370 | static const struct iio_ring_access_funcs sca3000_ring_access_funcs = { |
371 | .read_first_n = &sca3000_read_first_n_hw_rb, | |
372 | .get_length = &sca3000_ring_get_length, | |
373 | .get_bytes_per_datum = &sca3000_ring_get_bytes_per_datum, | |
374 | }; | |
375 | ||
574fb258 JC |
376 | int sca3000_configure_ring(struct iio_dev *indio_dev) |
377 | { | |
378 | indio_dev->ring = sca3000_rb_allocate(indio_dev); | |
379 | if (indio_dev->ring == NULL) | |
380 | return -ENOMEM; | |
381 | indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER; | |
382 | ||
5565a450 | 383 | indio_dev->ring->access = &sca3000_ring_access_funcs; |
25888dc5 JC |
384 | |
385 | iio_scan_mask_set(indio_dev->ring, 0); | |
386 | iio_scan_mask_set(indio_dev->ring, 1); | |
387 | iio_scan_mask_set(indio_dev->ring, 2); | |
574fb258 JC |
388 | |
389 | return 0; | |
390 | } | |
391 | ||
392 | void sca3000_unconfigure_ring(struct iio_dev *indio_dev) | |
393 | { | |
394 | sca3000_rb_free(indio_dev->ring); | |
395 | } | |
396 | ||
397 | static inline | |
398 | int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state) | |
399 | { | |
400 | struct sca3000_state *st = indio_dev->dev_data; | |
401 | int ret; | |
574fb258 JC |
402 | |
403 | mutex_lock(&st->lock); | |
25888dc5 | 404 | ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1); |
574fb258 JC |
405 | if (ret) |
406 | goto error_ret; | |
407 | if (state) { | |
408 | printk(KERN_INFO "supposedly enabling ring buffer\n"); | |
409 | ret = sca3000_write_reg(st, | |
410 | SCA3000_REG_ADDR_MODE, | |
25888dc5 | 411 | (st->rx[0] | SCA3000_RING_BUF_ENABLE)); |
574fb258 JC |
412 | } else |
413 | ret = sca3000_write_reg(st, | |
414 | SCA3000_REG_ADDR_MODE, | |
25888dc5 | 415 | (st->rx[0] & ~SCA3000_RING_BUF_ENABLE)); |
574fb258 JC |
416 | error_ret: |
417 | mutex_unlock(&st->lock); | |
418 | ||
419 | return ret; | |
420 | } | |
421 | /** | |
422 | * sca3000_hw_ring_preenable() hw ring buffer preenable function | |
423 | * | |
424 | * Very simple enable function as the chip will allows normal reads | |
425 | * during ring buffer operation so as long as it is indeed running | |
426 | * before we notify the core, the precise ordering does not matter. | |
427 | **/ | |
428 | static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev) | |
429 | { | |
430 | return __sca3000_hw_ring_state_set(indio_dev, 1); | |
431 | } | |
432 | ||
433 | static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev) | |
434 | { | |
435 | return __sca3000_hw_ring_state_set(indio_dev, 0); | |
436 | } | |
437 | ||
5565a450 JC |
438 | static const struct iio_ring_setup_ops sca3000_ring_setup_ops = { |
439 | .preenable = &sca3000_hw_ring_preenable, | |
440 | .postdisable = &sca3000_hw_ring_postdisable, | |
441 | }; | |
442 | ||
574fb258 JC |
443 | void sca3000_register_ring_funcs(struct iio_dev *indio_dev) |
444 | { | |
5565a450 | 445 | indio_dev->ring->setup_ops = &sca3000_ring_setup_ops; |
574fb258 JC |
446 | } |
447 | ||
448 | /** | |
449 | * sca3000_ring_int_process() ring specific interrupt handling. | |
450 | * | |
451 | * This is only split from the main interrupt handler so as to | |
452 | * reduce the amount of code if the ring buffer is not enabled. | |
453 | **/ | |
454 | void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring) | |
455 | { | |
25888dc5 JC |
456 | if (val & (SCA3000_INT_STATUS_THREE_QUARTERS | |
457 | SCA3000_INT_STATUS_HALF)) { | |
458 | ring->stufftoread = true; | |
459 | wake_up_interruptible(&ring->pollq); | |
460 | } | |
574fb258 | 461 | } |