Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/percpu
[sfrench/cifs-2.6.git] / drivers / staging / iio / accel / sca3000_ring.c
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/kernel.h>
17 #include <linux/spi/spi.h>
18 #include <linux/sysfs.h>
19
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "../ring_generic.h"
23 #include "../ring_hw.h"
24 #include "accel.h"
25 #include "sca3000.h"
26
27 /* RFC / future work
28  *
29  * The internal ring buffer doesn't actually change what it holds depending
30  * on which signals are enabled etc, merely whether you can read them.
31  * As such the scan mode selection is somewhat different than for a software
32  * ring buffer and changing it actually covers any data already in the buffer.
33  * Currently scan elements aren't configured so it doesn't matter.
34  */
35
36 /**
37  * sca3000_rip_hw_rb() - main ring access function, pulls data from ring
38  * @r:                  the ring
39  * @count:              number of samples to try and pull
40  * @data:               output the actual samples pulled from the hw ring
41  * @dead_offset:        cheating a bit here: Set to 1 so as to allow for the
42  *                      leading byte used in bus comms.
43  *
44  * Currently does not provide timestamps.  As the hardware doesn't add them they
45  * can only be inferred aproximately from ring buffer events such as 50% full
46  * and knowledge of when buffer was last emptied.  This is left to userspace.
47  **/
48 static int sca3000_rip_hw_rb(struct iio_ring_buffer *r,
49                              size_t count, u8 **data, int *dead_offset)
50 {
51         struct iio_hw_ring_buffer *hw_ring = iio_to_hw_ring_buf(r);
52         struct iio_dev *indio_dev = hw_ring->private;
53         struct sca3000_state *st = indio_dev->dev_data;
54         u8 *rx;
55         int ret, num_available, num_read = 0;
56         int bytes_per_sample = 1;
57
58         if (st->bpse == 11)
59                 bytes_per_sample = 2;
60
61         mutex_lock(&st->lock);
62         /* Check how much data is available:
63          * RFC: Implement an ioctl to not bother checking whether there
64          * is enough data in the ring?  Afterall, if we are responding
65          * to an interrupt we have a minimum content guaranteed so it
66          * seems slight silly to waste time checking it is there.
67          */
68         ret = sca3000_read_data(st,
69                                 SCA3000_REG_ADDR_BUF_COUNT,
70                                 &rx, 1);
71         if (ret)
72                 goto error_ret;
73         else
74                 num_available = rx[1];
75         /* num_available is the total number of samples available
76          * i.e. number of time points * number of channels.
77          */
78         kfree(rx);
79         if (count > num_available * bytes_per_sample)
80                 num_read = num_available*bytes_per_sample;
81         else
82                 num_read = count - (count % (bytes_per_sample));
83
84         /* Avoid the read request byte */
85         *dead_offset = 1;
86         ret = sca3000_read_data(st,
87                                 SCA3000_REG_ADDR_RING_OUT,
88                                 data, num_read);
89 error_ret:
90         mutex_unlock(&st->lock);
91
92         return ret ? ret : num_read;
93 }
94
95 /* This is only valid with all 3 elements enabled */
96 static int sca3000_ring_get_length(struct iio_ring_buffer *r)
97 {
98         return 64;
99 }
100
101 /* only valid if resolution is kept at 11bits */
102 static int sca3000_ring_get_bpd(struct iio_ring_buffer *r)
103 {
104         return 6;
105 }
106 static void sca3000_ring_release(struct device *dev)
107 {
108         struct iio_ring_buffer *r = to_iio_ring_buffer(dev);
109         kfree(iio_to_hw_ring_buf(r));
110 }
111
112 static IIO_RING_ENABLE_ATTR;
113 static IIO_RING_BPS_ATTR;
114 static IIO_RING_LENGTH_ATTR;
115
116 /**
117  * sca3000_show_ring_bpse() -sysfs function to query bits per sample from ring
118  * @dev: ring buffer device
119  * @attr: this device attribute
120  * @buf: buffer to write to
121  **/
122 static ssize_t sca3000_show_ring_bpse(struct device *dev,
123                                       struct device_attribute *attr,
124                                       char *buf)
125 {
126         int len = 0, ret;
127         u8 *rx;
128         struct iio_ring_buffer *r = dev_get_drvdata(dev);
129         struct sca3000_state *st = r->indio_dev->dev_data;
130
131         mutex_lock(&st->lock);
132         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
133         if (ret)
134                 goto error_ret;
135         len = sprintf(buf, "%d\n", (rx[1] & SCA3000_RING_BUF_8BIT) ? 8 : 11);
136         kfree(rx);
137 error_ret:
138         mutex_unlock(&st->lock);
139
140         return ret ? ret : len;
141 }
142
143 /**
144  * sca3000_store_ring_bpse() - bits per scan element
145  * @dev: ring buffer device
146  * @attr: attribute called from
147  * @buf: input from userspace
148  * @len: length of input
149  **/
150 static ssize_t sca3000_store_ring_bpse(struct device *dev,
151                                       struct device_attribute *attr,
152                                       const char *buf,
153                                       size_t len)
154 {
155         struct iio_ring_buffer *r = dev_get_drvdata(dev);
156         struct sca3000_state *st = r->indio_dev->dev_data;
157         int ret;
158         u8 *rx;
159         long val;
160         ret = strict_strtol(buf, 10, &val);
161         if (ret)
162                 return ret;
163
164         mutex_lock(&st->lock);
165
166         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
167         if (!ret)
168                 switch (val) {
169                 case 8:
170                         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
171                                                 rx[1] | SCA3000_RING_BUF_8BIT);
172                         st->bpse = 8;
173                         break;
174                 case 11:
175                         ret = sca3000_write_reg(st, SCA3000_REG_ADDR_MODE,
176                                                 rx[1] & ~SCA3000_RING_BUF_8BIT);
177                         st->bpse = 11;
178                         break;
179                 default:
180                         ret = -EINVAL;
181                         break;
182                 }
183         mutex_unlock(&st->lock);
184
185         return ret ? ret : len;
186 }
187
188 static IIO_CONST_ATTR(bpse_available, "8 11");
189
190 static IIO_DEV_ATTR_BPSE(S_IRUGO | S_IWUSR,
191                               sca3000_show_ring_bpse,
192                               sca3000_store_ring_bpse);
193
194 /*
195  * Ring buffer attributes
196  * This device is a bit unusual in that the sampling frequency and bpse
197  * only apply to the ring buffer.  At all times full rate and accuracy
198  * is available via direct reading from registers.
199  */
200 static struct attribute *iio_ring_attributes[] = {
201         &dev_attr_length.attr,
202         &dev_attr_bps.attr,
203         &dev_attr_ring_enable.attr,
204         &iio_dev_attr_bpse.dev_attr.attr,
205         &iio_const_attr_bpse_available.dev_attr.attr,
206         NULL,
207 };
208
209 static struct attribute_group sca3000_ring_attr = {
210         .attrs = iio_ring_attributes,
211 };
212
213 static const struct attribute_group *sca3000_ring_attr_groups[] = {
214         &sca3000_ring_attr,
215         NULL
216 };
217
218 static struct device_type sca3000_ring_type = {
219         .release = sca3000_ring_release,
220         .groups = sca3000_ring_attr_groups,
221 };
222
223 static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev)
224 {
225         struct iio_ring_buffer *buf;
226         struct iio_hw_ring_buffer *ring;
227
228         ring = kzalloc(sizeof *ring, GFP_KERNEL);
229         if (!ring)
230                 return 0;
231         ring->private = indio_dev;
232         buf = &ring->buf;
233         iio_ring_buffer_init(buf, indio_dev);
234         buf->dev.type = &sca3000_ring_type;
235         device_initialize(&buf->dev);
236         buf->dev.parent = &indio_dev->dev;
237         dev_set_drvdata(&buf->dev, (void *)buf);
238
239         return buf;
240 }
241
242 static inline void sca3000_rb_free(struct iio_ring_buffer *r)
243 {
244         if (r)
245                 iio_put_ring_buffer(r);
246 }
247
248 int sca3000_configure_ring(struct iio_dev *indio_dev)
249 {
250         indio_dev->ring = sca3000_rb_allocate(indio_dev);
251         if (indio_dev->ring == NULL)
252                 return -ENOMEM;
253         indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER;
254
255         indio_dev->ring->access.rip_lots = &sca3000_rip_hw_rb;
256         indio_dev->ring->access.get_length = &sca3000_ring_get_length;
257         indio_dev->ring->access.get_bpd = &sca3000_ring_get_bpd;
258
259         return 0;
260 }
261
262 void sca3000_unconfigure_ring(struct iio_dev *indio_dev)
263 {
264         sca3000_rb_free(indio_dev->ring);
265 }
266
267 static inline
268 int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
269 {
270         struct sca3000_state *st = indio_dev->dev_data;
271         int ret;
272         u8 *rx;
273
274         mutex_lock(&st->lock);
275         ret = sca3000_read_data(st, SCA3000_REG_ADDR_MODE, &rx, 1);
276         if (ret)
277                 goto error_ret;
278         if (state) {
279                 printk(KERN_INFO "supposedly enabling ring buffer\n");
280                 ret = sca3000_write_reg(st,
281                                         SCA3000_REG_ADDR_MODE,
282                                         (rx[1] | SCA3000_RING_BUF_ENABLE));
283         } else
284                 ret = sca3000_write_reg(st,
285                                         SCA3000_REG_ADDR_MODE,
286                                         (rx[1] & ~SCA3000_RING_BUF_ENABLE));
287         kfree(rx);
288 error_ret:
289         mutex_unlock(&st->lock);
290
291         return ret;
292 }
293 /**
294  * sca3000_hw_ring_preenable() hw ring buffer preenable function
295  *
296  * Very simple enable function as the chip will allows normal reads
297  * during ring buffer operation so as long as it is indeed running
298  * before we notify the core, the precise ordering does not matter.
299  **/
300 static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
301 {
302         return __sca3000_hw_ring_state_set(indio_dev, 1);
303 }
304
305 static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
306 {
307         return __sca3000_hw_ring_state_set(indio_dev, 0);
308 }
309
310 void sca3000_register_ring_funcs(struct iio_dev *indio_dev)
311 {
312         indio_dev->ring->preenable = &sca3000_hw_ring_preenable;
313         indio_dev->ring->postdisable = &sca3000_hw_ring_postdisable;
314 }
315
316 /**
317  * sca3000_ring_int_process() ring specific interrupt handling.
318  *
319  * This is only split from the main interrupt handler so as to
320  * reduce the amount of code if the ring buffer is not enabled.
321  **/
322 void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring)
323 {
324         if (val & SCA3000_INT_STATUS_THREE_QUARTERS)
325                 iio_push_or_escallate_ring_event(ring,
326                                                  IIO_EVENT_CODE_RING_75_FULL,
327                                                  0);
328         else if (val & SCA3000_INT_STATUS_HALF)
329                 iio_push_ring_event(ring,
330                                     IIO_EVENT_CODE_RING_50_FULL, 0);
331 }