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.
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"
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
);
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
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
,
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.
70 case IIO_EV_TYPE_THRESH
:
71 if (dir
== IIO_EV_DIR_RISING
)
81 case IIO_EV_TYPE_THRESH
:
90 case IIO_EV_TYPE_CHANGE
:
105 * iio_simple_dummy_read_event_value() - get value associated with event
106 * @indio_dev: device instance specific data
107 * @chan: channel for the event whose value is being read
108 * @type: type of the event whose value is being read
109 * @dir: direction of the vent whose value is being read
110 * @info: info type of the event whose value is being read
111 * @val: value for the event code.
113 * Many devices provide a large set of events of which only a subset may
114 * be enabled at a time, with value registers whose meaning changes depending
115 * on the event enabled. This often means that the driver must cache the values
116 * associated with each possible events so that the right value is in place when
117 * the enabled event is changed.
119 int iio_simple_dummy_read_event_value(struct iio_dev
*indio_dev
,
120 const struct iio_chan_spec
*chan
,
121 enum iio_event_type type
,
122 enum iio_event_direction dir
,
123 enum iio_event_info info
,
126 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
128 *val
= st
->event_val
;
134 * iio_simple_dummy_write_event_value() - set value associate with event
135 * @indio_dev: device instance specific data
136 * @chan: channel for the event whose value is being set
137 * @type: type of the event whose value is being set
138 * @dir: direction of the vent whose value is being set
139 * @info: info type of the event whose value is being set
140 * @val: the value to be set.
142 int iio_simple_dummy_write_event_value(struct iio_dev
*indio_dev
,
143 const struct iio_chan_spec
*chan
,
144 enum iio_event_type type
,
145 enum iio_event_direction dir
,
146 enum iio_event_info info
,
149 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
157 * iio_simple_dummy_event_handler() - identify and pass on event
158 * @irq: irq of event line
159 * @private: pointer to device instance state.
161 * This handler is responsible for querying the device to find out what
162 * event occurred and for then pushing that event towards userspace.
163 * Here only one event occurs so we push that directly on with locally
166 static irqreturn_t
iio_simple_dummy_event_handler(int irq
, void *private)
168 struct iio_dev
*indio_dev
= private;
169 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
171 dev_dbg(&indio_dev
->dev
, "id %x event %x\n",
172 st
->regs
->reg_id
, st
->regs
->reg_data
);
174 switch (st
->regs
->reg_data
) {
176 iio_push_event(indio_dev
,
177 IIO_EVENT_CODE(IIO_VOLTAGE
, 0, 0,
179 IIO_EV_TYPE_THRESH
, 0, 0, 0),
183 if (st
->activity_running
> st
->event_val
)
184 iio_push_event(indio_dev
,
185 IIO_EVENT_CODE(IIO_ACTIVITY
, 0,
193 if (st
->activity_walking
< st
->event_val
)
194 iio_push_event(indio_dev
,
195 IIO_EVENT_CODE(IIO_ACTIVITY
, 0,
203 iio_push_event(indio_dev
,
204 IIO_EVENT_CODE(IIO_STEPS
, 0, IIO_NO_MOD
,
206 IIO_EV_TYPE_CHANGE
, 0, 0, 0),
217 * iio_simple_dummy_events_register() - setup interrupt handling for events
218 * @indio_dev: device instance data
220 * This function requests the threaded interrupt to handle the events.
221 * Normally the irq is a hardware interrupt and the number comes
222 * from board configuration files. Here we get it from a companion
223 * module that fakes the interrupt for us. Note that module in
224 * no way forms part of this example. Just assume that events magically
225 * appear via the provided interrupt.
227 int iio_simple_dummy_events_register(struct iio_dev
*indio_dev
)
229 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
232 /* Fire up event source - normally not present */
233 st
->event_irq
= iio_dummy_evgen_get_irq();
234 if (st
->event_irq
< 0) {
238 st
->regs
= iio_dummy_evgen_get_regs(st
->event_irq
);
240 ret
= request_threaded_irq(st
->event_irq
,
242 &iio_simple_dummy_event_handler
,
247 goto error_free_evgen
;
251 iio_dummy_evgen_release_irq(st
->event_irq
);
257 * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove
258 * @indio_dev: device instance data
260 void iio_simple_dummy_events_unregister(struct iio_dev
*indio_dev
)
262 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
264 free_irq(st
->event_irq
, indio_dev
);
265 /* Not part of normal driver */
266 iio_dummy_evgen_release_irq(st
->event_irq
);