blob: 59e3186c3beec95096990147ac7bbdbe9842fcfa [file] [log] [blame]
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +01001/**
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>
Paul Gortmaker8e336a72011-07-10 13:09:12 -040015#include <linux/export.h>
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +010016#include <linux/slab.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/bitmap.h>
20
21#include "iio.h"
22#include "trigger_consumer.h"
23#include "kfifo_buf.h"
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 *
40 * This is the guts of buffered capture. On a trigger event occuring,
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;
49 struct iio_buffer *buffer = indio_dev->buffer;
50 int len = 0;
51 /*
52 * The datasize is obtained from the buffer. It was stored when
53 * the preenable setup function was called.
54 */
55 size_t datasize = buffer->access->get_bytes_per_datum(buffer);
56 u16 *data = kmalloc(datasize, GFP_KERNEL);
57 if (data == NULL)
58 return -ENOMEM;
59
Jonathan Cameron550268ca2011-12-05 22:18:15 +000060 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) {
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +010061 /*
62 * Three common options here:
63 * hardware scans: certain combinations of channels make
64 * up a fast read. The capture will consist of all of them.
65 * Hence we just call the grab data function and fill the
66 * buffer without processing.
67 * sofware scans: can be considered to be random access
68 * so efficient reading is just a case of minimal bus
69 * transactions.
70 * software culled hardware scans:
71 * occasionally a driver may process the nearest hardware
72 * scan to avoid storing elements that are not desired. This
73 * is the fidliest option by far.
74 * Here lets pretend we have random access. And the values are
75 * in the constant table fakedata.
76 */
77 int i, j;
Jonathan Cameron550268ca2011-12-05 22:18:15 +000078 for (i = 0, j = 0;
79 i < bitmap_weight(indio_dev->active_scan_mask,
80 indio_dev->masklength);
81 i++) {
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +010082 j = find_next_bit(buffer->scan_mask,
83 indio_dev->masklength, j + 1);
84 /* random access read form the 'device' */
85 data[i] = fakedata[j];
86 len += 2;
87 }
88 }
89 /* Store a timestampe at an 8 byte boundary */
90 if (buffer->scan_timestamp)
91 *(s64 *)(((phys_addr_t)data + len
92 + sizeof(s64) - 1) & ~(sizeof(s64) - 1))
93 = iio_get_time_ns();
94 buffer->access->store_to(buffer, (u8 *)data, pf->timestamp);
95
96 kfree(data);
97
98 /*
99 * Tell the core we are done with this trigger and ready for the
100 * next one.
101 */
102 iio_trigger_notify_done(indio_dev->trig);
103
104 return IRQ_HANDLED;
105}
106
107static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
108 /*
109 * iio_sw_buffer_preenable:
110 * Generic function for equal sized ring elements + 64 bit timestamp
111 * Assumes that any combination of channels can be enabled.
112 * Typically replaced to implement restrictions on what combinations
113 * can be captured (hardware scan modes).
114 */
115 .preenable = &iio_sw_buffer_preenable,
116 /*
117 * iio_triggered_buffer_postenable:
118 * Generic function that simply attaches the pollfunc to the trigger.
119 * Replace this to mess with hardware state before we attach the
120 * trigger.
121 */
122 .postenable = &iio_triggered_buffer_postenable,
123 /*
124 * iio_triggered_buffer_predisable:
125 * Generic function that simple detaches the pollfunc from the trigger.
126 * Replace this to put hardware state back again after the trigger is
127 * detached but before userspace knows we have disabled the ring.
128 */
129 .predisable = &iio_triggered_buffer_predisable,
130};
131
132int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
133{
134 int ret;
135 struct iio_buffer *buffer;
136
137 /* Allocate a buffer to use - here a kfifo */
138 buffer = iio_kfifo_allocate(indio_dev);
139 if (buffer == NULL) {
140 ret = -ENOMEM;
141 goto error_ret;
142 }
143
144 indio_dev->buffer = buffer;
145 /* Tell the core how to access the buffer */
146 buffer->access = &kfifo_access_funcs;
147
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100148 /* Enable timestamps by default */
149 buffer->scan_timestamp = true;
150
151 /*
152 * Tell the core what device type specific functions should
153 * be run on either side of buffer capture enable / disable.
154 */
Jonathan Cameron16122442011-12-05 22:18:14 +0000155 indio_dev->setup_ops = &iio_simple_dummy_buffer_setup_ops;
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100156 buffer->owner = THIS_MODULE;
157
158 /*
159 * Configure a polling function.
160 * When a trigger event with this polling function connected
161 * occurs, this function is run. Typically this grabs data
162 * from the device.
163 *
164 * NULL for the top half. This is normally implemented only if we
165 * either want to ping a capture now pin (no sleeping) or grab
166 * a timestamp as close as possible to a data ready trigger firing.
167 *
168 * IRQF_ONESHOT ensures irqs are masked such that only one instance
169 * of the handler can run at a time.
170 *
171 * "iio_simple_dummy_consumer%d" formatting string for the irq 'name'
172 * as seen under /proc/interrupts. Remaining parameters as per printk.
173 */
174 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
175 &iio_simple_dummy_trigger_h,
176 IRQF_ONESHOT,
177 indio_dev,
178 "iio_simple_dummy_consumer%d",
179 indio_dev->id);
180
181 if (indio_dev->pollfunc == NULL) {
182 ret = -ENOMEM;
183 goto error_free_buffer;
184 }
185
186 /*
187 * Notify the core that this device is capable of buffered capture
188 * driven by a trigger.
189 */
190 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
191 return 0;
192
193error_free_buffer:
194 iio_kfifo_free(indio_dev->buffer);
195error_ret:
196 return ret;
197
198}
199
200/**
201 * iio_simple_dummy_unconfigure_buffer() - release buffer resources
202 * @indo_dev: device instance state
203 */
204void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
205{
206 iio_dealloc_pollfunc(indio_dev->pollfunc);
207 iio_kfifo_free(indio_dev->buffer);
208}