]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/iio/iio_simple_dummy_buffer.c
Merge tag 'stable/for-linus-3.14-rc2-tag' of git://git.kernel.org/pub/scm/linux/kerne...
[mirror_ubuntu-artful-kernel.git] / drivers / staging / iio / iio_simple_dummy_buffer.c
CommitLineData
9ad2e2e1
JC
1/**
2 * Copyright (c) 2011 Jonathan Cameron
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 * Buffer handling elements of industrial I/O reference driver.
9 * Uses the kfifo buffer.
10 *
11 * To test without hardware use the sysfs trigger.
12 */
13
14#include <linux/kernel.h>
8e336a72 15#include <linux/export.h>
9ad2e2e1
JC
16#include <linux/slab.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/bitmap.h>
20
06458e27
JC
21#include <linux/iio/iio.h>
22#include <linux/iio/trigger_consumer.h>
23#include <linux/iio/kfifo_buf.h>
9ad2e2e1
JC
24
25#include "iio_simple_dummy.h"
26
27/* Some fake data */
28
29static const s16 fakedata[] = {
30 [voltage0] = 7,
31 [diffvoltage1m2] = -33,
32 [diffvoltage3m4] = -2,
33 [accelx] = 344,
34};
35/**
36 * iio_simple_dummy_trigger_h() - the trigger handler function
37 * @irq: the interrupt number
38 * @p: private data - always a pointer to the poll func.
39 *
99de0c2b 40 * This is the guts of buffered capture. On a trigger event occurring,
9ad2e2e1
JC
41 * if the pollfunc is attached then this handler is called as a threaded
42 * interrupt (and hence may sleep). It is responsible for grabbing data
43 * from the device and pushing it into the associated buffer.
44 */
45static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
46{
47 struct iio_poll_func *pf = p;
48 struct iio_dev *indio_dev = pf->indio_dev;
9ad2e2e1 49 int len = 0;
420fe2e9
JC
50 u16 *data;
51
52 data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
9ad2e2e1 53 if (data == NULL)
a1bdeefd 54 goto done;
9ad2e2e1 55
550268ca 56 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) {
9ad2e2e1
JC
57 /*
58 * Three common options here:
59 * hardware scans: certain combinations of channels make
60 * up a fast read. The capture will consist of all of them.
61 * Hence we just call the grab data function and fill the
62 * buffer without processing.
99de0c2b 63 * software scans: can be considered to be random access
9ad2e2e1
JC
64 * so efficient reading is just a case of minimal bus
65 * transactions.
66 * software culled hardware scans:
67 * occasionally a driver may process the nearest hardware
68 * scan to avoid storing elements that are not desired. This
25c38aa3
PM
69 * is the fiddliest option by far.
70 * Here let's pretend we have random access. And the values are
9ad2e2e1
JC
71 * in the constant table fakedata.
72 */
73 int i, j;
550268ca
JC
74 for (i = 0, j = 0;
75 i < bitmap_weight(indio_dev->active_scan_mask,
76 indio_dev->masklength);
708706ff 77 i++, j++) {
84b36ce5 78 j = find_next_bit(indio_dev->active_scan_mask,
708706ff 79 indio_dev->masklength, j);
d163a19d 80 /* random access read from the 'device' */
9ad2e2e1
JC
81 data[i] = fakedata[j];
82 len += 2;
83 }
84 }
09a799d0
LPC
85
86 iio_push_to_buffers_with_timestamp(indio_dev, data, iio_get_time_ns());
9ad2e2e1
JC
87
88 kfree(data);
89
a1bdeefd 90done:
9ad2e2e1
JC
91 /*
92 * Tell the core we are done with this trigger and ready for the
93 * next one.
94 */
95 iio_trigger_notify_done(indio_dev->trig);
96
97 return IRQ_HANDLED;
98}
99
100static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
9ad2e2e1
JC
101 /*
102 * iio_triggered_buffer_postenable:
103 * Generic function that simply attaches the pollfunc to the trigger.
104 * Replace this to mess with hardware state before we attach the
105 * trigger.
106 */
107 .postenable = &iio_triggered_buffer_postenable,
108 /*
109 * iio_triggered_buffer_predisable:
110 * Generic function that simple detaches the pollfunc from the trigger.
111 * Replace this to put hardware state back again after the trigger is
112 * detached but before userspace knows we have disabled the ring.
113 */
114 .predisable = &iio_triggered_buffer_predisable,
115};
116
3fff2274
LPC
117int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev,
118 const struct iio_chan_spec *channels, unsigned int num_channels)
9ad2e2e1
JC
119{
120 int ret;
121 struct iio_buffer *buffer;
122
123 /* Allocate a buffer to use - here a kfifo */
124 buffer = iio_kfifo_allocate(indio_dev);
125 if (buffer == NULL) {
126 ret = -ENOMEM;
127 goto error_ret;
128 }
129
9e69c935 130 iio_device_attach_buffer(indio_dev, buffer);
9ad2e2e1 131
9ad2e2e1
JC
132 /* Enable timestamps by default */
133 buffer->scan_timestamp = true;
134
135 /*
136 * Tell the core what device type specific functions should
137 * be run on either side of buffer capture enable / disable.
138 */
1612244f 139 indio_dev->setup_ops = &iio_simple_dummy_buffer_setup_ops;
9ad2e2e1
JC
140
141 /*
142 * Configure a polling function.
143 * When a trigger event with this polling function connected
144 * occurs, this function is run. Typically this grabs data
145 * from the device.
146 *
569c4ac2 147 * NULL for the bottom half. This is normally implemented only if we
9ad2e2e1
JC
148 * either want to ping a capture now pin (no sleeping) or grab
149 * a timestamp as close as possible to a data ready trigger firing.
150 *
151 * IRQF_ONESHOT ensures irqs are masked such that only one instance
152 * of the handler can run at a time.
153 *
154 * "iio_simple_dummy_consumer%d" formatting string for the irq 'name'
155 * as seen under /proc/interrupts. Remaining parameters as per printk.
156 */
157 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
158 &iio_simple_dummy_trigger_h,
159 IRQF_ONESHOT,
160 indio_dev,
161 "iio_simple_dummy_consumer%d",
162 indio_dev->id);
163
164 if (indio_dev->pollfunc == NULL) {
165 ret = -ENOMEM;
166 goto error_free_buffer;
167 }
168
169 /*
170 * Notify the core that this device is capable of buffered capture
171 * driven by a trigger.
172 */
173 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
3fff2274
LPC
174
175 ret = iio_buffer_register(indio_dev, channels, num_channels);
176 if (ret)
177 goto error_dealloc_pollfunc;
178
9ad2e2e1
JC
179 return 0;
180
3fff2274
LPC
181error_dealloc_pollfunc:
182 iio_dealloc_pollfunc(indio_dev->pollfunc);
9ad2e2e1
JC
183error_free_buffer:
184 iio_kfifo_free(indio_dev->buffer);
185error_ret:
186 return ret;
187
188}
189
190/**
191 * iio_simple_dummy_unconfigure_buffer() - release buffer resources
192 * @indo_dev: device instance state
193 */
194void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
195{
3fff2274 196 iio_buffer_unregister(indio_dev);
9ad2e2e1
JC
197 iio_dealloc_pollfunc(indio_dev->pollfunc);
198 iio_kfifo_free(indio_dev->buffer);
199}