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 * Event handling elements of industrial I/O reference driver.
9 */
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/events.h>
18 #include "iio_simple_dummy.h"
19
20 /* Evgen 'fakes' interrupt events for this example */
21 #include "iio_dummy_evgen.h"
22
23 /**
24 * iio_simple_dummy_read_event_config() - is event enabled?
25 * @indio_dev: the device instance data
26 * @chan: channel for the event whose state is being queried
27 * @type: type of the event whose state is being queried
28 * @dir: direction of the vent whose state is being queried
29 *
30 * This function would normally query the relevant registers or a cache to
31 * discover if the event generation is enabled on the device.
32 */
iio_simple_dummy_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)33 int iio_simple_dummy_read_event_config(struct iio_dev *indio_dev,
34 const struct iio_chan_spec *chan,
35 enum iio_event_type type,
36 enum iio_event_direction dir)
37 {
38 struct iio_dummy_state *st = iio_priv(indio_dev);
39
40 return st->event_en;
41 }
42
43 /**
44 * iio_simple_dummy_write_event_config() - set whether event is enabled
45 * @indio_dev: the device instance data
46 * @chan: channel for the event whose state is being set
47 * @type: type of the event whose state is being set
48 * @dir: direction of the vent whose state is being set
49 * @state: whether to enable or disable the device.
50 *
51 * This function would normally set the relevant registers on the devices
52 * so that it generates the specified event. Here it just sets up a cached
53 * value.
54 */
iio_simple_dummy_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)55 int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev,
56 const struct iio_chan_spec *chan,
57 enum iio_event_type type,
58 enum iio_event_direction dir,
59 int state)
60 {
61 struct iio_dummy_state *st = iio_priv(indio_dev);
62
63 /*
64 * Deliberately over the top code splitting to illustrate
65 * how this is done when multiple events exist.
66 */
67 switch (chan->type) {
68 case IIO_VOLTAGE:
69 switch (type) {
70 case IIO_EV_TYPE_THRESH:
71 if (dir == IIO_EV_DIR_RISING)
72 st->event_en = state;
73 else
74 return -EINVAL;
75 default:
76 return -EINVAL;
77 }
78 break;
79 case IIO_ACTIVITY:
80 switch (type) {
81 case IIO_EV_TYPE_THRESH:
82 st->event_en = state;
83 break;
84 default:
85 return -EINVAL;
86 }
87 case IIO_STEPS:
88 switch (type) {
89 case IIO_EV_TYPE_CHANGE:
90 st->event_en = state;
91 break;
92 default:
93 return -EINVAL;
94 }
95 default:
96 return -EINVAL;
97 }
98
99 return 0;
100 }
101
102 /**
103 * iio_simple_dummy_read_event_value() - get value associated with event
104 * @indio_dev: device instance specific data
105 * @chan: channel for the event whose value is being read
106 * @type: type of the event whose value is being read
107 * @dir: direction of the vent whose value is being read
108 * @info: info type of the event whose value is being read
109 * @val: value for the event code.
110 *
111 * Many devices provide a large set of events of which only a subset may
112 * be enabled at a time, with value registers whose meaning changes depending
113 * on the event enabled. This often means that the driver must cache the values
114 * associated with each possible events so that the right value is in place when
115 * the enabled event is changed.
116 */
iio_simple_dummy_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)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 * iio_simple_dummy_write_event_value() - set value associate with event
133 * @indio_dev: device instance specific data
134 * @chan: channel for the event whose value is being set
135 * @type: type of the event whose value is being set
136 * @dir: direction of the vent whose value is being set
137 * @info: info type of the event whose value is being set
138 * @val: the value to be set.
139 */
iio_simple_dummy_write_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)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 /**
155 * iio_simple_dummy_event_handler() - identify and pass on event
156 * @irq: irq of event line
157 * @private: pointer to device instance state.
158 *
159 * This handler is responsible for querying the device to find out what
160 * event occurred and for then pushing that event towards userspace.
161 * Here only one event occurs so we push that directly on with locally
162 * grabbed timestamp.
163 */
iio_simple_dummy_event_handler(int irq,void * private)164 static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private)
165 {
166 struct iio_dev *indio_dev = private;
167 struct iio_dummy_state *st = iio_priv(indio_dev);
168
169 dev_dbg(&indio_dev->dev, "id %x event %x\n",
170 st->regs->reg_id, st->regs->reg_data);
171
172 switch (st->regs->reg_data) {
173 case 0:
174 iio_push_event(indio_dev,
175 IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0,
176 IIO_EV_DIR_RISING,
177 IIO_EV_TYPE_THRESH, 0, 0, 0),
178 iio_get_time_ns());
179 break;
180 case 1:
181 if (st->activity_running > st->event_val)
182 iio_push_event(indio_dev,
183 IIO_EVENT_CODE(IIO_ACTIVITY, 0,
184 IIO_MOD_RUNNING,
185 IIO_EV_DIR_RISING,
186 IIO_EV_TYPE_THRESH,
187 0, 0, 0),
188 iio_get_time_ns());
189 break;
190 case 2:
191 if (st->activity_walking < st->event_val)
192 iio_push_event(indio_dev,
193 IIO_EVENT_CODE(IIO_ACTIVITY, 0,
194 IIO_MOD_WALKING,
195 IIO_EV_DIR_FALLING,
196 IIO_EV_TYPE_THRESH,
197 0, 0, 0),
198 iio_get_time_ns());
199 break;
200 case 3:
201 iio_push_event(indio_dev,
202 IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
203 IIO_EV_DIR_NONE,
204 IIO_EV_TYPE_CHANGE, 0, 0, 0),
205 iio_get_time_ns());
206 break;
207 default:
208 break;
209 }
210
211 return IRQ_HANDLED;
212 }
213
214 /**
215 * iio_simple_dummy_events_register() - setup interrupt handling for events
216 * @indio_dev: device instance data
217 *
218 * This function requests the threaded interrupt to handle the events.
219 * Normally the irq is a hardware interrupt and the number comes
220 * from board configuration files. Here we get it from a companion
221 * module that fakes the interrupt for us. Note that module in
222 * no way forms part of this example. Just assume that events magically
223 * appear via the provided interrupt.
224 */
iio_simple_dummy_events_register(struct iio_dev * indio_dev)225 int iio_simple_dummy_events_register(struct iio_dev *indio_dev)
226 {
227 struct iio_dummy_state *st = iio_priv(indio_dev);
228 int ret;
229
230 /* Fire up event source - normally not present */
231 st->event_irq = iio_dummy_evgen_get_irq();
232 if (st->event_irq < 0) {
233 ret = st->event_irq;
234 goto error_ret;
235 }
236 st->regs = iio_dummy_evgen_get_regs(st->event_irq);
237
238 ret = request_threaded_irq(st->event_irq,
239 NULL,
240 &iio_simple_dummy_event_handler,
241 IRQF_ONESHOT,
242 "iio_simple_event",
243 indio_dev);
244 if (ret < 0)
245 goto error_free_evgen;
246 return 0;
247
248 error_free_evgen:
249 iio_dummy_evgen_release_irq(st->event_irq);
250 error_ret:
251 return ret;
252 }
253
254 /**
255 * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove
256 * @indio_dev: device instance data
257 */
iio_simple_dummy_events_unregister(struct iio_dev * indio_dev)258 int iio_simple_dummy_events_unregister(struct iio_dev *indio_dev)
259 {
260 struct iio_dummy_state *st = iio_priv(indio_dev);
261
262 free_irq(st->event_irq, indio_dev);
263 /* Not part of normal driver */
264 iio_dummy_evgen_release_irq(st->event_irq);
265
266 return 0;
267 }
268