1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011 Jonathan Cameron
5 * Event handling elements of industrial I/O reference driver.
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/iio/events.h>
15 #include "iio_simple_dummy.h"
17 /* Evgen 'fakes' interrupt events for this example */
18 #include "iio_dummy_evgen.h"
21 * iio_simple_dummy_read_event_config() - is event enabled?
22 * @indio_dev: the device instance data
23 * @chan: channel for the event whose state is being queried
24 * @type: type of the event whose state is being queried
25 * @dir: direction of the vent whose state is being queried
27 * This function would normally query the relevant registers or a cache to
28 * discover if the event generation is enabled on the device.
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
)
35 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
41 * iio_simple_dummy_write_event_config() - set whether event is enabled
42 * @indio_dev: the device instance data
43 * @chan: channel for the event whose state is being set
44 * @type: type of the event whose state is being set
45 * @dir: direction of the vent whose state is being set
46 * @state: whether to enable or disable the device.
48 * This function would normally set the relevant registers on the devices
49 * so that it generates the specified event. Here it just sets up a cached
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
,
58 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
61 * Deliberately over the top code splitting to illustrate
62 * how this is done when multiple events exist.
67 case IIO_EV_TYPE_THRESH
:
68 if (dir
== IIO_EV_DIR_RISING
)
79 case IIO_EV_TYPE_THRESH
:
88 case IIO_EV_TYPE_CHANGE
:
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.
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.
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
,
124 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
126 *val
= st
->event_val
;
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.
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
,
147 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
154 static irqreturn_t
iio_simple_dummy_get_timestamp(int irq
, void *private)
156 struct iio_dev
*indio_dev
= private;
157 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
159 st
->event_timestamp
= iio_get_time_ns(indio_dev
);
160 return IRQ_WAKE_THREAD
;
164 * iio_simple_dummy_event_handler() - identify and pass on event
165 * @irq: irq of event line
166 * @private: pointer to device instance state.
168 * This handler is responsible for querying the device to find out what
169 * event occurred and for then pushing that event towards userspace.
170 * Here only one event occurs so we push that directly on with locally
173 static irqreturn_t
iio_simple_dummy_event_handler(int irq
, void *private)
175 struct iio_dev
*indio_dev
= private;
176 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
178 dev_dbg(&indio_dev
->dev
, "id %x event %x\n",
179 st
->regs
->reg_id
, st
->regs
->reg_data
);
181 switch (st
->regs
->reg_data
) {
183 iio_push_event(indio_dev
,
184 IIO_EVENT_CODE(IIO_VOLTAGE
, 0, 0,
186 IIO_EV_TYPE_THRESH
, 0, 0, 0),
187 st
->event_timestamp
);
190 if (st
->activity_running
> st
->event_val
)
191 iio_push_event(indio_dev
,
192 IIO_EVENT_CODE(IIO_ACTIVITY
, 0,
197 st
->event_timestamp
);
200 if (st
->activity_walking
< st
->event_val
)
201 iio_push_event(indio_dev
,
202 IIO_EVENT_CODE(IIO_ACTIVITY
, 0,
207 st
->event_timestamp
);
210 iio_push_event(indio_dev
,
211 IIO_EVENT_CODE(IIO_STEPS
, 0, IIO_NO_MOD
,
213 IIO_EV_TYPE_CHANGE
, 0, 0, 0),
214 st
->event_timestamp
);
224 * iio_simple_dummy_events_register() - setup interrupt handling for events
225 * @indio_dev: device instance data
227 * This function requests the threaded interrupt to handle the events.
228 * Normally the irq is a hardware interrupt and the number comes
229 * from board configuration files. Here we get it from a companion
230 * module that fakes the interrupt for us. Note that module in
231 * no way forms part of this example. Just assume that events magically
232 * appear via the provided interrupt.
234 int iio_simple_dummy_events_register(struct iio_dev
*indio_dev
)
236 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
239 /* Fire up event source - normally not present */
240 st
->event_irq
= iio_dummy_evgen_get_irq();
241 if (st
->event_irq
< 0) {
245 st
->regs
= iio_dummy_evgen_get_regs(st
->event_irq
);
247 ret
= request_threaded_irq(st
->event_irq
,
248 &iio_simple_dummy_get_timestamp
,
249 &iio_simple_dummy_event_handler
,
254 goto error_free_evgen
;
258 iio_dummy_evgen_release_irq(st
->event_irq
);
264 * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove
265 * @indio_dev: device instance data
267 void iio_simple_dummy_events_unregister(struct iio_dev
*indio_dev
)
269 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
271 free_irq(st
->event_irq
, indio_dev
);
272 /* Not part of normal driver */
273 iio_dummy_evgen_release_irq(st
->event_irq
);