staging: iio: Remove unused bit_count from struct iio_scan_el
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / accel / sca3000_ring.c
blob003097d9201cc73df38c003f4b36985eb728e80d
1 /*
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>
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>
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"
28 /* RFC / future work
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.
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.
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 **/
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;
55 u8 *rx;
56 s16 *samples;
57 int ret, i, num_available, num_read = 0;
58 int bytes_per_sample = 1;
60 if (st->bpse == 11)
61 bytes_per_sample = 2;
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,
72 &rx, 1);
73 if (ret)
74 goto error_ret;
75 else
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.
80 kfree(rx);
81 if (count > num_available * bytes_per_sample)
82 num_read = num_available*bytes_per_sample;
83 else
84 num_read = count - (count % (bytes_per_sample));
86 /* Avoid the read request byte */
87 *dead_offset = 1;
88 ret = sca3000_read_data(st,
89 SCA3000_REG_ADDR_RING_OUT,
90 data, num_read);
92 /* Convert byte order and shift to default resolution */
93 if (st->bpse == 11) {
94 samples = (s16*)(*data+1);
95 for (i = 0; i < (num_read/2); i++) {
96 samples[i] = be16_to_cpup(
97 (__be16 *)&(samples[i]));
98 samples[i] >>= 3;
102 error_ret:
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)
111 return 64;
114 /* only valid if resolution is kept at 11bits */
115 static int sca3000_ring_get_bytes_per_datum(struct iio_ring_buffer *r)
117 return 6;
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,
137 char *buf)
139 int len = 0, ret;
140 u8 *rx;
141 struct iio_dev *indio_dev = dev_get_drvdata(dev);
142 struct sca3000_state *st = indio_dev->dev_data;
144 mutex_lock(&st->lock);
145 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
146 if (ret)
147 goto error_ret;
148 if (rx[1] & SCA3000_RING_BUF_8BIT)
149 len = sprintf(buf, "s8/8\n");
150 else
151 len = sprintf(buf, "s11/16\n");
152 kfree(rx);
153 error_ret:
154 mutex_unlock(&st->lock);
156 return ret ? ret : len;
160 * sca3000_store_ring_bpse() - bits per scan element
161 * @dev: ring buffer device
162 * @attr: attribute called from
163 * @buf: input from userspace
164 * @len: length of input
166 static ssize_t sca3000_store_ring_bpse(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf,
169 size_t len)
171 struct iio_dev *indio_dev = dev_get_drvdata(dev);
172 struct sca3000_state *st = indio_dev->dev_data;
173 int ret;
174 u8 *rx;
176 mutex_lock(&st->lock);
178 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
179 if (ret)
180 goto error_ret;
181 if (strncmp(buf, "s8/8", 4) == 0) {
182 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
183 rx[1] | SCA3000_RING_BUF_8BIT);
184 st->bpse = 8;
185 } else if (strncmp(buf, "s11/16", 5) == 0) {
186 ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
187 rx[1] & ~SCA3000_RING_BUF_8BIT);
188 st->bpse = 11;
189 } else
190 ret = -EINVAL;
191 error_ret:
192 mutex_unlock(&st->lock);
194 return ret ? ret : len;
197 static IIO_SCAN_EL_C(accel_x, 0, 0, NULL);
198 static IIO_SCAN_EL_C(accel_y, 1, 0, NULL);
199 static IIO_SCAN_EL_C(accel_z, 2, 0, NULL);
200 static IIO_CONST_ATTR(accel_type_available, "s8/8 s11/16");
201 static IIO_DEVICE_ATTR(accel_type,
202 S_IRUGO | S_IWUSR,
203 sca3000_show_ring_bpse,
204 sca3000_store_ring_bpse,
207 static struct attribute *sca3000_scan_el_attrs[] = {
208 &iio_scan_el_accel_x.dev_attr.attr,
209 &iio_scan_el_accel_y.dev_attr.attr,
210 &iio_scan_el_accel_z.dev_attr.attr,
211 &iio_const_attr_accel_type_available.dev_attr.attr,
212 &iio_dev_attr_accel_type.dev_attr.attr,
213 NULL
216 static struct attribute_group sca3000_scan_el_group = {
217 .attrs = sca3000_scan_el_attrs,
218 .name = "scan_elements",
222 * Ring buffer attributes
223 * This device is a bit unusual in that the sampling frequency and bpse
224 * only apply to the ring buffer. At all times full rate and accuracy
225 * is available via direct reading from registers.
227 static struct attribute *sca3000_ring_attributes[] = {
228 &dev_attr_length.attr,
229 &dev_attr_bytes_per_datum.attr,
230 &dev_attr_enable.attr,
231 NULL,
234 static struct attribute_group sca3000_ring_attr = {
235 .attrs = sca3000_ring_attributes,
238 static const struct attribute_group *sca3000_ring_attr_groups[] = {
239 &sca3000_ring_attr,
240 NULL
243 static struct device_type sca3000_ring_type = {
244 .release = sca3000_ring_release,
245 .groups = sca3000_ring_attr_groups,
248 static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev)
250 struct iio_ring_buffer *buf;
251 struct iio_hw_ring_buffer *ring;
253 ring = kzalloc(sizeof *ring, GFP_KERNEL);
254 if (!ring)
255 return NULL;
256 ring->private = indio_dev;
257 buf = &ring->buf;
258 iio_ring_buffer_init(buf, indio_dev);
259 buf->dev.type = &sca3000_ring_type;
260 device_initialize(&buf->dev);
261 buf->dev.parent = &indio_dev->dev;
262 dev_set_drvdata(&buf->dev, (void *)buf);
264 return buf;
267 static inline void sca3000_rb_free(struct iio_ring_buffer *r)
269 if (r)
270 iio_put_ring_buffer(r);
273 int sca3000_configure_ring(struct iio_dev *indio_dev)
275 indio_dev->ring = sca3000_rb_allocate(indio_dev);
276 if (indio_dev->ring == NULL)
277 return -ENOMEM;
278 indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER;
280 indio_dev->ring->scan_el_attrs = &sca3000_scan_el_group;
281 indio_dev->ring->access.rip_lots = &sca3000_rip_hw_rb;
282 indio_dev->ring->access.get_length = &sca3000_ring_get_length;
283 indio_dev->ring->access.get_bytes_per_datum = &sca3000_ring_get_bytes_per_datum;
285 return 0;
288 void sca3000_unconfigure_ring(struct iio_dev *indio_dev)
290 sca3000_rb_free(indio_dev->ring);
293 static inline
294 int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
296 struct sca3000_state *st = indio_dev->dev_data;
297 int ret;
298 u8 *rx;
300 mutex_lock(&st->lock);
301 ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
302 if (ret)
303 goto error_ret;
304 if (state) {
305 printk(KERN_INFO "supposedly enabling ring buffer\n");
306 ret = sca3000_write_reg(st,
307 SCA3000_REG_ADDR_MODE,
308 (rx[1] | SCA3000_RING_BUF_ENABLE));
309 } else
310 ret = sca3000_write_reg(st,
311 SCA3000_REG_ADDR_MODE,
312 (rx[1] & ~SCA3000_RING_BUF_ENABLE));
313 kfree(rx);
314 error_ret:
315 mutex_unlock(&st->lock);
317 return ret;
320 * sca3000_hw_ring_preenable() hw ring buffer preenable function
322 * Very simple enable function as the chip will allows normal reads
323 * during ring buffer operation so as long as it is indeed running
324 * before we notify the core, the precise ordering does not matter.
326 static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
328 return __sca3000_hw_ring_state_set(indio_dev, 1);
331 static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
333 return __sca3000_hw_ring_state_set(indio_dev, 0);
336 void sca3000_register_ring_funcs(struct iio_dev *indio_dev)
338 indio_dev->ring->preenable = &sca3000_hw_ring_preenable;
339 indio_dev->ring->postdisable = &sca3000_hw_ring_postdisable;
343 * sca3000_ring_int_process() ring specific interrupt handling.
345 * This is only split from the main interrupt handler so as to
346 * reduce the amount of code if the ring buffer is not enabled.
348 void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring)
350 if (val & SCA3000_INT_STATUS_THREE_QUARTERS)
351 iio_push_or_escallate_ring_event(ring,
352 IIO_EVENT_CODE_RING_75_FULL,
354 else if (val & SCA3000_INT_STATUS_HALF)
355 iio_push_ring_event(ring,
356 IIO_EVENT_CODE_RING_50_FULL, 0);