staging: iio: push the main buffer chrdev down to the top level.
[deliverable/linux.git] / drivers / staging / iio / accel / sca3000_ring.c
CommitLineData
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>
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
JC
20
21#include "../iio.h"
22#include "../sysfs.h"
23#include "../ring_generic.h"
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
36static 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;
42 struct spi_message msg;
43 struct spi_transfer xfer[2] = {
44 {
45 .len = 1,
46 .tx_buf = st->tx,
47 }, {
48 .len = len,
49 }
50 };
51 *rx_p = kmalloc(len, GFP_KERNEL);
52 if (*rx_p == NULL) {
53 ret = -ENOMEM;
54 goto error_ret;
55 }
56 xfer[1].rx_buf = *rx_p;
57 st->tx[0] = SCA3000_READ_REG(reg_address_high);
58 spi_message_init(&msg);
59 spi_message_add_tail(&xfer[0], &msg);
60 spi_message_add_tail(&xfer[1], &msg);
61 ret = spi_sync(st->us, &msg);
62 if (ret) {
63 dev_err(get_device(&st->us->dev), "problem reading register");
64 goto error_free_rx;
65 }
66
67 return 0;
68error_free_rx:
69 kfree(*rx_p);
70error_ret:
71 return ret;
72}
73
574fb258 74/**
b4281733 75 * sca3000_read_first_n_hw_rb() - main ring access, pulls data from ring
574fb258
JC
76 * @r: the ring
77 * @count: number of samples to try and pull
78 * @data: output the actual samples pulled from the hw ring
574fb258
JC
79 *
80 * Currently does not provide timestamps. As the hardware doesn't add them they
25985edc 81 * can only be inferred approximately from ring buffer events such as 50% full
574fb258
JC
82 * and knowledge of when buffer was last emptied. This is left to userspace.
83 **/
b4281733 84static int sca3000_read_first_n_hw_rb(struct iio_ring_buffer *r,
b26a2188 85 size_t count, char __user *buf)
574fb258
JC
86{
87 struct iio_hw_ring_buffer *hw_ring = iio_to_hw_ring_buf(r);
88 struct iio_dev *indio_dev = hw_ring->private;
83f0422d 89 struct sca3000_state *st = iio_priv(indio_dev);
574fb258 90 u8 *rx;
6267ea65 91 int ret, i, num_available, num_read = 0;
574fb258
JC
92 int bytes_per_sample = 1;
93
94 if (st->bpse == 11)
95 bytes_per_sample = 2;
96
97 mutex_lock(&st->lock);
25888dc5
JC
98 if (count % bytes_per_sample) {
99 ret = -EINVAL;
100 goto error_ret;
101 }
102
103 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_BUF_COUNT, 1);
574fb258
JC
104 if (ret)
105 goto error_ret;
106 else
25888dc5
JC
107 num_available = st->rx[0];
108 /*
109 * num_available is the total number of samples available
574fb258
JC
110 * i.e. number of time points * number of channels.
111 */
574fb258
JC
112 if (count > num_available * bytes_per_sample)
113 num_read = num_available*bytes_per_sample;
114 else
25888dc5 115 num_read = count;
574fb258 116
574fb258
JC
117 ret = sca3000_read_data(st,
118 SCA3000_REG_ADDR_RING_OUT,
25888dc5
JC
119 &rx, num_read);
120 if (ret)
121 goto error_ret;
122
123 for (i = 0; i < num_read; i++)
124 *(((u16 *)rx) + i) = be16_to_cpup((u16 *)rx + i);
6267ea65 125
25888dc5
JC
126 if (copy_to_user(buf, rx, num_read))
127 ret = -EFAULT;
128 kfree(rx);
129 r->stufftoread = 0;
574fb258
JC
130error_ret:
131 mutex_unlock(&st->lock);
132
133 return ret ? ret : num_read;
134}
135
136/* This is only valid with all 3 elements enabled */
137static int sca3000_ring_get_length(struct iio_ring_buffer *r)
138{
139 return 64;
140}
141
142/* only valid if resolution is kept at 11bits */
ffcab07a 143static int sca3000_ring_get_bytes_per_datum(struct iio_ring_buffer *r)
574fb258
JC
144{
145 return 6;
146}
574fb258
JC
147
148static IIO_RING_ENABLE_ATTR;
ffcab07a 149static IIO_RING_BYTES_PER_DATUM_ATTR;
574fb258
JC
150static IIO_RING_LENGTH_ATTR;
151
25888dc5
JC
152/**
153 * sca3000_query_ring_int() is the hardware ring status interrupt enabled
154 **/
155static ssize_t sca3000_query_ring_int(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
158{
159 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
160 int ret, val;
161 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
162 struct iio_dev *indio_dev = ring->indio_dev;
83f0422d 163 struct sca3000_state *st = iio_priv(indio_dev);
25888dc5
JC
164
165 mutex_lock(&st->lock);
166 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
167 val = st->rx[0];
168 mutex_unlock(&st->lock);
169 if (ret)
170 return ret;
171
172 return sprintf(buf, "%d\n", !!(val & this_attr->address));
173}
174
175/**
176 * sca3000_set_ring_int() set state of ring status interrupt
177 **/
178static ssize_t sca3000_set_ring_int(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf,
181 size_t len)
182{
183 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
184 struct iio_dev *indio_dev = ring->indio_dev;
83f0422d 185 struct sca3000_state *st = iio_priv(indio_dev);
25888dc5
JC
186 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
187 long val;
188 int ret;
189
190 mutex_lock(&st->lock);
191 ret = strict_strtol(buf, 10, &val);
192 if (ret)
193 goto error_ret;
194 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
195 if (ret)
196 goto error_ret;
197 if (val)
198 ret = sca3000_write_reg(st,
199 SCA3000_REG_ADDR_INT_MASK,
200 st->rx[0] | this_attr->address);
201 else
202 ret = sca3000_write_reg(st,
203 SCA3000_REG_ADDR_INT_MASK,
204 st->rx[0] & ~this_attr->address);
205error_ret:
206 mutex_unlock(&st->lock);
207
208 return ret ? ret : len;
209}
210
211static IIO_DEVICE_ATTR(50_percent, S_IRUGO | S_IWUSR,
212 sca3000_query_ring_int,
213 sca3000_set_ring_int,
214 SCA3000_INT_MASK_RING_HALF);
215
216static IIO_DEVICE_ATTR(75_percent, S_IRUGO | S_IWUSR,
217 sca3000_query_ring_int,
218 sca3000_set_ring_int,
219 SCA3000_INT_MASK_RING_THREE_QUARTER);
220
25888dc5
JC
221static ssize_t sca3000_show_buffer_scale(struct device *dev,
222 struct device_attribute *attr,
223 char *buf)
224{
225 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
226 struct iio_dev *indio_dev = ring->indio_dev;
83f0422d 227 struct sca3000_state *st = iio_priv(indio_dev);
574fb258 228
25888dc5
JC
229 return sprintf(buf, "0.%06d\n", 4*st->info->scale);
230}
f3fb0011 231
25888dc5
JC
232static IIO_DEVICE_ATTR(accel_scale,
233 S_IRUGO,
234 sca3000_show_buffer_scale,
235 NULL,
236 0);
574fb258
JC
237
238/*
239 * Ring buffer attributes
240 * This device is a bit unusual in that the sampling frequency and bpse
241 * only apply to the ring buffer. At all times full rate and accuracy
242 * is available via direct reading from registers.
243 */
f3fb0011 244static struct attribute *sca3000_ring_attributes[] = {
574fb258 245 &dev_attr_length.attr,
ffcab07a
MS
246 &dev_attr_bytes_per_datum.attr,
247 &dev_attr_enable.attr,
25888dc5
JC
248 &iio_dev_attr_50_percent.dev_attr.attr,
249 &iio_dev_attr_75_percent.dev_attr.attr,
250 &iio_dev_attr_accel_scale.dev_attr.attr,
574fb258
JC
251 NULL,
252};
253
254static struct attribute_group sca3000_ring_attr = {
f3fb0011 255 .attrs = sca3000_ring_attributes,
1aa04278 256 .name = "buffer",
574fb258
JC
257};
258
259static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev)
260{
261 struct iio_ring_buffer *buf;
262 struct iio_hw_ring_buffer *ring;
263
264 ring = kzalloc(sizeof *ring, GFP_KERNEL);
265 if (!ring)
7cfce527 266 return NULL;
25888dc5 267
574fb258
JC
268 ring->private = indio_dev;
269 buf = &ring->buf;
25888dc5 270 buf->stufftoread = 0;
1aa04278 271 buf->attrs = &sca3000_ring_attr;
574fb258 272 iio_ring_buffer_init(buf, indio_dev);
574fb258
JC
273
274 return buf;
275}
276
277static inline void sca3000_rb_free(struct iio_ring_buffer *r)
278{
1aa04278 279 kfree(iio_to_hw_ring_buf(r));
574fb258
JC
280}
281
5565a450
JC
282static const struct iio_ring_access_funcs sca3000_ring_access_funcs = {
283 .read_first_n = &sca3000_read_first_n_hw_rb,
284 .get_length = &sca3000_ring_get_length,
285 .get_bytes_per_datum = &sca3000_ring_get_bytes_per_datum,
286};
287
574fb258
JC
288int sca3000_configure_ring(struct iio_dev *indio_dev)
289{
290 indio_dev->ring = sca3000_rb_allocate(indio_dev);
291 if (indio_dev->ring == NULL)
292 return -ENOMEM;
293 indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER;
294
5565a450 295 indio_dev->ring->access = &sca3000_ring_access_funcs;
25888dc5
JC
296
297 iio_scan_mask_set(indio_dev->ring, 0);
298 iio_scan_mask_set(indio_dev->ring, 1);
299 iio_scan_mask_set(indio_dev->ring, 2);
574fb258
JC
300
301 return 0;
302}
303
304void sca3000_unconfigure_ring(struct iio_dev *indio_dev)
305{
306 sca3000_rb_free(indio_dev->ring);
307}
308
309static inline
310int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
311{
83f0422d 312 struct sca3000_state *st = iio_priv(indio_dev);
574fb258 313 int ret;
574fb258
JC
314
315 mutex_lock(&st->lock);
25888dc5 316 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
574fb258
JC
317 if (ret)
318 goto error_ret;
319 if (state) {
320 printk(KERN_INFO "supposedly enabling ring buffer\n");
321 ret = sca3000_write_reg(st,
322 SCA3000_REG_ADDR_MODE,
25888dc5 323 (st->rx[0] | SCA3000_RING_BUF_ENABLE));
574fb258
JC
324 } else
325 ret = sca3000_write_reg(st,
326 SCA3000_REG_ADDR_MODE,
25888dc5 327 (st->rx[0] & ~SCA3000_RING_BUF_ENABLE));
574fb258
JC
328error_ret:
329 mutex_unlock(&st->lock);
330
331 return ret;
332}
333/**
334 * sca3000_hw_ring_preenable() hw ring buffer preenable function
335 *
336 * Very simple enable function as the chip will allows normal reads
337 * during ring buffer operation so as long as it is indeed running
338 * before we notify the core, the precise ordering does not matter.
339 **/
340static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
341{
342 return __sca3000_hw_ring_state_set(indio_dev, 1);
343}
344
345static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
346{
347 return __sca3000_hw_ring_state_set(indio_dev, 0);
348}
349
5565a450
JC
350static const struct iio_ring_setup_ops sca3000_ring_setup_ops = {
351 .preenable = &sca3000_hw_ring_preenable,
352 .postdisable = &sca3000_hw_ring_postdisable,
353};
354
574fb258
JC
355void sca3000_register_ring_funcs(struct iio_dev *indio_dev)
356{
5565a450 357 indio_dev->ring->setup_ops = &sca3000_ring_setup_ops;
574fb258
JC
358}
359
360/**
361 * sca3000_ring_int_process() ring specific interrupt handling.
362 *
363 * This is only split from the main interrupt handler so as to
364 * reduce the amount of code if the ring buffer is not enabled.
365 **/
366void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring)
367{
25888dc5
JC
368 if (val & (SCA3000_INT_STATUS_THREE_QUARTERS |
369 SCA3000_INT_STATUS_HALF)) {
370 ring->stufftoread = true;
371 wake_up_interruptible(&ring->pollq);
372 }
574fb258 373}
This page took 0.418611 seconds and 5 git commands to generate.