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>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/bitmap.h>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/kfifo_buf.h>
25 
26 #include "iio_simple_dummy.h"
27 
28 /* Some fake data */
29 
30 static const s16 fakedata[] = {
31 	[DUMMY_INDEX_VOLTAGE_0] = 7,
32 	[DUMMY_INDEX_DIFFVOLTAGE_1M2] = -33,
33 	[DUMMY_INDEX_DIFFVOLTAGE_3M4] = -2,
34 	[DUMMY_INDEX_ACCELX] = 344,
35 };
36 
37 /**
38  * iio_simple_dummy_trigger_h() - the trigger handler function
39  * @irq: the interrupt number
40  * @p: private data - always a pointer to the poll func.
41  *
42  * This is the guts of buffered capture. On a trigger event occurring,
43  * if the pollfunc is attached then this handler is called as a threaded
44  * interrupt (and hence may sleep). It is responsible for grabbing data
45  * from the device and pushing it into the associated buffer.
46  */
47 static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
48 {
49 	struct iio_poll_func *pf = p;
50 	struct iio_dev *indio_dev = pf->indio_dev;
51 	int len = 0;
52 	u16 *data;
53 
54 	data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
55 	if (!data)
56 		goto done;
57 
58 	if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) {
59 		/*
60 		 * Three common options here:
61 		 * hardware scans: certain combinations of channels make
62 		 *   up a fast read.  The capture will consist of all of them.
63 		 *   Hence we just call the grab data function and fill the
64 		 *   buffer without processing.
65 		 * software scans: can be considered to be random access
66 		 *   so efficient reading is just a case of minimal bus
67 		 *   transactions.
68 		 * software culled hardware scans:
69 		 *   occasionally a driver may process the nearest hardware
70 		 *   scan to avoid storing elements that are not desired. This
71 		 *   is the fiddliest option by far.
72 		 * Here let's pretend we have random access. And the values are
73 		 * in the constant table fakedata.
74 		 */
75 		int i, j;
76 
77 		for (i = 0, j = 0;
78 		     i < bitmap_weight(indio_dev->active_scan_mask,
79 				       indio_dev->masklength);
80 		     i++, j++) {
81 			j = find_next_bit(indio_dev->active_scan_mask,
82 					  indio_dev->masklength, j);
83 			/* random access read from the 'device' */
84 			data[i] = fakedata[j];
85 			len += 2;
86 		}
87 	}
88 
89 	iio_push_to_buffers_with_timestamp(indio_dev, data,
90 					   iio_get_time_ns(indio_dev));
91 
92 	kfree(data);
93 
94 done:
95 	/*
96 	 * Tell the core we are done with this trigger and ready for the
97 	 * next one.
98 	 */
99 	iio_trigger_notify_done(indio_dev->trig);
100 
101 	return IRQ_HANDLED;
102 }
103 
104 static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
105 	/*
106 	 * iio_triggered_buffer_postenable:
107 	 * Generic function that simply attaches the pollfunc to the trigger.
108 	 * Replace this to mess with hardware state before we attach the
109 	 * trigger.
110 	 */
111 	.postenable = &iio_triggered_buffer_postenable,
112 	/*
113 	 * iio_triggered_buffer_predisable:
114 	 * Generic function that simple detaches the pollfunc from the trigger.
115 	 * Replace this to put hardware state back again after the trigger is
116 	 * detached but before userspace knows we have disabled the ring.
117 	 */
118 	.predisable = &iio_triggered_buffer_predisable,
119 };
120 
121 int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
122 {
123 	int ret;
124 	struct iio_buffer *buffer;
125 
126 	/* Allocate a buffer to use - here a kfifo */
127 	buffer = iio_kfifo_allocate();
128 	if (!buffer) {
129 		ret = -ENOMEM;
130 		goto error_ret;
131 	}
132 
133 	iio_device_attach_buffer(indio_dev, buffer);
134 
135 	/*
136 	 * Tell the core what device type specific functions should
137 	 * be run on either side of buffer capture enable / disable.
138 	 */
139 	indio_dev->setup_ops = &iio_simple_dummy_buffer_setup_ops;
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 	 *
147 	 * NULL for the bottom half. This is normally implemented only if we
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) {
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;
174 
175 	return 0;
176 
177 error_free_buffer:
178 	iio_kfifo_free(indio_dev->buffer);
179 error_ret:
180 	return ret;
181 }
182 
183 /**
184  * iio_simple_dummy_unconfigure_buffer() - release buffer resources
185  * @indo_dev: device instance state
186  */
187 void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
188 {
189 	iio_dealloc_pollfunc(indio_dev->pollfunc);
190 	iio_kfifo_free(indio_dev->buffer);
191 }
192