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@cam.ac.uk>
12 #include <linux/interrupt.h>
13 #include <linux/gpio.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/kernel.h>
18 #include <linux/spi/spi.h>
19 #include <linux/sysfs.h>
20 #include <linux/sched.h>
21 #include <linux/poll.h>
25 #include "../ring_generic.h"
26 #include "../ring_hw.h"
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.
39 static int sca3000_read_data(struct sca3000_state *st,
40 uint8_t reg_address_high,
45 struct spi_message msg;
46 struct spi_transfer xfer[2] = {
54 *rx_p = kmalloc(len, GFP_KERNEL);
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);
66 dev_err(get_device(&st->us->dev), "problem reading register");
78 * sca3000_read_first_n_hw_rb() - main ring access, pulls data from ring
80 * @count: number of samples to try and pull
81 * @data: output the actual samples pulled from the hw ring
83 * Currently does not provide timestamps. As the hardware doesn't add them they
84 * can only be inferred approximately from ring buffer events such as 50% full
85 * and knowledge of when buffer was last emptied. This is left to userspace.
87 static int sca3000_read_first_n_hw_rb(struct iio_ring_buffer *r,
88 size_t count, char __user *buf)
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 = iio_priv(indio_dev);
94 int ret, i, num_available, num_read = 0;
95 int bytes_per_sample = 1;
100 mutex_lock(&st->lock);
101 if (count % bytes_per_sample) {
106 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_BUF_COUNT, 1);
110 num_available = st->rx[0];
112 * num_available is the total number of samples available
113 * i.e. number of time points * number of channels.
115 if (count > num_available * bytes_per_sample)
116 num_read = num_available*bytes_per_sample;
120 ret = sca3000_read_data(st,
121 SCA3000_REG_ADDR_RING_OUT,
126 for (i = 0; i < num_read; i++)
127 *(((u16 *)rx) + i) = be16_to_cpup((u16 *)rx + i);
129 if (copy_to_user(buf, rx, num_read))
134 mutex_unlock(&st->lock);
136 return ret ? ret : num_read;
139 /* This is only valid with all 3 elements enabled */
140 static int sca3000_ring_get_length(struct iio_ring_buffer *r)
145 /* only valid if resolution is kept at 11bits */
146 static int sca3000_ring_get_bytes_per_datum(struct iio_ring_buffer *r)
150 static void sca3000_ring_release(struct device *dev)
152 struct iio_ring_buffer *r = to_iio_ring_buffer(dev);
153 kfree(iio_to_hw_ring_buf(r));
156 static IIO_RING_ENABLE_ATTR;
157 static IIO_RING_BYTES_PER_DATUM_ATTR;
158 static IIO_RING_LENGTH_ATTR;
161 * sca3000_query_ring_int() is the hardware ring status interrupt enabled
163 static ssize_t sca3000_query_ring_int(struct device *dev,
164 struct device_attribute *attr,
167 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
169 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
170 struct iio_dev *indio_dev = ring->indio_dev;
171 struct sca3000_state *st = iio_priv(indio_dev);
173 mutex_lock(&st->lock);
174 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
176 mutex_unlock(&st->lock);
180 return sprintf(buf, "%d\n", !!(val & this_attr->address));
184 * sca3000_set_ring_int() set state of ring status interrupt
186 static ssize_t sca3000_set_ring_int(struct device *dev,
187 struct device_attribute *attr,
191 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
192 struct iio_dev *indio_dev = ring->indio_dev;
193 struct sca3000_state *st = iio_priv(indio_dev);
194 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
198 mutex_lock(&st->lock);
199 ret = strict_strtol(buf, 10, &val);
202 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
206 ret = sca3000_write_reg(st,
207 SCA3000_REG_ADDR_INT_MASK,
208 st->rx[0] | this_attr->address);
210 ret = sca3000_write_reg(st,
211 SCA3000_REG_ADDR_INT_MASK,
212 st->rx[0] & ~this_attr->address);
214 mutex_unlock(&st->lock);
216 return ret ? ret : len;
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);
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);
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
236 static ssize_t sca3000_show_ring_bpse(struct device *dev,
237 struct device_attribute *attr,
241 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
242 struct iio_dev *indio_dev = ring->indio_dev;
243 struct sca3000_state *st = iio_priv(indio_dev);
245 mutex_lock(&st->lock);
246 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
249 if (st->rx[0] & SCA3000_RING_BUF_8BIT)
250 len = sprintf(buf, "s8/8\n");
252 len = sprintf(buf, "s11/16\n");
254 mutex_unlock(&st->lock);
256 return ret ? ret : len;
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
266 static ssize_t sca3000_store_ring_bpse(struct device *dev,
267 struct device_attribute *attr,
271 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
272 struct iio_dev *indio_dev = ring->indio_dev;
273 struct sca3000_state *st = iio_priv(indio_dev);
276 mutex_lock(&st->lock);
278 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
281 if (sysfs_streq(buf, "s8/8")) {
282 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
283 st->rx[0] | SCA3000_RING_BUF_8BIT);
285 } else if (sysfs_streq(buf, "s11/16")) {
286 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
287 st->rx[0] & ~SCA3000_RING_BUF_8BIT);
292 mutex_unlock(&st->lock);
294 return ret ? ret : len;
297 static ssize_t sca3000_show_buffer_scale(struct device *dev,
298 struct device_attribute *attr,
301 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
302 struct iio_dev *indio_dev = ring->indio_dev;
303 struct sca3000_state *st = iio_priv(indio_dev);
305 return sprintf(buf, "0.%06d\n", 4*st->info->scale);
308 static IIO_DEVICE_ATTR(accel_scale,
310 sca3000_show_buffer_scale,
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.
320 static struct attribute *sca3000_ring_attributes[] = {
321 &dev_attr_length.attr,
322 &dev_attr_bytes_per_datum.attr,
323 &dev_attr_enable.attr,
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,
330 static struct attribute_group sca3000_ring_attr = {
331 .attrs = sca3000_ring_attributes,
334 static const struct attribute_group *sca3000_ring_attr_groups[] = {
339 static struct device_type sca3000_ring_type = {
340 .release = sca3000_ring_release,
341 .groups = sca3000_ring_attr_groups,
344 static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev)
346 struct iio_ring_buffer *buf;
347 struct iio_hw_ring_buffer *ring;
349 ring = kzalloc(sizeof *ring, GFP_KERNEL);
353 ring->private = indio_dev;
355 buf->stufftoread = 0;
356 iio_ring_buffer_init(buf, indio_dev);
357 buf->dev.type = &sca3000_ring_type;
358 buf->dev.parent = &indio_dev->dev;
359 dev_set_drvdata(&buf->dev, (void *)buf);
364 static inline void sca3000_rb_free(struct iio_ring_buffer *r)
367 iio_put_ring_buffer(r);
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,
376 int sca3000_configure_ring(struct iio_dev *indio_dev)
378 indio_dev->ring = sca3000_rb_allocate(indio_dev);
379 if (indio_dev->ring == NULL)
381 indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER;
383 indio_dev->ring->access = &sca3000_ring_access_funcs;
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);
392 void sca3000_unconfigure_ring(struct iio_dev *indio_dev)
394 sca3000_rb_free(indio_dev->ring);
398 int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
400 struct sca3000_state *st = iio_priv(indio_dev);
403 mutex_lock(&st->lock);
404 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
408 printk(KERN_INFO "supposedly enabling ring buffer\n");
409 ret = sca3000_write_reg(st,
410 SCA3000_REG_ADDR_MODE,
411 (st->rx[0] | SCA3000_RING_BUF_ENABLE));
413 ret = sca3000_write_reg(st,
414 SCA3000_REG_ADDR_MODE,
415 (st->rx[0] & ~SCA3000_RING_BUF_ENABLE));
417 mutex_unlock(&st->lock);
422 * sca3000_hw_ring_preenable() hw ring buffer preenable function
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.
428 static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
430 return __sca3000_hw_ring_state_set(indio_dev, 1);
433 static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
435 return __sca3000_hw_ring_state_set(indio_dev, 0);
438 static const struct iio_ring_setup_ops sca3000_ring_setup_ops = {
439 .preenable = &sca3000_hw_ring_preenable,
440 .postdisable = &sca3000_hw_ring_postdisable,
443 void sca3000_register_ring_funcs(struct iio_dev *indio_dev)
445 indio_dev->ring->setup_ops = &sca3000_ring_setup_ops;
449 * sca3000_ring_int_process() ring specific interrupt handling.
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.
454 void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring)
456 if (val & (SCA3000_INT_STATUS_THREE_QUARTERS |
457 SCA3000_INT_STATUS_HALF)) {
458 ring->stufftoread = true;
459 wake_up_interruptible(&ring->pollq);