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 approximately 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
;
57 int ret
, i
, num_available
, num_read
= 0;
58 int bytes_per_sample
= 1;
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.
70 ret
= sca3000_read_data(st
,
71 SCA3000_REG_ADDR_BUF_COUNT
,
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.
81 if (count
> num_available
* bytes_per_sample
)
82 num_read
= num_available
*bytes_per_sample
;
84 num_read
= count
- (count
% (bytes_per_sample
));
86 /* Avoid the read request byte */
88 ret
= sca3000_read_data(st
,
89 SCA3000_REG_ADDR_RING_OUT
,
92 /* Convert byte order and shift to default resolution */
94 samples
= (s16
*)(*data
+1);
95 for (i
= 0; i
< (num_read
/2); i
++) {
96 samples
[i
] = be16_to_cpup(
97 (__be16
*)&(samples
[i
]));
103 mutex_unlock(&st
->lock
);
105 return ret
? ret
: num_read
;
108 /* This is only valid with all 3 elements enabled */
109 static int sca3000_ring_get_length(struct iio_ring_buffer
*r
)
114 /* only valid if resolution is kept at 11bits */
115 static int sca3000_ring_get_bytes_per_datum(struct iio_ring_buffer
*r
)
119 static void sca3000_ring_release(struct device
*dev
)
121 struct iio_ring_buffer
*r
= to_iio_ring_buffer(dev
);
122 kfree(iio_to_hw_ring_buf(r
));
125 static IIO_RING_ENABLE_ATTR
;
126 static IIO_RING_BYTES_PER_DATUM_ATTR
;
127 static IIO_RING_LENGTH_ATTR
;
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
135 static ssize_t
sca3000_show_ring_bpse(struct device
*dev
,
136 struct device_attribute
*attr
,
141 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
142 struct iio_dev
*indio_dev
= ring
->indio_dev
;
143 struct sca3000_state
*st
= indio_dev
->dev_data
;
145 mutex_lock(&st
->lock
);
146 ret
= sca3000_read_data(st
, SCA3000_REG_ADDR_MODE
, &rx
, 1);
149 if (rx
[1] & SCA3000_RING_BUF_8BIT
)
150 len
= sprintf(buf
, "s8/8\n");
152 len
= sprintf(buf
, "s11/16\n");
155 mutex_unlock(&st
->lock
);
157 return ret
? ret
: len
;
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
167 static ssize_t
sca3000_store_ring_bpse(struct device
*dev
,
168 struct device_attribute
*attr
,
172 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
173 struct iio_dev
*indio_dev
= ring
->indio_dev
;
174 struct sca3000_state
*st
= indio_dev
->dev_data
;
178 mutex_lock(&st
->lock
);
180 ret
= sca3000_read_data(st
, SCA3000_REG_ADDR_MODE
, &rx
, 1);
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
);
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
);
194 mutex_unlock(&st
->lock
);
196 return ret
? ret
: len
;
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
);
202 static IIO_CONST_ATTR(accel_type_available
, "s8/8 s11/16");
203 static IIO_DEVICE_ATTR(accel_type
,
205 sca3000_show_ring_bpse
,
206 sca3000_store_ring_bpse
,
209 static struct attribute
*sca3000_scan_el_attrs
[] = {
210 &iio_scan_el_accel_x
.dev_attr
.attr
,
211 &iio_const_attr_accel_x_index
.dev_attr
.attr
,
212 &iio_scan_el_accel_y
.dev_attr
.attr
,
213 &iio_const_attr_accel_y_index
.dev_attr
.attr
,
214 &iio_scan_el_accel_z
.dev_attr
.attr
,
215 &iio_const_attr_accel_z_index
.dev_attr
.attr
,
216 &iio_const_attr_accel_type_available
.dev_attr
.attr
,
217 &iio_dev_attr_accel_type
.dev_attr
.attr
,
221 static struct attribute_group sca3000_scan_el_group
= {
222 .attrs
= sca3000_scan_el_attrs
,
223 .name
= "scan_elements",
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.
232 static struct attribute
*sca3000_ring_attributes
[] = {
233 &dev_attr_length
.attr
,
234 &dev_attr_bytes_per_datum
.attr
,
235 &dev_attr_enable
.attr
,
239 static struct attribute_group sca3000_ring_attr
= {
240 .attrs
= sca3000_ring_attributes
,
243 static const struct attribute_group
*sca3000_ring_attr_groups
[] = {
248 static struct device_type sca3000_ring_type
= {
249 .release
= sca3000_ring_release
,
250 .groups
= sca3000_ring_attr_groups
,
253 static struct iio_ring_buffer
*sca3000_rb_allocate(struct iio_dev
*indio_dev
)
255 struct iio_ring_buffer
*buf
;
256 struct iio_hw_ring_buffer
*ring
;
258 ring
= kzalloc(sizeof *ring
, GFP_KERNEL
);
261 ring
->private = indio_dev
;
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
);
272 static inline void sca3000_rb_free(struct iio_ring_buffer
*r
)
275 iio_put_ring_buffer(r
);
278 int sca3000_configure_ring(struct iio_dev
*indio_dev
)
280 indio_dev
->ring
= sca3000_rb_allocate(indio_dev
);
281 if (indio_dev
->ring
== NULL
)
283 indio_dev
->modes
|= INDIO_RING_HARDWARE_BUFFER
;
285 indio_dev
->ring
->scan_el_attrs
= &sca3000_scan_el_group
;
286 indio_dev
->ring
->access
.rip_lots
= &sca3000_rip_hw_rb
;
287 indio_dev
->ring
->access
.get_length
= &sca3000_ring_get_length
;
288 indio_dev
->ring
->access
.get_bytes_per_datum
= &sca3000_ring_get_bytes_per_datum
;
293 void sca3000_unconfigure_ring(struct iio_dev
*indio_dev
)
295 sca3000_rb_free(indio_dev
->ring
);
299 int __sca3000_hw_ring_state_set(struct iio_dev
*indio_dev
, bool state
)
301 struct sca3000_state
*st
= indio_dev
->dev_data
;
305 mutex_lock(&st
->lock
);
306 ret
= sca3000_read_data(st
, SCA3000_REG_ADDR_MODE
, &rx
, 1);
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
));
315 ret
= sca3000_write_reg(st
,
316 SCA3000_REG_ADDR_MODE
,
317 (rx
[1] & ~SCA3000_RING_BUF_ENABLE
));
320 mutex_unlock(&st
->lock
);
325 * sca3000_hw_ring_preenable() hw ring buffer preenable function
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.
331 static int sca3000_hw_ring_preenable(struct iio_dev
*indio_dev
)
333 return __sca3000_hw_ring_state_set(indio_dev
, 1);
336 static int sca3000_hw_ring_postdisable(struct iio_dev
*indio_dev
)
338 return __sca3000_hw_ring_state_set(indio_dev
, 0);
341 void sca3000_register_ring_funcs(struct iio_dev
*indio_dev
)
343 indio_dev
->ring
->preenable
= &sca3000_hw_ring_preenable
;
344 indio_dev
->ring
->postdisable
= &sca3000_hw_ring_postdisable
;
348 * sca3000_ring_int_process() ring specific interrupt handling.
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.
353 void sca3000_ring_int_process(u8 val
, struct iio_ring_buffer
*ring
)
355 if (val
& SCA3000_INT_STATUS_THREE_QUARTERS
)
356 iio_push_or_escallate_ring_event(ring
,
357 IIO_EVENT_CODE_RING_75_FULL
,
359 else if (val
& SCA3000_INT_STATUS_HALF
)
360 iio_push_ring_event(ring
,
361 IIO_EVENT_CODE_RING_50_FULL
, 0);