]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/iio/accel/sca3000_ring.c
0b736dbe818cd2d9d684948e2e3d8d8a59ccf6ad
[mirror_ubuntu-bionic-kernel.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/fs.h>
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/sysfs.h>
18 #include <linux/sched.h>
19 #include <linux/poll.h>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "../ring_generic.h"
24 #include "../ring_hw.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 static int sca3000_read_data(struct sca3000_state *st,
37 uint8_t reg_address_high,
38 u8 **rx_p,
39 int len)
40 {
41 int ret;
42 struct spi_message msg;
43 struct spi_transfer xfer[2] = {
44 {
45 .len = 1,
46 .tx_buf = st->tx,
47 }, {
48 .len = len,
49 }
50 };
51 *rx_p = kmalloc(len, GFP_KERNEL);
52 if (*rx_p == NULL) {
53 ret = -ENOMEM;
54 goto error_ret;
55 }
56 xfer[1].rx_buf = *rx_p;
57 st->tx[0] = SCA3000_READ_REG(reg_address_high);
58 spi_message_init(&msg);
59 spi_message_add_tail(&xfer[0], &msg);
60 spi_message_add_tail(&xfer[1], &msg);
61 ret = spi_sync(st->us, &msg);
62 if (ret) {
63 dev_err(get_device(&st->us->dev), "problem reading register");
64 goto error_free_rx;
65 }
66
67 return 0;
68 error_free_rx:
69 kfree(*rx_p);
70 error_ret:
71 return ret;
72 }
73
74 /**
75 * sca3000_read_first_n_hw_rb() - main ring access, pulls data from ring
76 * @r: the ring
77 * @count: number of samples to try and pull
78 * @data: output the actual samples pulled from the hw ring
79 *
80 * Currently does not provide timestamps. As the hardware doesn't add them they
81 * can only be inferred approximately from ring buffer events such as 50% full
82 * and knowledge of when buffer was last emptied. This is left to userspace.
83 **/
84 static int sca3000_read_first_n_hw_rb(struct iio_ring_buffer *r,
85 size_t count, char __user *buf)
86 {
87 struct iio_hw_ring_buffer *hw_ring = iio_to_hw_ring_buf(r);
88 struct iio_dev *indio_dev = hw_ring->private;
89 struct sca3000_state *st = iio_priv(indio_dev);
90 u8 *rx;
91 int ret, i, num_available, num_read = 0;
92 int bytes_per_sample = 1;
93
94 if (st->bpse == 11)
95 bytes_per_sample = 2;
96
97 mutex_lock(&st->lock);
98 if (count % bytes_per_sample) {
99 ret = -EINVAL;
100 goto error_ret;
101 }
102
103 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_BUF_COUNT, 1);
104 if (ret)
105 goto error_ret;
106 else
107 num_available = st->rx[0];
108 /*
109 * num_available is the total number of samples available
110 * i.e. number of time points * number of channels.
111 */
112 if (count > num_available * bytes_per_sample)
113 num_read = num_available*bytes_per_sample;
114 else
115 num_read = count;
116
117 ret = sca3000_read_data(st,
118 SCA3000_REG_ADDR_RING_OUT,
119 &rx, num_read);
120 if (ret)
121 goto error_ret;
122
123 for (i = 0; i < num_read; i++)
124 *(((u16 *)rx) + i) = be16_to_cpup((u16 *)rx + i);
125
126 if (copy_to_user(buf, rx, num_read))
127 ret = -EFAULT;
128 kfree(rx);
129 r->stufftoread = 0;
130 error_ret:
131 mutex_unlock(&st->lock);
132
133 return ret ? ret : num_read;
134 }
135
136 /* This is only valid with all 3 elements enabled */
137 static int sca3000_ring_get_length(struct iio_ring_buffer *r)
138 {
139 return 64;
140 }
141
142 /* only valid if resolution is kept at 11bits */
143 static int sca3000_ring_get_bytes_per_datum(struct iio_ring_buffer *r)
144 {
145 return 6;
146 }
147 static void sca3000_ring_release(struct device *dev)
148 {
149 struct iio_ring_buffer *r = to_iio_ring_buffer(dev);
150 kfree(iio_to_hw_ring_buf(r));
151 }
152
153 static IIO_RING_ENABLE_ATTR;
154 static IIO_RING_BYTES_PER_DATUM_ATTR;
155 static IIO_RING_LENGTH_ATTR;
156
157 /**
158 * sca3000_query_ring_int() is the hardware ring status interrupt enabled
159 **/
160 static ssize_t sca3000_query_ring_int(struct device *dev,
161 struct device_attribute *attr,
162 char *buf)
163 {
164 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
165 int ret, val;
166 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
167 struct iio_dev *indio_dev = ring->indio_dev;
168 struct sca3000_state *st = iio_priv(indio_dev);
169
170 mutex_lock(&st->lock);
171 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
172 val = st->rx[0];
173 mutex_unlock(&st->lock);
174 if (ret)
175 return ret;
176
177 return sprintf(buf, "%d\n", !!(val & this_attr->address));
178 }
179
180 /**
181 * sca3000_set_ring_int() set state of ring status interrupt
182 **/
183 static ssize_t sca3000_set_ring_int(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf,
186 size_t len)
187 {
188 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
189 struct iio_dev *indio_dev = ring->indio_dev;
190 struct sca3000_state *st = iio_priv(indio_dev);
191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
192 long val;
193 int ret;
194
195 mutex_lock(&st->lock);
196 ret = strict_strtol(buf, 10, &val);
197 if (ret)
198 goto error_ret;
199 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_INT_MASK, 1);
200 if (ret)
201 goto error_ret;
202 if (val)
203 ret = sca3000_write_reg(st,
204 SCA3000_REG_ADDR_INT_MASK,
205 st->rx[0] | this_attr->address);
206 else
207 ret = sca3000_write_reg(st,
208 SCA3000_REG_ADDR_INT_MASK,
209 st->rx[0] & ~this_attr->address);
210 error_ret:
211 mutex_unlock(&st->lock);
212
213 return ret ? ret : len;
214 }
215
216 static IIO_DEVICE_ATTR(50_percent, S_IRUGO | S_IWUSR,
217 sca3000_query_ring_int,
218 sca3000_set_ring_int,
219 SCA3000_INT_MASK_RING_HALF);
220
221 static IIO_DEVICE_ATTR(75_percent, S_IRUGO | S_IWUSR,
222 sca3000_query_ring_int,
223 sca3000_set_ring_int,
224 SCA3000_INT_MASK_RING_THREE_QUARTER);
225
226 static ssize_t sca3000_show_buffer_scale(struct device *dev,
227 struct device_attribute *attr,
228 char *buf)
229 {
230 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
231 struct iio_dev *indio_dev = ring->indio_dev;
232 struct sca3000_state *st = iio_priv(indio_dev);
233
234 return sprintf(buf, "0.%06d\n", 4*st->info->scale);
235 }
236
237 static IIO_DEVICE_ATTR(accel_scale,
238 S_IRUGO,
239 sca3000_show_buffer_scale,
240 NULL,
241 0);
242
243 /*
244 * Ring buffer attributes
245 * This device is a bit unusual in that the sampling frequency and bpse
246 * only apply to the ring buffer. At all times full rate and accuracy
247 * is available via direct reading from registers.
248 */
249 static struct attribute *sca3000_ring_attributes[] = {
250 &dev_attr_length.attr,
251 &dev_attr_bytes_per_datum.attr,
252 &dev_attr_enable.attr,
253 &iio_dev_attr_50_percent.dev_attr.attr,
254 &iio_dev_attr_75_percent.dev_attr.attr,
255 &iio_dev_attr_accel_scale.dev_attr.attr,
256 NULL,
257 };
258
259 static struct attribute_group sca3000_ring_attr = {
260 .attrs = sca3000_ring_attributes,
261 };
262
263 static const struct attribute_group *sca3000_ring_attr_groups[] = {
264 &sca3000_ring_attr,
265 NULL
266 };
267
268 static struct device_type sca3000_ring_type = {
269 .release = sca3000_ring_release,
270 .groups = sca3000_ring_attr_groups,
271 };
272
273 static struct iio_ring_buffer *sca3000_rb_allocate(struct iio_dev *indio_dev)
274 {
275 struct iio_ring_buffer *buf;
276 struct iio_hw_ring_buffer *ring;
277
278 ring = kzalloc(sizeof *ring, GFP_KERNEL);
279 if (!ring)
280 return NULL;
281
282 ring->private = indio_dev;
283 buf = &ring->buf;
284 buf->stufftoread = 0;
285 iio_ring_buffer_init(buf, indio_dev);
286 buf->dev.type = &sca3000_ring_type;
287 buf->dev.parent = &indio_dev->dev;
288 dev_set_drvdata(&buf->dev, (void *)buf);
289
290 return buf;
291 }
292
293 static inline void sca3000_rb_free(struct iio_ring_buffer *r)
294 {
295 if (r)
296 iio_put_ring_buffer(r);
297 }
298
299 static const struct iio_ring_access_funcs sca3000_ring_access_funcs = {
300 .read_first_n = &sca3000_read_first_n_hw_rb,
301 .get_length = &sca3000_ring_get_length,
302 .get_bytes_per_datum = &sca3000_ring_get_bytes_per_datum,
303 };
304
305 int sca3000_configure_ring(struct iio_dev *indio_dev)
306 {
307 indio_dev->ring = sca3000_rb_allocate(indio_dev);
308 if (indio_dev->ring == NULL)
309 return -ENOMEM;
310 indio_dev->modes |= INDIO_RING_HARDWARE_BUFFER;
311
312 indio_dev->ring->access = &sca3000_ring_access_funcs;
313
314 iio_scan_mask_set(indio_dev->ring, 0);
315 iio_scan_mask_set(indio_dev->ring, 1);
316 iio_scan_mask_set(indio_dev->ring, 2);
317
318 return 0;
319 }
320
321 void sca3000_unconfigure_ring(struct iio_dev *indio_dev)
322 {
323 sca3000_rb_free(indio_dev->ring);
324 }
325
326 static inline
327 int __sca3000_hw_ring_state_set(struct iio_dev *indio_dev, bool state)
328 {
329 struct sca3000_state *st = iio_priv(indio_dev);
330 int ret;
331
332 mutex_lock(&st->lock);
333 ret = sca3000_read_data_short(st, SCA3000_REG_ADDR_MODE, 1);
334 if (ret)
335 goto error_ret;
336 if (state) {
337 printk(KERN_INFO "supposedly enabling ring buffer\n");
338 ret = sca3000_write_reg(st,
339 SCA3000_REG_ADDR_MODE,
340 (st->rx[0] | SCA3000_RING_BUF_ENABLE));
341 } else
342 ret = sca3000_write_reg(st,
343 SCA3000_REG_ADDR_MODE,
344 (st->rx[0] & ~SCA3000_RING_BUF_ENABLE));
345 error_ret:
346 mutex_unlock(&st->lock);
347
348 return ret;
349 }
350 /**
351 * sca3000_hw_ring_preenable() hw ring buffer preenable function
352 *
353 * Very simple enable function as the chip will allows normal reads
354 * during ring buffer operation so as long as it is indeed running
355 * before we notify the core, the precise ordering does not matter.
356 **/
357 static int sca3000_hw_ring_preenable(struct iio_dev *indio_dev)
358 {
359 return __sca3000_hw_ring_state_set(indio_dev, 1);
360 }
361
362 static int sca3000_hw_ring_postdisable(struct iio_dev *indio_dev)
363 {
364 return __sca3000_hw_ring_state_set(indio_dev, 0);
365 }
366
367 static const struct iio_ring_setup_ops sca3000_ring_setup_ops = {
368 .preenable = &sca3000_hw_ring_preenable,
369 .postdisable = &sca3000_hw_ring_postdisable,
370 };
371
372 void sca3000_register_ring_funcs(struct iio_dev *indio_dev)
373 {
374 indio_dev->ring->setup_ops = &sca3000_ring_setup_ops;
375 }
376
377 /**
378 * sca3000_ring_int_process() ring specific interrupt handling.
379 *
380 * This is only split from the main interrupt handler so as to
381 * reduce the amount of code if the ring buffer is not enabled.
382 **/
383 void sca3000_ring_int_process(u8 val, struct iio_ring_buffer *ring)
384 {
385 if (val & (SCA3000_INT_STATUS_THREE_QUARTERS |
386 SCA3000_INT_STATUS_HALF)) {
387 ring->stufftoread = true;
388 wake_up_interruptible(&ring->pollq);
389 }
390 }