• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/staging/iio/accel/
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>
13#include <linux/gpio.h>
14#include <linux/fs.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
21#include "../iio.h"
22#include "../sysfs.h"
23#include "../ring_generic.h"
24#include "../ring_hw.h"
25#include "accel.h"
26#include "sca3000.h"
27
28/* RFC / future work
29 *
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.
35 */
36
37/**
38 * sca3000_rip_hw_rb() - main ring access function, pulls data from ring
39 * @r:			the 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.
44 *
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.
48 **/
49static int sca3000_rip_hw_rb(struct iio_ring_buffer *r,
50			     size_t count, u8 **data, int *dead_offset)
51{
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;
55	u8 *rx;
56	int ret, num_available, num_read = 0;
57	int bytes_per_sample = 1;
58
59	if (st->bpse == 11)
60		bytes_per_sample = 2;
61
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.
68	 */
69	ret = sca3000_read_data(st,
70				SCA3000_REG_ADDR_BUF_COUNT,
71				&rx, 1);
72	if (ret)
73		goto error_ret;
74	else
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.
78	 */
79	kfree(rx);
80	if (count > num_available * bytes_per_sample)
81		num_read = num_available*bytes_per_sample;
82	else
83		num_read = count - (count % (bytes_per_sample));
84
85	/* Avoid the read request byte */
86	*dead_offset = 1;
87	ret = sca3000_read_data(st,
88				SCA3000_REG_ADDR_RING_OUT,
89				data, num_read);
90error_ret:
91	mutex_unlock(&st->lock);
92
93	return ret ? ret : num_read;
94}
95
96/* This is only valid with all 3 elements enabled */
97static int sca3000_ring_get_length(struct iio_ring_buffer *r)
98{
99	return 64;
100}
101
102/* only valid if resolution is kept at 11bits */
103static int sca3000_ring_get_bpd(struct iio_ring_buffer *r)
104{
105	return 6;
106}
107static void sca3000_ring_release(struct device *dev)
108{
109	struct iio_ring_buffer *r = to_iio_ring_buffer(dev);
110	kfree(iio_to_hw_ring_buf(r));
111}
112
113static IIO_RING_ENABLE_ATTR;
114static IIO_RING_BPS_ATTR;
115static IIO_RING_LENGTH_ATTR;
116
117/**
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
122 **/
123static ssize_t sca3000_show_ring_bpse(struct device *dev,
124				      struct device_attribute *attr,
125				      char *buf)
126{
127	int len = 0, ret;
128	u8 *rx;
129	struct iio_ring_buffer *r = dev_get_drvdata(dev);
130	struct sca3000_state *st = r->indio_dev->dev_data;
131
132	mutex_lock(&st->lock);
133	ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
134	if (ret)
135		goto error_ret;
136	len = sprintf(buf, "%d\n", (rx[1] & SCA3000_RING_BUF_8BIT) ? 8 : 11);
137	kfree(rx);
138error_ret:
139	mutex_unlock(&st->lock);
140
141	return ret ? ret : len;
142}
143
144/**
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
150 **/
151static ssize_t sca3000_store_ring_bpse(struct device *dev,
152				      struct device_attribute *attr,
153				      const char *buf,
154				      size_t len)
155{
156	struct iio_ring_buffer *r = dev_get_drvdata(dev);
157	struct sca3000_state *st = r->indio_dev->dev_data;
158	int ret;
159	u8 *rx;
160	long val;
161	ret = strict_strtol(buf, 10, &val);
162	if (ret)
163		return ret;
164
165	mutex_lock(&st->lock);
166
167	ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
168	if (!ret)
169		switch (val) {
170		case 8:
171			ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
172						rx[1] | SCA3000_RING_BUF_8BIT);
173			st->bpse = 8;
174			break;
175		case 11:
176			ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
177						rx[1] & ~SCA3000_RING_BUF_8BIT);
178			st->bpse = 11;
179			break;
180		default:
181			ret = -EINVAL;
182			break;
183		}
184	mutex_unlock(&st->lock);
185
186	return ret ? ret : len;
187}
188
189static IIO_SCAN_EL_C(accel_x, 0, 0, 0, NULL);
190static IIO_SCAN_EL_C(accel_y, 1, 0, 0, NULL);
191static IIO_SCAN_EL_C(accel_z, 2, 0, 0, NULL);
192static IIO_CONST_ATTR(accel_precision_available, "8 11");
193static IIO_DEVICE_ATTR(accel_precision,
194		       S_IRUGO | S_IWUSR,
195		       sca3000_show_ring_bpse,
196		       sca3000_store_ring_bpse,
197		       0);
198
199static struct attribute *sca3000_scan_el_attrs[] = {
200	&iio_scan_el_accel_x.dev_attr.attr,
201	&iio_scan_el_accel_y.dev_attr.attr,
202	&iio_scan_el_accel_z.dev_attr.attr,
203	&iio_const_attr_accel_precision_available.dev_attr.attr,
204	&iio_dev_attr_accel_precision.dev_attr.attr,
205	NULL
206};
207
208static struct attribute_group sca3000_scan_el_group = {
209	.attrs = sca3000_scan_el_attrs,
210	.name = "scan_elements",
211};
212
213/*
214 * Ring buffer attributes
215 * This device is a bit unusual in that the sampling frequency and bpse
216 * only apply to the ring buffer.  At all times full rate and accuracy
217 * is available via direct reading from registers.
218 */
219static struct attribute *sca3000_ring_attributes[] = {
220	&dev_attr_length.attr,
221	&dev_attr_bps.attr,
222	&dev_attr_ring_enable.attr,
223	NULL,
224};
225
226static struct attribute_group sca3000_ring_attr = {
227	.attrs = sca3000_ring_attributes,
228};
229
230static const struct attribute_group *sca3000_ring_attr_groups[] = {
231	&sca3000_ring_attr,
232	NULL
233};
234
235static struct device_type sca3000_ring_type = {
236	.release = sca3000_ring_release,
237	.groups = sca3000_ring_attr_groups,
238};
239
240static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev)
241{
242	struct iio_ring_buffer *buf;
243	struct iio_hw_ring_buffer *ring;
244
245	ring = kzalloc(sizeof *ring, GFP_KERNEL);
246	if (!ring)
247		return NULL;
248	ring->private = indio_dev;
249	buf = &ring->buf;
250	iio_ring_buffer_init(buf, indio_dev);
251	buf->dev.type = &sca3000_ring_type;
252	device_initialize(&buf->dev);
253	buf->dev.parent = &indio_dev->dev;
254	dev_set_drvdata(&buf->dev, (void *)buf);
255
256	return buf;
257}
258
259static inline void sca3000_rb_free(struct iio_ring_buffer *r)
260{
261	if (r)
262		iio_put_ring_buffer(r);
263}
264
265int sca3000_configure_ring(struct iio_dev *indio_dev)
266{
267	indio_dev->scan_el_attrs = &sca3000_scan_el_group;
268	indio_dev->ring = sca3000_rb_allocate(indio_dev);
269	if (indio_dev->ring == NULL)
270		return -ENOMEM;
271	indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER;
272
273	indio_dev->ring->access.rip_lots = &sca3000_rip_hw_rb;
274	indio_dev->ring->access.get_length = &sca3000_ring_get_length;
275	indio_dev->ring->access.get_bpd = &sca3000_ring_get_bpd;
276
277	return 0;
278}
279
280void sca3000_unconfigure_ring(struct iio_dev *indio_dev)
281{
282	sca3000_rb_free(indio_dev->ring);
283}
284
285static inline
286int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
287{
288	struct sca3000_state *st = indio_dev->dev_data;
289	int ret;
290	u8 *rx;
291
292	mutex_lock(&st->lock);
293	ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
294	if (ret)
295		goto error_ret;
296	if (state) {
297		printk(KERN_INFO "supposedly enabling ring buffer\n");
298		ret = sca3000_write_reg(st,
299					SCA3000_REG_ADDR_MODE,
300					(rx[1] | SCA3000_RING_BUF_ENABLE));
301	} else
302		ret = sca3000_write_reg(st,
303					SCA3000_REG_ADDR_MODE,
304					(rx[1] & ~SCA3000_RING_BUF_ENABLE));
305	kfree(rx);
306error_ret:
307	mutex_unlock(&st->lock);
308
309	return ret;
310}
311/**
312 * sca3000_hw_ring_preenable() hw ring buffer preenable function
313 *
314 * Very simple enable function as the chip will allows normal reads
315 * during ring buffer operation so as long as it is indeed running
316 * before we notify the core, the precise ordering does not matter.
317 **/
318static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
319{
320	return __sca3000_hw_ring_state_set(indio_dev, 1);
321}
322
323static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
324{
325	return __sca3000_hw_ring_state_set(indio_dev, 0);
326}
327
328void sca3000_register_ring_funcs(struct iio_dev *indio_dev)
329{
330	indio_dev->ring->preenable = &sca3000_hw_ring_preenable;
331	indio_dev->ring->postdisable = &sca3000_hw_ring_postdisable;
332}
333
334/**
335 * sca3000_ring_int_process() ring specific interrupt handling.
336 *
337 * This is only split from the main interrupt handler so as to
338 * reduce the amount of code if the ring buffer is not enabled.
339 **/
340void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring)
341{
342	if (val & SCA3000_INT_STATUS_THREE_QUARTERS)
343		iio_push_or_escallate_ring_event(ring,
344						 IIO_EVENT_CODE_RING_75_FULL,
345						 0);
346	else if (val & SCA3000_INT_STATUS_HALF)
347		iio_push_ring_event(ring,
348				    IIO_EVENT_CODE_RING_50_FULL, 0);
349}
350