This source file includes following definitions.
- iio_simple_dummy_read_event_config
- iio_simple_dummy_write_event_config
- iio_simple_dummy_read_event_value
- iio_simple_dummy_write_event_value
- iio_simple_dummy_get_timestamp
- iio_simple_dummy_event_handler
- iio_simple_dummy_events_register
- iio_simple_dummy_events_unregister
1
2
3
4
5
6
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11
12 #include <linux/iio/iio.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/iio/events.h>
15 #include "iio_simple_dummy.h"
16
17
18 #include "iio_dummy_evgen.h"
19
20
21
22
23
24
25
26
27
28
29
30 int iio_simple_dummy_read_event_config(struct iio_dev *indio_dev,
31 const struct iio_chan_spec *chan,
32 enum iio_event_type type,
33 enum iio_event_direction dir)
34 {
35 struct iio_dummy_state *st = iio_priv(indio_dev);
36
37 return st->event_en;
38 }
39
40
41
42
43
44
45
46
47
48
49
50
51
52 int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev,
53 const struct iio_chan_spec *chan,
54 enum iio_event_type type,
55 enum iio_event_direction dir,
56 int state)
57 {
58 struct iio_dummy_state *st = iio_priv(indio_dev);
59
60
61
62
63
64 switch (chan->type) {
65 case IIO_VOLTAGE:
66 switch (type) {
67 case IIO_EV_TYPE_THRESH:
68 if (dir == IIO_EV_DIR_RISING)
69 st->event_en = state;
70 else
71 return -EINVAL;
72 break;
73 default:
74 return -EINVAL;
75 }
76 break;
77 case IIO_ACTIVITY:
78 switch (type) {
79 case IIO_EV_TYPE_THRESH:
80 st->event_en = state;
81 break;
82 default:
83 return -EINVAL;
84 }
85 break;
86 case IIO_STEPS:
87 switch (type) {
88 case IIO_EV_TYPE_CHANGE:
89 st->event_en = state;
90 break;
91 default:
92 return -EINVAL;
93 }
94 break;
95 default:
96 return -EINVAL;
97 }
98
99 return 0;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev,
118 const struct iio_chan_spec *chan,
119 enum iio_event_type type,
120 enum iio_event_direction dir,
121 enum iio_event_info info,
122 int *val, int *val2)
123 {
124 struct iio_dummy_state *st = iio_priv(indio_dev);
125
126 *val = st->event_val;
127
128 return IIO_VAL_INT;
129 }
130
131
132
133
134
135
136
137
138
139
140 int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev,
141 const struct iio_chan_spec *chan,
142 enum iio_event_type type,
143 enum iio_event_direction dir,
144 enum iio_event_info info,
145 int val, int val2)
146 {
147 struct iio_dummy_state *st = iio_priv(indio_dev);
148
149 st->event_val = val;
150
151 return 0;
152 }
153
154 static irqreturn_t iio_simple_dummy_get_timestamp(int irq, void *private)
155 {
156 struct iio_dev *indio_dev = private;
157 struct iio_dummy_state *st = iio_priv(indio_dev);
158
159 st->event_timestamp = iio_get_time_ns(indio_dev);
160 return IRQ_WAKE_THREAD;
161 }
162
163
164
165
166
167
168
169
170
171
172
173 static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private)
174 {
175 struct iio_dev *indio_dev = private;
176 struct iio_dummy_state *st = iio_priv(indio_dev);
177
178 dev_dbg(&indio_dev->dev, "id %x event %x\n",
179 st->regs->reg_id, st->regs->reg_data);
180
181 switch (st->regs->reg_data) {
182 case 0:
183 iio_push_event(indio_dev,
184 IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0,
185 IIO_EV_DIR_RISING,
186 IIO_EV_TYPE_THRESH, 0, 0, 0),
187 st->event_timestamp);
188 break;
189 case 1:
190 if (st->activity_running > st->event_val)
191 iio_push_event(indio_dev,
192 IIO_EVENT_CODE(IIO_ACTIVITY, 0,
193 IIO_MOD_RUNNING,
194 IIO_EV_DIR_RISING,
195 IIO_EV_TYPE_THRESH,
196 0, 0, 0),
197 st->event_timestamp);
198 break;
199 case 2:
200 if (st->activity_walking < st->event_val)
201 iio_push_event(indio_dev,
202 IIO_EVENT_CODE(IIO_ACTIVITY, 0,
203 IIO_MOD_WALKING,
204 IIO_EV_DIR_FALLING,
205 IIO_EV_TYPE_THRESH,
206 0, 0, 0),
207 st->event_timestamp);
208 break;
209 case 3:
210 iio_push_event(indio_dev,
211 IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
212 IIO_EV_DIR_NONE,
213 IIO_EV_TYPE_CHANGE, 0, 0, 0),
214 st->event_timestamp);
215 break;
216 default:
217 break;
218 }
219
220 return IRQ_HANDLED;
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234 int iio_simple_dummy_events_register(struct iio_dev *indio_dev)
235 {
236 struct iio_dummy_state *st = iio_priv(indio_dev);
237 int ret;
238
239
240 st->event_irq = iio_dummy_evgen_get_irq();
241 if (st->event_irq < 0) {
242 ret = st->event_irq;
243 goto error_ret;
244 }
245 st->regs = iio_dummy_evgen_get_regs(st->event_irq);
246
247 ret = request_threaded_irq(st->event_irq,
248 &iio_simple_dummy_get_timestamp,
249 &iio_simple_dummy_event_handler,
250 IRQF_ONESHOT,
251 "iio_simple_event",
252 indio_dev);
253 if (ret < 0)
254 goto error_free_evgen;
255 return 0;
256
257 error_free_evgen:
258 iio_dummy_evgen_release_irq(st->event_irq);
259 error_ret:
260 return ret;
261 }
262
263
264
265
266
267 void iio_simple_dummy_events_unregister(struct iio_dev *indio_dev)
268 {
269 struct iio_dummy_state *st = iio_priv(indio_dev);
270
271 free_irq(st->event_irq, indio_dev);
272
273 iio_dummy_evgen_release_irq(st->event_irq);
274 }