Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / dummy / iio_simple_dummy_events.c
blob7ec2a0bb08076ffa4ffefaf50cc0336471553756
1 /**
2 * Copyright (c) 2011 Jonathan Cameron
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.
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>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/events.h>
18 #include "iio_simple_dummy.h"
20 /* Evgen 'fakes' interrupt events for this example */
21 #include "iio_dummy_evgen.h"
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
30 * This function would normally query the relevant registers or a cache to
31 * discover if the event generation is enabled on the device.
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)
38 struct iio_dummy_state *st = iio_priv(indio_dev);
40 return st->event_en;
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.
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.
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)
61 struct iio_dummy_state *st = iio_priv(indio_dev);
64 * Deliberately over the top code splitting to illustrate
65 * how this is done when multiple events exist.
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 break;
76 default:
77 return -EINVAL;
79 break;
80 case IIO_ACTIVITY:
81 switch (type) {
82 case IIO_EV_TYPE_THRESH:
83 st->event_en = state;
84 break;
85 default:
86 return -EINVAL;
88 break;
89 case IIO_STEPS:
90 switch (type) {
91 case IIO_EV_TYPE_CHANGE:
92 st->event_en = state;
93 break;
94 default:
95 return -EINVAL;
97 break;
98 default:
99 return -EINVAL;
102 return 0;
106 * iio_simple_dummy_read_event_value() - get value associated with event
107 * @indio_dev: device instance specific data
108 * @chan: channel for the event whose value is being read
109 * @type: type of the event whose value is being read
110 * @dir: direction of the vent whose value is being read
111 * @info: info type of the event whose value is being read
112 * @val: value for the event code.
114 * Many devices provide a large set of events of which only a subset may
115 * be enabled at a time, with value registers whose meaning changes depending
116 * on the event enabled. This often means that the driver must cache the values
117 * associated with each possible events so that the right value is in place when
118 * the enabled event is changed.
120 int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev,
121 const struct iio_chan_spec *chan,
122 enum iio_event_type type,
123 enum iio_event_direction dir,
124 enum iio_event_info info,
125 int *val, int *val2)
127 struct iio_dummy_state *st = iio_priv(indio_dev);
129 *val = st->event_val;
131 return IIO_VAL_INT;
135 * iio_simple_dummy_write_event_value() - set value associate with event
136 * @indio_dev: device instance specific data
137 * @chan: channel for the event whose value is being set
138 * @type: type of the event whose value is being set
139 * @dir: direction of the vent whose value is being set
140 * @info: info type of the event whose value is being set
141 * @val: the value to be set.
143 int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev,
144 const struct iio_chan_spec *chan,
145 enum iio_event_type type,
146 enum iio_event_direction dir,
147 enum iio_event_info info,
148 int val, int val2)
150 struct iio_dummy_state *st = iio_priv(indio_dev);
152 st->event_val = val;
154 return 0;
157 static irqreturn_t iio_simple_dummy_get_timestamp(int irq, void *private)
159 struct iio_dev *indio_dev = private;
160 struct iio_dummy_state *st = iio_priv(indio_dev);
162 st->event_timestamp = iio_get_time_ns(indio_dev);
163 return IRQ_WAKE_THREAD;
167 * iio_simple_dummy_event_handler() - identify and pass on event
168 * @irq: irq of event line
169 * @private: pointer to device instance state.
171 * This handler is responsible for querying the device to find out what
172 * event occurred and for then pushing that event towards userspace.
173 * Here only one event occurs so we push that directly on with locally
174 * grabbed timestamp.
176 static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private)
178 struct iio_dev *indio_dev = private;
179 struct iio_dummy_state *st = iio_priv(indio_dev);
181 dev_dbg(&indio_dev->dev, "id %x event %x\n",
182 st->regs->reg_id, st->regs->reg_data);
184 switch (st->regs->reg_data) {
185 case 0:
186 iio_push_event(indio_dev,
187 IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0,
188 IIO_EV_DIR_RISING,
189 IIO_EV_TYPE_THRESH, 0, 0, 0),
190 st->event_timestamp);
191 break;
192 case 1:
193 if (st->activity_running > st->event_val)
194 iio_push_event(indio_dev,
195 IIO_EVENT_CODE(IIO_ACTIVITY, 0,
196 IIO_MOD_RUNNING,
197 IIO_EV_DIR_RISING,
198 IIO_EV_TYPE_THRESH,
199 0, 0, 0),
200 st->event_timestamp);
201 break;
202 case 2:
203 if (st->activity_walking < st->event_val)
204 iio_push_event(indio_dev,
205 IIO_EVENT_CODE(IIO_ACTIVITY, 0,
206 IIO_MOD_WALKING,
207 IIO_EV_DIR_FALLING,
208 IIO_EV_TYPE_THRESH,
209 0, 0, 0),
210 st->event_timestamp);
211 break;
212 case 3:
213 iio_push_event(indio_dev,
214 IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
215 IIO_EV_DIR_NONE,
216 IIO_EV_TYPE_CHANGE, 0, 0, 0),
217 st->event_timestamp);
218 break;
219 default:
220 break;
223 return IRQ_HANDLED;
227 * iio_simple_dummy_events_register() - setup interrupt handling for events
228 * @indio_dev: device instance data
230 * This function requests the threaded interrupt to handle the events.
231 * Normally the irq is a hardware interrupt and the number comes
232 * from board configuration files. Here we get it from a companion
233 * module that fakes the interrupt for us. Note that module in
234 * no way forms part of this example. Just assume that events magically
235 * appear via the provided interrupt.
237 int iio_simple_dummy_events_register(struct iio_dev *indio_dev)
239 struct iio_dummy_state *st = iio_priv(indio_dev);
240 int ret;
242 /* Fire up event source - normally not present */
243 st->event_irq = iio_dummy_evgen_get_irq();
244 if (st->event_irq < 0) {
245 ret = st->event_irq;
246 goto error_ret;
248 st->regs = iio_dummy_evgen_get_regs(st->event_irq);
250 ret = request_threaded_irq(st->event_irq,
251 &iio_simple_dummy_get_timestamp,
252 &iio_simple_dummy_event_handler,
253 IRQF_ONESHOT,
254 "iio_simple_event",
255 indio_dev);
256 if (ret < 0)
257 goto error_free_evgen;
258 return 0;
260 error_free_evgen:
261 iio_dummy_evgen_release_irq(st->event_irq);
262 error_ret:
263 return ret;
267 * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove
268 * @indio_dev: device instance data
270 void iio_simple_dummy_events_unregister(struct iio_dev *indio_dev)
272 struct iio_dummy_state *st = iio_priv(indio_dev);
274 free_irq(st->event_irq, indio_dev);
275 /* Not part of normal driver */
276 iio_dummy_evgen_release_irq(st->event_irq);