Commit | Line | Data |
---|---|---|
e6477000 JC |
1 | /** |
2 | * Copyright (c) 2011 Jonathan Cameron | |
3 | * | |
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. | |
7 | * | |
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> | |
14 | ||
06458e27 JC |
15 | #include <linux/iio/iio.h> |
16 | #include <linux/iio/sysfs.h> | |
17 | #include <linux/iio/events.h> | |
e6477000 JC |
18 | #include "iio_simple_dummy.h" |
19 | ||
20 | /* Evgen 'fakes' interrupt events for this example */ | |
21 | #include "iio_dummy_evgen.h" | |
22 | ||
23 | /** | |
24 | * iio_simple_dummy_read_event_config() - is event enabled? | |
25 | * @indio_dev: the device instance data | |
bda624b0 LPC |
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 | |
e6477000 JC |
29 | * |
30 | * This function would normally query the relevant registers or a cache to | |
31 | * discover if the event generation is enabled on the device. | |
32 | */ | |
33 | int iio_simple_dummy_read_event_config(struct iio_dev *indio_dev, | |
bda624b0 LPC |
34 | const struct iio_chan_spec *chan, |
35 | enum iio_event_type type, | |
36 | enum iio_event_direction dir) | |
e6477000 JC |
37 | { |
38 | struct iio_dummy_state *st = iio_priv(indio_dev); | |
39 | ||
40 | return st->event_en; | |
41 | } | |
42 | ||
43 | /** | |
44 | * iio_simple_dummy_write_event_config() - set whether event is enabled | |
45 | * @indio_dev: the device instance data | |
bda624b0 LPC |
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 | |
e6477000 JC |
49 | * @state: whether to enable or disable the device. |
50 | * | |
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. | |
54 | */ | |
55 | int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev, | |
bda624b0 LPC |
56 | const struct iio_chan_spec *chan, |
57 | enum iio_event_type type, | |
58 | enum iio_event_direction dir, | |
e6477000 JC |
59 | int state) |
60 | { | |
61 | struct iio_dummy_state *st = iio_priv(indio_dev); | |
62 | ||
63 | /* | |
64 | * Deliberately over the top code splitting to illustrate | |
65 | * how this is done when multiple events exist. | |
66 | */ | |
bda624b0 | 67 | switch (chan->type) { |
e6477000 | 68 | case IIO_VOLTAGE: |
bda624b0 | 69 | switch (type) { |
e6477000 | 70 | case IIO_EV_TYPE_THRESH: |
bda624b0 | 71 | if (dir == IIO_EV_DIR_RISING) |
e6477000 JC |
72 | st->event_en = state; |
73 | else | |
74 | return -EINVAL; | |
75 | break; | |
76 | default: | |
77 | return -EINVAL; | |
78 | } | |
79 | default: | |
80 | return -EINVAL; | |
81 | } | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
86 | /** | |
87 | * iio_simple_dummy_read_event_value() - get value associated with event | |
88 | * @indio_dev: device instance specific data | |
bda624b0 LPC |
89 | * @chan: channel for the event whose value is being read |
90 | * @type: type of the event whose value is being read | |
91 | * @dir: direction of the vent whose value is being read | |
92 | * @info: info type of the event whose value is being read | |
e6477000 JC |
93 | * @val: value for the event code. |
94 | * | |
95 | * Many devices provide a large set of events of which only a subset may | |
96 | * be enabled at a time, with value registers whose meaning changes depending | |
97 | * on the event enabled. This often means that the driver must cache the values | |
98 | * associated with each possible events so that the right value is in place when | |
99 | * the enabled event is changed. | |
100 | */ | |
101 | int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev, | |
bda624b0 LPC |
102 | const struct iio_chan_spec *chan, |
103 | enum iio_event_type type, | |
104 | enum iio_event_direction dir, | |
105 | enum iio_event_info info, | |
106 | int *val, int *val2) | |
e6477000 JC |
107 | { |
108 | struct iio_dummy_state *st = iio_priv(indio_dev); | |
109 | ||
110 | *val = st->event_val; | |
111 | ||
bda624b0 | 112 | return IIO_VAL_INT; |
e6477000 JC |
113 | } |
114 | ||
115 | /** | |
116 | * iio_simple_dummy_write_event_value() - set value associate with event | |
117 | * @indio_dev: device instance specific data | |
bda624b0 LPC |
118 | * @chan: channel for the event whose value is being set |
119 | * @type: type of the event whose value is being set | |
120 | * @dir: direction of the vent whose value is being set | |
121 | * @info: info type of the event whose value is being set | |
e6477000 JC |
122 | * @val: the value to be set. |
123 | */ | |
124 | int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev, | |
bda624b0 LPC |
125 | const struct iio_chan_spec *chan, |
126 | enum iio_event_type type, | |
127 | enum iio_event_direction dir, | |
128 | enum iio_event_info info, | |
129 | int val, int val2) | |
e6477000 JC |
130 | { |
131 | struct iio_dummy_state *st = iio_priv(indio_dev); | |
132 | ||
133 | st->event_val = val; | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | /** | |
139 | * iio_simple_dummy_event_handler() - identify and pass on event | |
140 | * @irq: irq of event line | |
141 | * @private: pointer to device instance state. | |
142 | * | |
143 | * This handler is responsible for querying the device to find out what | |
99de0c2b | 144 | * event occurred and for then pushing that event towards userspace. |
e6477000 JC |
145 | * Here only one event occurs so we push that directly on with locally |
146 | * grabbed timestamp. | |
147 | */ | |
148 | static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private) | |
149 | { | |
150 | struct iio_dev *indio_dev = private; | |
151 | iio_push_event(indio_dev, | |
152 | IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0, | |
153 | IIO_EV_DIR_RISING, | |
154 | IIO_EV_TYPE_THRESH, 0, 0, 0), | |
155 | iio_get_time_ns()); | |
156 | return IRQ_HANDLED; | |
157 | } | |
158 | ||
159 | /** | |
160 | * iio_simple_dummy_events_register() - setup interrupt handling for events | |
161 | * @indio_dev: device instance data | |
162 | * | |
163 | * This function requests the threaded interrupt to handle the events. | |
164 | * Normally the irq is a hardware interrupt and the number comes | |
165 | * from board configuration files. Here we get it from a companion | |
166 | * module that fakes the interrupt for us. Note that module in | |
167 | * no way forms part of this example. Just assume that events magically | |
168 | * appear via the provided interrupt. | |
169 | */ | |
170 | int iio_simple_dummy_events_register(struct iio_dev *indio_dev) | |
171 | { | |
172 | struct iio_dummy_state *st = iio_priv(indio_dev); | |
173 | int ret; | |
174 | ||
175 | /* Fire up event source - normally not present */ | |
176 | st->event_irq = iio_dummy_evgen_get_irq(); | |
177 | if (st->event_irq < 0) { | |
178 | ret = st->event_irq; | |
179 | goto error_ret; | |
180 | } | |
181 | ret = request_threaded_irq(st->event_irq, | |
182 | NULL, | |
183 | &iio_simple_dummy_event_handler, | |
184 | IRQF_ONESHOT, | |
185 | "iio_simple_event", | |
186 | indio_dev); | |
187 | if (ret < 0) | |
188 | goto error_free_evgen; | |
189 | return 0; | |
190 | ||
191 | error_free_evgen: | |
192 | iio_dummy_evgen_release_irq(st->event_irq); | |
193 | error_ret: | |
194 | return ret; | |
195 | } | |
196 | ||
197 | /** | |
198 | * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove | |
199 | * @indio_dev: device instance data | |
200 | */ | |
201 | int iio_simple_dummy_events_unregister(struct iio_dev *indio_dev) | |
202 | { | |
203 | struct iio_dummy_state *st = iio_priv(indio_dev); | |
204 | ||
205 | free_irq(st->event_irq, indio_dev); | |
206 | /* Not part of normal driver */ | |
207 | iio_dummy_evgen_release_irq(st->event_irq); | |
208 | ||
209 | return 0; | |
210 | } |