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>
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 * @event_code: event code of the event being queried
28 * This function would normally query the relevant registers or a cache to
29 * discover if the event generation is enabled on the device.
31 int iio_simple_dummy_read_event_config(struct iio_dev *indio_dev,
34 struct iio_dummy_state *st = iio_priv(indio_dev);
40 * iio_simple_dummy_write_event_config() - set whether event is enabled
41 * @indio_dev: the device instance data
42 * @event_code: event code of event being enabled/disabled
43 * @state: whether to enable or disable the device.
45 * This function would normally set the relevant registers on the devices
46 * so that it generates the specified event. Here it just sets up a cached
49 int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev,
53 struct iio_dummy_state *st = iio_priv(indio_dev);
56 * Deliberately over the top code splitting to illustrate
57 * how this is done when multiple events exist.
59 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
61 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
62 case IIO_EV_TYPE_THRESH:
63 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
80 * iio_simple_dummy_read_event_value() - get value associated with event
81 * @indio_dev: device instance specific data
82 * @event_code: event code for the event whose value is being queried
83 * @val: value for the event code.
85 * Many devices provide a large set of events of which only a subset may
86 * be enabled at a time, with value registers whose meaning changes depending
87 * on the event enabled. This often means that the driver must cache the values
88 * associated with each possible events so that the right value is in place when
89 * the enabled event is changed.
91 int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev,
95 struct iio_dummy_state *st = iio_priv(indio_dev);
103 * iio_simple_dummy_write_event_value() - set value associate with event
104 * @indio_dev: device instance specific data
105 * @event_code: event code for the event whose value is being set
106 * @val: the value to be set.
108 int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev,
112 struct iio_dummy_state *st = iio_priv(indio_dev);
120 * iio_simple_dummy_event_handler() - identify and pass on event
121 * @irq: irq of event line
122 * @private: pointer to device instance state.
124 * This handler is responsible for querying the device to find out what
125 * event occured and for then pushing that event towards userspace.
126 * Here only one event occurs so we push that directly on with locally
129 static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private)
131 struct iio_dev *indio_dev = private;
132 iio_push_event(indio_dev,
133 IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0,
135 IIO_EV_TYPE_THRESH, 0, 0, 0),
141 * iio_simple_dummy_events_register() - setup interrupt handling for events
142 * @indio_dev: device instance data
144 * This function requests the threaded interrupt to handle the events.
145 * Normally the irq is a hardware interrupt and the number comes
146 * from board configuration files. Here we get it from a companion
147 * module that fakes the interrupt for us. Note that module in
148 * no way forms part of this example. Just assume that events magically
149 * appear via the provided interrupt.
151 int iio_simple_dummy_events_register(struct iio_dev *indio_dev)
153 struct iio_dummy_state *st = iio_priv(indio_dev);
156 /* Fire up event source - normally not present */
157 st->event_irq = iio_dummy_evgen_get_irq();
158 if (st->event_irq < 0) {
162 ret = request_threaded_irq(st->event_irq,
164 &iio_simple_dummy_event_handler,
169 goto error_free_evgen;
173 iio_dummy_evgen_release_irq(st->event_irq);
179 * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove
180 * @indio_dev: device instance data
182 int iio_simple_dummy_events_unregister(struct iio_dev *indio_dev)
184 struct iio_dummy_state *st = iio_priv(indio_dev);
186 free_irq(st->event_irq, indio_dev);
187 /* Not part of normal driver */
188 iio_dummy_evgen_release_irq(st->event_irq);