2 * sca3000_ring.c -- support VTI sca3000 series accelerometers via SPI
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.
8 * Copyright (c) 2009 Jonathan Cameron <jic23@kernel.org>
12 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/sysfs.h>
18 #include <linux/sched.h>
19 #include <linux/poll.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include "../ring_hw.h"
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.
36 static int sca3000_read_data(struct sca3000_state *st,
42 struct spi_transfer xfer[2] = {
50 *rx_p = kmalloc(len, GFP_KERNEL);
55 xfer[1].rx_buf = *rx_p;
56 st->tx[0] = SCA3000_READ_REG(reg_address_high);
57 ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
59 dev_err(&st->us->dev, "problem reading register");
71 * sca3000_read_first_n_hw_rb() - main ring access, pulls data from ring
73 * @count: number of samples to try and pull
74 * @data: output the actual samples pulled from the hw ring
76 * Currently does not provide timestamps. As the hardware doesn't add them they
77 * can only be inferred approximately from ring buffer events such as 50% full
78 * and knowledge of when buffer was last emptied. This is left to userspace.
80 static int sca3000_read_first_n_hw_rb(struct iio_buffer *r,
81 size_t count, char __user *buf)
83 struct iio_hw_buffer *hw_ring = iio_to_hw_buf(r);
84 struct iio_dev *indio_dev = hw_ring->private;
85 struct sca3000_state *st = iio_priv(indio_dev);
87 int ret, i, num_available, num_read = 0;
88 int bytes_per_sample = 1;
93 mutex_lock(&st->lock);
94 if (count % bytes_per_sample) {
99 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_BUF_COUNT, 1);
102 num_available = st->rx[0];
104 * num_available is the total number of samples available
105 * i.e. number of time points * number of channels.
107 if (count > num_available * bytes_per_sample)
108 num_read = num_available * bytes_per_sample;
112 ret = sca3000_read_data(st,
113 SCA3000_REG_ADDR_RING_OUT,
118 for (i = 0; i < num_read / sizeof(u16); i++)
119 *(((u16 *)rx) + i) = be16_to_cpup((__be16 *)rx + i);
121 if (copy_to_user(buf, rx, num_read))
126 mutex_unlock(&st->lock);
128 return ret ? ret : num_read;
131 static size_t sca3000_ring_buf_data_available(struct iio_buffer *r)
133 return r->stufftoread ? r->watermark : 0;
137 * sca3000_query_ring_int() is the hardware ring status interrupt enabled
139 static ssize_t sca3000_query_ring_int(struct device *dev,
140 struct device_attribute *attr,
143 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
145 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
146 struct sca3000_state *st = iio_priv(indio_dev);
148 mutex_lock(&st->lock);
149 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
151 mutex_unlock(&st->lock);
155 return sprintf(buf, "%d\n", !!(val & this_attr->address));
159 * sca3000_set_ring_int() set state of ring status interrupt
161 static ssize_t sca3000_set_ring_int(struct device *dev,
162 struct device_attribute *attr,
166 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
167 struct sca3000_state *st = iio_priv(indio_dev);
168 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
172 mutex_lock(&st->lock);
173 ret = kstrtou8(buf, 10, &val);
176 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
180 ret = sca3000_write_reg(st,
181 SCA3000_REG_ADDR_INT_MASK,
182 st->rx[0] | this_attr->address);
184 ret = sca3000_write_reg(st,
185 SCA3000_REG_ADDR_INT_MASK,
186 st->rx[0] & ~this_attr->address);
188 mutex_unlock(&st->lock);
190 return ret ? ret : len;
193 static IIO_DEVICE_ATTR(50_percent, S_IRUGO | S_IWUSR,
194 sca3000_query_ring_int,
195 sca3000_set_ring_int,
196 SCA3000_INT_MASK_RING_HALF);
198 static IIO_DEVICE_ATTR(75_percent, S_IRUGO | S_IWUSR,
199 sca3000_query_ring_int,
200 sca3000_set_ring_int,
201 SCA3000_INT_MASK_RING_THREE_QUARTER);
203 static ssize_t sca3000_show_buffer_scale(struct device *dev,
204 struct device_attribute *attr,
207 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
208 struct sca3000_state *st = iio_priv(indio_dev);
210 return sprintf(buf, "0.%06d\n", 4 * st->info->scale);
213 static IIO_DEVICE_ATTR(in_accel_scale,
215 sca3000_show_buffer_scale,
220 * Ring buffer attributes
221 * This device is a bit unusual in that the sampling frequency and bpse
222 * only apply to the ring buffer. At all times full rate and accuracy
223 * is available via direct reading from registers.
225 static const struct attribute *sca3000_ring_attributes[] = {
226 &iio_dev_attr_50_percent.dev_attr.attr,
227 &iio_dev_attr_75_percent.dev_attr.attr,
228 &iio_dev_attr_in_accel_scale.dev_attr.attr,
232 static struct iio_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev)
234 struct iio_buffer *buf;
235 struct iio_hw_buffer *ring;
237 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
241 ring->private = indio_dev;
243 buf->stufftoread = 0;
245 buf->attrs = sca3000_ring_attributes;
246 iio_buffer_init(buf);
251 static void sca3000_ring_release(struct iio_buffer *r)
253 kfree(iio_to_hw_buf(r));
256 static const struct iio_buffer_access_funcs sca3000_ring_access_funcs = {
257 .read_first_n = &sca3000_read_first_n_hw_rb,
258 .data_available = sca3000_ring_buf_data_available,
259 .release = sca3000_ring_release,
261 .modes = INDIO_BUFFER_HARDWARE,
264 int sca3000_configure_ring(struct iio_dev *indio_dev)
266 struct iio_buffer *buffer;
268 buffer = sca3000_rb_allocate(indio_dev);
271 indio_dev->modes |= INDIO_BUFFER_HARDWARE;
273 indio_dev->buffer->access = &sca3000_ring_access_funcs;
275 iio_device_attach_buffer(indio_dev, buffer);
280 void sca3000_unconfigure_ring(struct iio_dev *indio_dev)
282 iio_buffer_put(indio_dev->buffer);
286 int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
288 struct sca3000_state *st = iio_priv(indio_dev);
291 mutex_lock(&st->lock);
292 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
296 dev_info(&indio_dev->dev, "supposedly enabling ring buffer\n");
297 ret = sca3000_write_reg(st,
298 SCA3000_REG_ADDR_MODE,
299 (st->rx[0] | SCA3000_RING_BUF_ENABLE));
301 ret = sca3000_write_reg(st,
302 SCA3000_REG_ADDR_MODE,
303 (st->rx[0] & ~SCA3000_RING_BUF_ENABLE));
305 mutex_unlock(&st->lock);
311 * sca3000_hw_ring_preenable() hw ring buffer preenable function
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.
317 static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
319 return __sca3000_hw_ring_state_set(indio_dev, 1);
322 static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
324 return __sca3000_hw_ring_state_set(indio_dev, 0);
327 static const struct iio_buffer_setup_ops sca3000_ring_setup_ops = {
328 .preenable = &sca3000_hw_ring_preenable,
329 .postdisable = &sca3000_hw_ring_postdisable,
332 void sca3000_register_ring_funcs(struct iio_dev *indio_dev)
334 indio_dev->setup_ops = &sca3000_ring_setup_ops;
338 * sca3000_ring_int_process() ring specific interrupt handling.
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.
343 void sca3000_ring_int_process(u8 val, struct iio_buffer *ring)
345 if (val & (SCA3000_INT_STATUS_THREE_QUARTERS |
346 SCA3000_INT_STATUS_HALF)) {
347 ring->stufftoread = true;
348 wake_up_interruptible(&ring->pollq);