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>
17 #include "iio_simple_dummy.h"
19 /* Evgen 'fakes' interrupt events for this example */
20 #include "iio_dummy_evgen.h"
23 * iio_simple_dummy_read_event_config() - is event enabled?
24 * @indio_dev: the device instance data
25 * @event_code: event code of the event 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,
33 struct iio_dummy_state *st = iio_priv(indio_dev);
39 * iio_simple_dummy_write_event_config() - set whether event is enabled
40 * @indio_dev: the device instance data
41 * @event_code: event code of event being enabled/disabled
42 * @state: whether to enable or disable the device.
44 * This function would normally set the relevant registers on the devices
45 * so that it generates the specified event. Here it just sets up a cached
48 int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev,
52 struct iio_dummy_state *st = iio_priv(indio_dev);
55 * Deliberately over the top code splitting to illustrate
56 * how this is done when multiple events exist.
58 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
60 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
61 case IIO_EV_TYPE_THRESH:
62 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
79 * iio_simple_dummy_read_event_value() - get value associated with event
80 * @indio_dev: device instance specific data
81 * @event_code: event code for the event whose value is being queried
82 * @val: value for the event code.
84 * Many devices provide a large set of events of which only a subset may
85 * be enabled at a time, with value registers whose meaning changes depending
86 * on the event enabled. This often means that the driver must cache the values
87 * associated with each possible events so that the right value is in place when
88 * the enabled event is changed.
90 int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev,
94 struct iio_dummy_state *st = iio_priv(indio_dev);
102 * iio_simple_dummy_write_event_value() - set value associate with event
103 * @indio_dev: device instance specific data
104 * @event_code: event code for the event whose value is being set
105 * @val: the value to be set.
107 int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev,
111 struct iio_dummy_state *st = iio_priv(indio_dev);
119 * iio_simple_dummy_event_handler() - identify and pass on event
120 * @irq: irq of event line
121 * @private: pointer to device instance state.
123 * This handler is responsible for querying the device to find out what
124 * event occured and for then pushing that event towards userspace.
125 * Here only one event occurs so we push that directly on with locally
128 static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private)
130 struct iio_dev *indio_dev = private;
131 iio_push_event(indio_dev,
132 IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0,
134 IIO_EV_TYPE_THRESH, 0, 0, 0),
140 * iio_simple_dummy_events_register() - setup interrupt handling for events
141 * @indio_dev: device instance data
143 * This function requests the threaded interrupt to handle the events.
144 * Normally the irq is a hardware interrupt and the number comes
145 * from board configuration files. Here we get it from a companion
146 * module that fakes the interrupt for us. Note that module in
147 * no way forms part of this example. Just assume that events magically
148 * appear via the provided interrupt.
150 int iio_simple_dummy_events_register(struct iio_dev *indio_dev)
152 struct iio_dummy_state *st = iio_priv(indio_dev);
155 /* Fire up event source - normally not present */
156 st->event_irq = iio_dummy_evgen_get_irq();
157 if (st->event_irq < 0) {
161 ret = request_threaded_irq(st->event_irq,
163 &iio_simple_dummy_event_handler,
168 goto error_free_evgen;
172 iio_dummy_evgen_release_irq(st->event_irq);
178 * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove
179 * @indio_dev: device instance data
181 int iio_simple_dummy_events_unregister(struct iio_dev *indio_dev)
183 struct iio_dummy_state *st = iio_priv(indio_dev);
185 free_irq(st->event_irq, indio_dev);
186 /* Not part of normal driver */
187 iio_dummy_evgen_release_irq(st->event_irq);