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