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>
23 #include "../ring_generic.h"
24 #include "../ring_hw.h"
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.
38 * sca3000_rip_hw_rb() - main ring access function, pulls data from 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.
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.
49 static int sca3000_rip_hw_rb(struct iio_ring_buffer *r,
50 size_t count, u8 **data, int *dead_offset)
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;
56 int ret, num_available, num_read = 0;
57 int bytes_per_sample = 1;
62 mutex_lock(&st->lock);
63 /* Check how much data is available:
64 * RFC: Implement an ioctl to not bother checking whether there
65 * is enough data in the ring? Afterall, if we are responding
66 * to an interrupt we have a minimum content guaranteed so it
67 * seems slight silly to waste time checking it is there.
69 ret = sca3000_read_data(st,
70 SCA3000_REG_ADDR_BUF_COUNT,
75 num_available = rx[1];
76 /* num_available is the total number of samples available
77 * i.e. number of time points * number of channels.
80 if (count > num_available * bytes_per_sample)
81 num_read = num_available*bytes_per_sample;
83 num_read = count - (count % (bytes_per_sample));
85 /* Avoid the read request byte */
87 ret = sca3000_read_data(st,
88 SCA3000_REG_ADDR_RING_OUT,
91 mutex_unlock(&st->lock);
93 return ret ? ret : num_read;
96 /* This is only valid with all 3 elements enabled */
97 static int sca3000_ring_get_length(struct iio_ring_buffer *r)
102 /* only valid if resolution is kept at 11bits */
103 static int sca3000_ring_get_bpd(struct iio_ring_buffer *r)
107 static void sca3000_ring_release(struct device *dev)
109 struct iio_ring_buffer *r = to_iio_ring_buffer(dev);
110 kfree(iio_to_hw_ring_buf(r));
113 static IIO_RING_ENABLE_ATTR;
114 static IIO_RING_BPS_ATTR;
115 static IIO_RING_LENGTH_ATTR;
118 * sca3000_show_ring_bpse() -sysfs function to query bits per sample from ring
119 * @dev: ring buffer device
120 * @attr: this device attribute
121 * @buf: buffer to write to
123 static ssize_t sca3000_show_ring_bpse(struct device *dev,
124 struct device_attribute *attr,
129 struct iio_ring_buffer *r = dev_get_drvdata(dev);
130 struct sca3000_state *st = r->indio_dev->dev_data;
132 mutex_lock(&st->lock);
133 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
136 len = sprintf(buf, "%d\n", (rx[1] & SCA3000_RING_BUF_8BIT) ? 8 : 11);
139 mutex_unlock(&st->lock);
141 return ret ? ret : len;
145 * sca3000_store_ring_bpse() - bits per scan element
146 * @dev: ring buffer device
147 * @attr: attribute called from
148 * @buf: input from userspace
149 * @len: length of input
151 static ssize_t sca3000_store_ring_bpse(struct device *dev,
152 struct device_attribute *attr,
156 struct iio_ring_buffer *r = dev_get_drvdata(dev);
157 struct sca3000_state *st = r->indio_dev->dev_data;
161 ret = strict_strtol(buf, 10, &val);
165 mutex_lock(&st->lock);
167 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
171 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
172 rx[1] | SCA3000_RING_BUF_8BIT);
176 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
177 rx[1] & ~SCA3000_RING_BUF_8BIT);
184 mutex_unlock(&st->lock);
186 return ret ? ret : len;
189 static IIO_CONST_ATTR(bpse_available, "8 11");
191 static IIO_DEV_ATTR_BPSE(S_IRUGO | S_IWUSR,
192 sca3000_show_ring_bpse,
193 sca3000_store_ring_bpse);
196 * Ring buffer attributes
197 * This device is a bit unusual in that the sampling frequency and bpse
198 * only apply to the ring buffer. At all times full rate and accuracy
199 * is available via direct reading from registers.
201 static struct attribute *iio_ring_attributes[] = {
202 &dev_attr_length.attr,
204 &dev_attr_ring_enable.attr,
205 &iio_dev_attr_bpse.dev_attr.attr,
206 &iio_const_attr_bpse_available.dev_attr.attr,
210 static struct attribute_group sca3000_ring_attr = {
211 .attrs = iio_ring_attributes,
214 static const struct attribute_group *sca3000_ring_attr_groups[] = {
219 static struct device_type sca3000_ring_type = {
220 .release = sca3000_ring_release,
221 .groups = sca3000_ring_attr_groups,
224 static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev)
226 struct iio_ring_buffer *buf;
227 struct iio_hw_ring_buffer *ring;
229 ring = kzalloc(sizeof *ring, GFP_KERNEL);
232 ring->private = indio_dev;
234 iio_ring_buffer_init(buf, indio_dev);
235 buf->dev.type = &sca3000_ring_type;
236 device_initialize(&buf->dev);
237 buf->dev.parent = &indio_dev->dev;
238 dev_set_drvdata(&buf->dev, (void *)buf);
243 static inline void sca3000_rb_free(struct iio_ring_buffer *r)
246 iio_put_ring_buffer(r);
249 int sca3000_configure_ring(struct iio_dev *indio_dev)
251 indio_dev->ring = sca3000_rb_allocate(indio_dev);
252 if (indio_dev->ring == NULL)
254 indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER;
256 indio_dev->ring->access.rip_lots = &sca3000_rip_hw_rb;
257 indio_dev->ring->access.get_length = &sca3000_ring_get_length;
258 indio_dev->ring->access.get_bpd = &sca3000_ring_get_bpd;
263 void sca3000_unconfigure_ring(struct iio_dev *indio_dev)
265 sca3000_rb_free(indio_dev->ring);
269 int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
271 struct sca3000_state *st = indio_dev->dev_data;
275 mutex_lock(&st->lock);
276 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
280 printk(KERN_INFO "supposedly enabling ring buffer\n");
281 ret = sca3000_write_reg(st,
282 SCA3000_REG_ADDR_MODE,
283 (rx[1] | SCA3000_RING_BUF_ENABLE));
285 ret = sca3000_write_reg(st,
286 SCA3000_REG_ADDR_MODE,
287 (rx[1] & ~SCA3000_RING_BUF_ENABLE));
290 mutex_unlock(&st->lock);
295 * sca3000_hw_ring_preenable() hw ring buffer preenable function
297 * Very simple enable function as the chip will allows normal reads
298 * during ring buffer operation so as long as it is indeed running
299 * before we notify the core, the precise ordering does not matter.
301 static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
303 return __sca3000_hw_ring_state_set(indio_dev, 1);
306 static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
308 return __sca3000_hw_ring_state_set(indio_dev, 0);
311 void sca3000_register_ring_funcs(struct iio_dev *indio_dev)
313 indio_dev->ring->preenable = &sca3000_hw_ring_preenable;
314 indio_dev->ring->postdisable = &sca3000_hw_ring_postdisable;
318 * sca3000_ring_int_process() ring specific interrupt handling.
320 * This is only split from the main interrupt handler so as to
321 * reduce the amount of code if the ring buffer is not enabled.
323 void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring)
325 if (val & SCA3000_INT_STATUS_THREE_QUARTERS)
326 iio_push_or_escallate_ring_event(ring,
327 IIO_EVENT_CODE_RING_75_FULL,
329 else if (val & SCA3000_INT_STATUS_HALF)
330 iio_push_ring_event(ring,
331 IIO_EVENT_CODE_RING_50_FULL, 0);