1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2011 Jonathan Cameron
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Buffer handling elements of industrial I/O reference driver.
6*4882a593Smuzhiyun * Uses the kfifo buffer.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * To test without hardware use the sysfs trigger.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/export.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/interrupt.h>
15*4882a593Smuzhiyun #include <linux/irq.h>
16*4882a593Smuzhiyun #include <linux/bitmap.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/iio/iio.h>
19*4882a593Smuzhiyun #include <linux/iio/trigger_consumer.h>
20*4882a593Smuzhiyun #include <linux/iio/buffer.h>
21*4882a593Smuzhiyun #include <linux/iio/kfifo_buf.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "iio_simple_dummy.h"
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /* Some fake data */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun static const s16 fakedata[] = {
28*4882a593Smuzhiyun [DUMMY_INDEX_VOLTAGE_0] = 7,
29*4882a593Smuzhiyun [DUMMY_INDEX_DIFFVOLTAGE_1M2] = -33,
30*4882a593Smuzhiyun [DUMMY_INDEX_DIFFVOLTAGE_3M4] = -2,
31*4882a593Smuzhiyun [DUMMY_INDEX_ACCELX] = 344,
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /**
35*4882a593Smuzhiyun * iio_simple_dummy_trigger_h() - the trigger handler function
36*4882a593Smuzhiyun * @irq: the interrupt number
37*4882a593Smuzhiyun * @p: private data - always a pointer to the poll func.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * This is the guts of buffered capture. On a trigger event occurring,
40*4882a593Smuzhiyun * if the pollfunc is attached then this handler is called as a threaded
41*4882a593Smuzhiyun * interrupt (and hence may sleep). It is responsible for grabbing data
42*4882a593Smuzhiyun * from the device and pushing it into the associated buffer.
43*4882a593Smuzhiyun */
iio_simple_dummy_trigger_h(int irq,void * p)44*4882a593Smuzhiyun static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun struct iio_poll_func *pf = p;
47*4882a593Smuzhiyun struct iio_dev *indio_dev = pf->indio_dev;
48*4882a593Smuzhiyun int len = 0;
49*4882a593Smuzhiyun u16 *data;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
52*4882a593Smuzhiyun if (!data)
53*4882a593Smuzhiyun goto done;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) {
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * Three common options here:
58*4882a593Smuzhiyun * hardware scans: certain combinations of channels make
59*4882a593Smuzhiyun * up a fast read. The capture will consist of all of them.
60*4882a593Smuzhiyun * Hence we just call the grab data function and fill the
61*4882a593Smuzhiyun * buffer without processing.
62*4882a593Smuzhiyun * software scans: can be considered to be random access
63*4882a593Smuzhiyun * so efficient reading is just a case of minimal bus
64*4882a593Smuzhiyun * transactions.
65*4882a593Smuzhiyun * software culled hardware scans:
66*4882a593Smuzhiyun * occasionally a driver may process the nearest hardware
67*4882a593Smuzhiyun * scan to avoid storing elements that are not desired. This
68*4882a593Smuzhiyun * is the fiddliest option by far.
69*4882a593Smuzhiyun * Here let's pretend we have random access. And the values are
70*4882a593Smuzhiyun * in the constant table fakedata.
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun int i, j;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun for (i = 0, j = 0;
75*4882a593Smuzhiyun i < bitmap_weight(indio_dev->active_scan_mask,
76*4882a593Smuzhiyun indio_dev->masklength);
77*4882a593Smuzhiyun i++, j++) {
78*4882a593Smuzhiyun j = find_next_bit(indio_dev->active_scan_mask,
79*4882a593Smuzhiyun indio_dev->masklength, j);
80*4882a593Smuzhiyun /* random access read from the 'device' */
81*4882a593Smuzhiyun data[i] = fakedata[j];
82*4882a593Smuzhiyun len += 2;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun iio_push_to_buffers_with_timestamp(indio_dev, data,
87*4882a593Smuzhiyun iio_get_time_ns(indio_dev));
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun kfree(data);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun done:
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * Tell the core we are done with this trigger and ready for the
94*4882a593Smuzhiyun * next one.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun iio_trigger_notify_done(indio_dev->trig);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun return IRQ_HANDLED;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun
iio_simple_dummy_configure_buffer(struct iio_dev * indio_dev)104*4882a593Smuzhiyun int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun int ret;
107*4882a593Smuzhiyun struct iio_buffer *buffer;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* Allocate a buffer to use - here a kfifo */
110*4882a593Smuzhiyun buffer = iio_kfifo_allocate();
111*4882a593Smuzhiyun if (!buffer) {
112*4882a593Smuzhiyun ret = -ENOMEM;
113*4882a593Smuzhiyun goto error_ret;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun iio_device_attach_buffer(indio_dev, buffer);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun * Tell the core what device type specific functions should
120*4882a593Smuzhiyun * be run on either side of buffer capture enable / disable.
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun indio_dev->setup_ops = &iio_simple_dummy_buffer_setup_ops;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /*
125*4882a593Smuzhiyun * Configure a polling function.
126*4882a593Smuzhiyun * When a trigger event with this polling function connected
127*4882a593Smuzhiyun * occurs, this function is run. Typically this grabs data
128*4882a593Smuzhiyun * from the device.
129*4882a593Smuzhiyun *
130*4882a593Smuzhiyun * NULL for the bottom half. This is normally implemented only if we
131*4882a593Smuzhiyun * either want to ping a capture now pin (no sleeping) or grab
132*4882a593Smuzhiyun * a timestamp as close as possible to a data ready trigger firing.
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * IRQF_ONESHOT ensures irqs are masked such that only one instance
135*4882a593Smuzhiyun * of the handler can run at a time.
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * "iio_simple_dummy_consumer%d" formatting string for the irq 'name'
138*4882a593Smuzhiyun * as seen under /proc/interrupts. Remaining parameters as per printk.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
141*4882a593Smuzhiyun &iio_simple_dummy_trigger_h,
142*4882a593Smuzhiyun IRQF_ONESHOT,
143*4882a593Smuzhiyun indio_dev,
144*4882a593Smuzhiyun "iio_simple_dummy_consumer%d",
145*4882a593Smuzhiyun indio_dev->id);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (!indio_dev->pollfunc) {
148*4882a593Smuzhiyun ret = -ENOMEM;
149*4882a593Smuzhiyun goto error_free_buffer;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun * Notify the core that this device is capable of buffered capture
154*4882a593Smuzhiyun * driven by a trigger.
155*4882a593Smuzhiyun */
156*4882a593Smuzhiyun indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun return 0;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun error_free_buffer:
161*4882a593Smuzhiyun iio_kfifo_free(indio_dev->buffer);
162*4882a593Smuzhiyun error_ret:
163*4882a593Smuzhiyun return ret;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /**
167*4882a593Smuzhiyun * iio_simple_dummy_unconfigure_buffer() - release buffer resources
168*4882a593Smuzhiyun * @indio_dev: device instance state
169*4882a593Smuzhiyun */
iio_simple_dummy_unconfigure_buffer(struct iio_dev * indio_dev)170*4882a593Smuzhiyun void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun iio_dealloc_pollfunc(indio_dev->pollfunc);
173*4882a593Smuzhiyun iio_kfifo_free(indio_dev->buffer);
174*4882a593Smuzhiyun }
175