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