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> | |
20 | ||
21 | #include "../iio.h" | |
22 | #include "../sysfs.h" | |
23 | #include "../ring_generic.h" | |
24 | #include "../ring_hw.h" | |
25 | #include "accel.h" | |
26 | #include "sca3000.h" | |
27 | ||
28 | /* RFC / future work | |
29 | * | |
30 | * The internal ring buffer doesn't actually change what it holds depending | |
31 | * on which signals are enabled etc, merely whether you can read them. | |
32 | * As such the scan mode selection is somewhat different than for a software | |
33 | * ring buffer and changing it actually covers any data already in the buffer. | |
34 | * Currently scan elements aren't configured so it doesn't matter. | |
35 | */ | |
36 | ||
37 | /** | |
38 | * sca3000_rip_hw_rb() - main ring access function, pulls data from ring | |
39 | * @r: the ring | |
40 | * @count: number of samples to try and pull | |
41 | * @data: output the actual samples pulled from the hw ring | |
42 | * @dead_offset: cheating a bit here: Set to 1 so as to allow for the | |
43 | * leading byte used in bus comms. | |
44 | * | |
45 | * Currently does not provide timestamps. As the hardware doesn't add them they | |
46 | * can only be inferred aproximately from ring buffer events such as 50% full | |
47 | * and knowledge of when buffer was last emptied. This is left to userspace. | |
48 | **/ | |
49 | static int sca3000_rip_hw_rb(struct iio_ring_buffer *r, | |
50 | size_t count, u8 **data, int *dead_offset) | |
51 | { | |
52 | struct iio_hw_ring_buffer *hw_ring = iio_to_hw_ring_buf(r); | |
53 | struct iio_dev *indio_dev = hw_ring->private; | |
54 | struct sca3000_state *st = indio_dev->dev_data; | |
55 | u8 *rx; | |
6267ea65 MS |
56 | s16 *samples; |
57 | int ret, i, num_available, num_read = 0; | |
574fb258 JC |
58 | int bytes_per_sample = 1; |
59 | ||
60 | if (st->bpse == 11) | |
61 | bytes_per_sample = 2; | |
62 | ||
63 | mutex_lock(&st->lock); | |
64 | /* Check how much data is available: | |
65 | * RFC: Implement an ioctl to not bother checking whether there | |
66 | * is enough data in the ring? Afterall, if we are responding | |
67 | * to an interrupt we have a minimum content guaranteed so it | |
68 | * seems slight silly to waste time checking it is there. | |
69 | */ | |
70 | ret = sca3000_read_data(st, | |
71 | SCA3000_REG_ADDR_BUF_COUNT, | |
72 | &rx, 1); | |
73 | if (ret) | |
74 | goto error_ret; | |
75 | else | |
76 | num_available = rx[1]; | |
77 | /* num_available is the total number of samples available | |
78 | * i.e. number of time points * number of channels. | |
79 | */ | |
80 | kfree(rx); | |
81 | if (count > num_available * bytes_per_sample) | |
82 | num_read = num_available*bytes_per_sample; | |
83 | else | |
84 | num_read = count - (count % (bytes_per_sample)); | |
85 | ||
86 | /* Avoid the read request byte */ | |
87 | *dead_offset = 1; | |
88 | ret = sca3000_read_data(st, | |
89 | SCA3000_REG_ADDR_RING_OUT, | |
90 | data, num_read); | |
6267ea65 MS |
91 | |
92 | /* Convert byte order and shift to default resolution */ | |
93 | if (st->bpse == 11) { | |
94 | samples = (s16*)(*data+1); | |
95 | for (i = 0; i < (num_read/2); i++) { | |
96 | samples[i] = be16_to_cpup( | |
97 | (__be16 *)&(samples[i])); | |
98 | samples[i] >>= 3; | |
99 | } | |
100 | } | |
101 | ||
574fb258 JC |
102 | error_ret: |
103 | mutex_unlock(&st->lock); | |
104 | ||
105 | return ret ? ret : num_read; | |
106 | } | |
107 | ||
108 | /* This is only valid with all 3 elements enabled */ | |
109 | static int sca3000_ring_get_length(struct iio_ring_buffer *r) | |
110 | { | |
111 | return 64; | |
112 | } | |
113 | ||
114 | /* only valid if resolution is kept at 11bits */ | |
ffcab07a | 115 | static int sca3000_ring_get_bytes_per_datum(struct iio_ring_buffer *r) |
574fb258 JC |
116 | { |
117 | return 6; | |
118 | } | |
119 | static void sca3000_ring_release(struct device *dev) | |
120 | { | |
121 | struct iio_ring_buffer *r = to_iio_ring_buffer(dev); | |
122 | kfree(iio_to_hw_ring_buf(r)); | |
123 | } | |
124 | ||
125 | static IIO_RING_ENABLE_ATTR; | |
ffcab07a | 126 | static IIO_RING_BYTES_PER_DATUM_ATTR; |
574fb258 JC |
127 | static IIO_RING_LENGTH_ATTR; |
128 | ||
129 | /** | |
130 | * sca3000_show_ring_bpse() -sysfs function to query bits per sample from ring | |
131 | * @dev: ring buffer device | |
132 | * @attr: this device attribute | |
133 | * @buf: buffer to write to | |
134 | **/ | |
135 | static ssize_t sca3000_show_ring_bpse(struct device *dev, | |
136 | struct device_attribute *attr, | |
137 | char *buf) | |
138 | { | |
139 | int len = 0, ret; | |
140 | u8 *rx; | |
7aa3baeb JC |
141 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
142 | struct iio_dev *indio_dev = ring->indio_dev; | |
b68d58a8 | 143 | struct sca3000_state *st = indio_dev->dev_data; |
574fb258 JC |
144 | |
145 | mutex_lock(&st->lock); | |
146 | ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); | |
147 | if (ret) | |
148 | goto error_ret; | |
ef26b830 JC |
149 | if (rx[1] & SCA3000_RING_BUF_8BIT) |
150 | len = sprintf(buf, "s8/8\n"); | |
151 | else | |
152 | len = sprintf(buf, "s11/16\n"); | |
574fb258 JC |
153 | kfree(rx); |
154 | error_ret: | |
155 | mutex_unlock(&st->lock); | |
156 | ||
157 | return ret ? ret : len; | |
158 | } | |
159 | ||
160 | /** | |
161 | * sca3000_store_ring_bpse() - bits per scan element | |
162 | * @dev: ring buffer device | |
163 | * @attr: attribute called from | |
164 | * @buf: input from userspace | |
165 | * @len: length of input | |
166 | **/ | |
167 | static ssize_t sca3000_store_ring_bpse(struct device *dev, | |
168 | struct device_attribute *attr, | |
169 | const char *buf, | |
170 | size_t len) | |
171 | { | |
7aa3baeb JC |
172 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
173 | struct iio_dev *indio_dev = ring->indio_dev; | |
b68d58a8 | 174 | struct sca3000_state *st = indio_dev->dev_data; |
574fb258 JC |
175 | int ret; |
176 | u8 *rx; | |
574fb258 JC |
177 | |
178 | mutex_lock(&st->lock); | |
179 | ||
180 | ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); | |
ef26b830 JC |
181 | if (ret) |
182 | goto error_ret; | |
183 | if (strncmp(buf, "s8/8", 4) == 0) { | |
184 | ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, | |
185 | rx[1] | SCA3000_RING_BUF_8BIT); | |
186 | st->bpse = 8; | |
187 | } else if (strncmp(buf, "s11/16", 5) == 0) { | |
188 | ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE, | |
189 | rx[1] & ~SCA3000_RING_BUF_8BIT); | |
190 | st->bpse = 11; | |
191 | } else | |
192 | ret = -EINVAL; | |
193 | error_ret: | |
574fb258 JC |
194 | mutex_unlock(&st->lock); |
195 | ||
196 | return ret ? ret : len; | |
197 | } | |
198 | ||
44f270de JC |
199 | static IIO_SCAN_EL_C(accel_x, 0, 0, NULL); |
200 | static IIO_SCAN_EL_C(accel_y, 1, 0, NULL); | |
201 | static IIO_SCAN_EL_C(accel_z, 2, 0, NULL); | |
ef26b830 JC |
202 | static IIO_CONST_ATTR(accel_type_available, "s8/8 s11/16"); |
203 | static IIO_DEVICE_ATTR(accel_type, | |
f3fb0011 JC |
204 | S_IRUGO | S_IWUSR, |
205 | sca3000_show_ring_bpse, | |
206 | sca3000_store_ring_bpse, | |
207 | 0); | |
574fb258 | 208 | |
f3fb0011 JC |
209 | static struct attribute *sca3000_scan_el_attrs[] = { |
210 | &iio_scan_el_accel_x.dev_attr.attr, | |
d16061bb | 211 | &iio_const_attr_accel_x_index.dev_attr.attr, |
f3fb0011 | 212 | &iio_scan_el_accel_y.dev_attr.attr, |
d16061bb | 213 | &iio_const_attr_accel_y_index.dev_attr.attr, |
f3fb0011 | 214 | &iio_scan_el_accel_z.dev_attr.attr, |
d16061bb | 215 | &iio_const_attr_accel_z_index.dev_attr.attr, |
ef26b830 JC |
216 | &iio_const_attr_accel_type_available.dev_attr.attr, |
217 | &iio_dev_attr_accel_type.dev_attr.attr, | |
f3fb0011 JC |
218 | NULL |
219 | }; | |
220 | ||
221 | static struct attribute_group sca3000_scan_el_group = { | |
222 | .attrs = sca3000_scan_el_attrs, | |
223 | .name = "scan_elements", | |
224 | }; | |
574fb258 JC |
225 | |
226 | /* | |
227 | * Ring buffer attributes | |
228 | * This device is a bit unusual in that the sampling frequency and bpse | |
229 | * only apply to the ring buffer. At all times full rate and accuracy | |
230 | * is available via direct reading from registers. | |
231 | */ | |
f3fb0011 | 232 | static struct attribute *sca3000_ring_attributes[] = { |
574fb258 | 233 | &dev_attr_length.attr, |
ffcab07a MS |
234 | &dev_attr_bytes_per_datum.attr, |
235 | &dev_attr_enable.attr, | |
574fb258 JC |
236 | NULL, |
237 | }; | |
238 | ||
239 | static struct attribute_group sca3000_ring_attr = { | |
f3fb0011 | 240 | .attrs = sca3000_ring_attributes, |
574fb258 JC |
241 | }; |
242 | ||
3860dc82 | 243 | static const struct attribute_group *sca3000_ring_attr_groups[] = { |
574fb258 JC |
244 | &sca3000_ring_attr, |
245 | NULL | |
246 | }; | |
247 | ||
248 | static struct device_type sca3000_ring_type = { | |
249 | .release = sca3000_ring_release, | |
250 | .groups = sca3000_ring_attr_groups, | |
251 | }; | |
252 | ||
253 | static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev) | |
254 | { | |
255 | struct iio_ring_buffer *buf; | |
256 | struct iio_hw_ring_buffer *ring; | |
257 | ||
258 | ring = kzalloc(sizeof *ring, GFP_KERNEL); | |
259 | if (!ring) | |
7cfce527 | 260 | return NULL; |
574fb258 JC |
261 | ring->private = indio_dev; |
262 | buf = &ring->buf; | |
263 | iio_ring_buffer_init(buf, indio_dev); | |
264 | buf->dev.type = &sca3000_ring_type; | |
265 | device_initialize(&buf->dev); | |
266 | buf->dev.parent = &indio_dev->dev; | |
267 | dev_set_drvdata(&buf->dev, (void *)buf); | |
268 | ||
269 | return buf; | |
270 | } | |
271 | ||
272 | static inline void sca3000_rb_free(struct iio_ring_buffer *r) | |
273 | { | |
274 | if (r) | |
275 | iio_put_ring_buffer(r); | |
276 | } | |
277 | ||
278 | int sca3000_configure_ring(struct iio_dev *indio_dev) | |
279 | { | |
280 | indio_dev->ring = sca3000_rb_allocate(indio_dev); | |
281 | if (indio_dev->ring == NULL) | |
282 | return -ENOMEM; | |
283 | indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER; | |
284 | ||
bf32963c | 285 | indio_dev->ring->scan_el_attrs = &sca3000_scan_el_group; |
574fb258 JC |
286 | indio_dev->ring->access.rip_lots = &sca3000_rip_hw_rb; |
287 | indio_dev->ring->access.get_length = &sca3000_ring_get_length; | |
ffcab07a | 288 | indio_dev->ring->access.get_bytes_per_datum = &sca3000_ring_get_bytes_per_datum; |
574fb258 JC |
289 | |
290 | return 0; | |
291 | } | |
292 | ||
293 | void sca3000_unconfigure_ring(struct iio_dev *indio_dev) | |
294 | { | |
295 | sca3000_rb_free(indio_dev->ring); | |
296 | } | |
297 | ||
298 | static inline | |
299 | int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state) | |
300 | { | |
301 | struct sca3000_state *st = indio_dev->dev_data; | |
302 | int ret; | |
303 | u8 *rx; | |
304 | ||
305 | mutex_lock(&st->lock); | |
306 | ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1); | |
307 | if (ret) | |
308 | goto error_ret; | |
309 | if (state) { | |
310 | printk(KERN_INFO "supposedly enabling ring buffer\n"); | |
311 | ret = sca3000_write_reg(st, | |
312 | SCA3000_REG_ADDR_MODE, | |
313 | (rx[1] | SCA3000_RING_BUF_ENABLE)); | |
314 | } else | |
315 | ret = sca3000_write_reg(st, | |
316 | SCA3000_REG_ADDR_MODE, | |
317 | (rx[1] & ~SCA3000_RING_BUF_ENABLE)); | |
318 | kfree(rx); | |
319 | error_ret: | |
320 | mutex_unlock(&st->lock); | |
321 | ||
322 | return ret; | |
323 | } | |
324 | /** | |
325 | * sca3000_hw_ring_preenable() hw ring buffer preenable function | |
326 | * | |
327 | * Very simple enable function as the chip will allows normal reads | |
328 | * during ring buffer operation so as long as it is indeed running | |
329 | * before we notify the core, the precise ordering does not matter. | |
330 | **/ | |
331 | static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev) | |
332 | { | |
333 | return __sca3000_hw_ring_state_set(indio_dev, 1); | |
334 | } | |
335 | ||
336 | static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev) | |
337 | { | |
338 | return __sca3000_hw_ring_state_set(indio_dev, 0); | |
339 | } | |
340 | ||
341 | void sca3000_register_ring_funcs(struct iio_dev *indio_dev) | |
342 | { | |
343 | indio_dev->ring->preenable = &sca3000_hw_ring_preenable; | |
344 | indio_dev->ring->postdisable = &sca3000_hw_ring_postdisable; | |
345 | } | |
346 | ||
347 | /** | |
348 | * sca3000_ring_int_process() ring specific interrupt handling. | |
349 | * | |
350 | * This is only split from the main interrupt handler so as to | |
351 | * reduce the amount of code if the ring buffer is not enabled. | |
352 | **/ | |
353 | void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring) | |
354 | { | |
355 | if (val & SCA3000_INT_STATUS_THREE_QUARTERS) | |
356 | iio_push_or_escallate_ring_event(ring, | |
357 | IIO_EVENT_CODE_RING_75_FULL, | |
358 | 0); | |
359 | else if (val & SCA3000_INT_STATUS_HALF) | |
360 | iio_push_ring_event(ring, | |
361 | IIO_EVENT_CODE_RING_50_FULL, 0); | |
362 | } |