Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 15 | #include <linux/iio/iio.h> |
| 16 | #include <linux/iio/sysfs.h> |
| 17 | #include <linux/iio/events.h> |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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 |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 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 |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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, |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 34 | const struct iio_chan_spec *chan, |
| 35 | enum iio_event_type type, |
| 36 | enum iio_event_direction dir) |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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 |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 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 |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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, |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 56 | const struct iio_chan_spec *chan, |
| 57 | enum iio_event_type type, |
| 58 | enum iio_event_direction dir, |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 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 | */ |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 67 | switch (chan->type) { |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 68 | case IIO_VOLTAGE: |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 69 | switch (type) { |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 70 | case IIO_EV_TYPE_THRESH: |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 71 | if (dir == IIO_EV_DIR_RISING) |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 72 | st->event_en = state; |
| 73 | else |
| 74 | return -EINVAL; |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 75 | default: |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | break; |
| 79 | case IIO_ACTIVITY: |
| 80 | switch (type) { |
| 81 | case IIO_EV_TYPE_THRESH: |
| 82 | st->event_en = state; |
| 83 | break; |
| 84 | default: |
| 85 | return -EINVAL; |
| 86 | } |
Alan | aeda3b2 | 2015-04-08 20:24:43 +0100 | [diff] [blame] | 87 | break; |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 88 | case IIO_STEPS: |
| 89 | switch (type) { |
Irina Tirdea | 17a2cbc | 2015-01-11 21:10:12 +0200 | [diff] [blame] | 90 | case IIO_EV_TYPE_CHANGE: |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 91 | st->event_en = state; |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 92 | break; |
| 93 | default: |
| 94 | return -EINVAL; |
| 95 | } |
Alan | aeda3b2 | 2015-04-08 20:24:43 +0100 | [diff] [blame] | 96 | break; |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 97 | default: |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * iio_simple_dummy_read_event_value() - get value associated with event |
| 106 | * @indio_dev: device instance specific data |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 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 |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 111 | * @val: value for the event code. |
| 112 | * |
| 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. |
| 118 | */ |
| 119 | int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev, |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 120 | const struct iio_chan_spec *chan, |
| 121 | enum iio_event_type type, |
| 122 | enum iio_event_direction dir, |
Lars Svensson | 2cb1df7 | 2015-08-05 14:15:12 +0200 | [diff] [blame] | 123 | enum iio_event_info info, |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 124 | int *val, int *val2) |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 125 | { |
| 126 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 127 | |
| 128 | *val = st->event_val; |
| 129 | |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 130 | return IIO_VAL_INT; |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * iio_simple_dummy_write_event_value() - set value associate with event |
| 135 | * @indio_dev: device instance specific data |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 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 |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 140 | * @val: the value to be set. |
| 141 | */ |
| 142 | int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev, |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 143 | const struct iio_chan_spec *chan, |
| 144 | enum iio_event_type type, |
| 145 | enum iio_event_direction dir, |
Lars Svensson | 2cb1df7 | 2015-08-05 14:15:12 +0200 | [diff] [blame] | 146 | enum iio_event_info info, |
Lars-Peter Clausen | bda624b | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 147 | int val, int val2) |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 148 | { |
| 149 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 150 | |
| 151 | st->event_val = val; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 156 | static irqreturn_t iio_simple_dummy_get_timestamp(int irq, void *private) |
| 157 | { |
| 158 | struct iio_dev *indio_dev = private; |
| 159 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 160 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 161 | st->event_timestamp = iio_get_time_ns(indio_dev); |
Ioana Ciornei | 2e9fed4 | 2015-10-27 20:40:56 +0200 | [diff] [blame] | 162 | return IRQ_WAKE_THREAD; |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 163 | } |
| 164 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 165 | /** |
| 166 | * iio_simple_dummy_event_handler() - identify and pass on event |
| 167 | * @irq: irq of event line |
| 168 | * @private: pointer to device instance state. |
| 169 | * |
| 170 | * This handler is responsible for querying the device to find out what |
Masanari Iida | 99de0c2 | 2012-05-09 03:18:17 +0900 | [diff] [blame] | 171 | * event occurred and for then pushing that event towards userspace. |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 172 | * Here only one event occurs so we push that directly on with locally |
| 173 | * grabbed timestamp. |
| 174 | */ |
| 175 | static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private) |
| 176 | { |
| 177 | struct iio_dev *indio_dev = private; |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 178 | struct iio_dummy_state *st = iio_priv(indio_dev); |
Catalina Mocanu | 4b4c727 | 2014-09-18 14:55:06 -0700 | [diff] [blame] | 179 | |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 180 | dev_dbg(&indio_dev->dev, "id %x event %x\n", |
| 181 | st->regs->reg_id, st->regs->reg_data); |
| 182 | |
| 183 | switch (st->regs->reg_data) { |
| 184 | case 0: |
| 185 | iio_push_event(indio_dev, |
| 186 | IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0, |
| 187 | IIO_EV_DIR_RISING, |
| 188 | IIO_EV_TYPE_THRESH, 0, 0, 0), |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 189 | st->event_timestamp); |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 190 | break; |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 191 | case 1: |
| 192 | if (st->activity_running > st->event_val) |
| 193 | iio_push_event(indio_dev, |
| 194 | IIO_EVENT_CODE(IIO_ACTIVITY, 0, |
| 195 | IIO_MOD_RUNNING, |
| 196 | IIO_EV_DIR_RISING, |
| 197 | IIO_EV_TYPE_THRESH, |
| 198 | 0, 0, 0), |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 199 | st->event_timestamp); |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 200 | break; |
| 201 | case 2: |
| 202 | if (st->activity_walking < st->event_val) |
| 203 | iio_push_event(indio_dev, |
| 204 | IIO_EVENT_CODE(IIO_ACTIVITY, 0, |
| 205 | IIO_MOD_WALKING, |
| 206 | IIO_EV_DIR_FALLING, |
| 207 | IIO_EV_TYPE_THRESH, |
| 208 | 0, 0, 0), |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 209 | st->event_timestamp); |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 210 | break; |
| 211 | case 3: |
| 212 | iio_push_event(indio_dev, |
| 213 | IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD, |
| 214 | IIO_EV_DIR_NONE, |
Irina Tirdea | 17a2cbc | 2015-01-11 21:10:12 +0200 | [diff] [blame] | 215 | IIO_EV_TYPE_CHANGE, 0, 0, 0), |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 216 | st->event_timestamp); |
Daniel Baluta | 3e34e65 | 2014-11-10 14:45:34 +0200 | [diff] [blame] | 217 | break; |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 218 | default: |
| 219 | break; |
| 220 | } |
| 221 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 222 | return IRQ_HANDLED; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * iio_simple_dummy_events_register() - setup interrupt handling for events |
| 227 | * @indio_dev: device instance data |
| 228 | * |
| 229 | * This function requests the threaded interrupt to handle the events. |
| 230 | * Normally the irq is a hardware interrupt and the number comes |
| 231 | * from board configuration files. Here we get it from a companion |
| 232 | * module that fakes the interrupt for us. Note that module in |
| 233 | * no way forms part of this example. Just assume that events magically |
| 234 | * appear via the provided interrupt. |
| 235 | */ |
| 236 | int iio_simple_dummy_events_register(struct iio_dev *indio_dev) |
| 237 | { |
| 238 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 239 | int ret; |
| 240 | |
| 241 | /* Fire up event source - normally not present */ |
| 242 | st->event_irq = iio_dummy_evgen_get_irq(); |
| 243 | if (st->event_irq < 0) { |
| 244 | ret = st->event_irq; |
| 245 | goto error_ret; |
| 246 | } |
Daniel Baluta | 356ae94 | 2014-11-10 14:45:29 +0200 | [diff] [blame] | 247 | st->regs = iio_dummy_evgen_get_regs(st->event_irq); |
| 248 | |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 249 | ret = request_threaded_irq(st->event_irq, |
Cristina Opriceana | fd2bb31 | 2015-09-11 16:59:30 +0300 | [diff] [blame] | 250 | &iio_simple_dummy_get_timestamp, |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 251 | &iio_simple_dummy_event_handler, |
| 252 | IRQF_ONESHOT, |
| 253 | "iio_simple_event", |
| 254 | indio_dev); |
| 255 | if (ret < 0) |
| 256 | goto error_free_evgen; |
| 257 | return 0; |
| 258 | |
| 259 | error_free_evgen: |
| 260 | iio_dummy_evgen_release_irq(st->event_irq); |
| 261 | error_ret: |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove |
| 267 | * @indio_dev: device instance data |
| 268 | */ |
Vladimirs Ambrosovs | 62a90da | 2015-05-30 11:20:16 +0300 | [diff] [blame] | 269 | void iio_simple_dummy_events_unregister(struct iio_dev *indio_dev) |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 270 | { |
| 271 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 272 | |
| 273 | free_irq(st->event_irq, indio_dev); |
| 274 | /* Not part of normal driver */ |
| 275 | iio_dummy_evgen_release_irq(st->event_irq); |
Jonathan Cameron | e647700 | 2011-10-14 16:34:14 +0100 | [diff] [blame] | 276 | } |