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 | |
| 15 | #include "iio.h" |
| 16 | #include "sysfs.h" |
| 17 | #include "iio_simple_dummy.h" |
| 18 | |
| 19 | /* Evgen 'fakes' interrupt events for this example */ |
| 20 | #include "iio_dummy_evgen.h" |
| 21 | |
| 22 | /** |
| 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 |
| 26 | * |
| 27 | * This function would normally query the relevant registers or a cache to |
| 28 | * discover if the event generation is enabled on the device. |
| 29 | */ |
| 30 | int iio_simple_dummy_read_event_config(struct iio_dev *indio_dev, |
| 31 | u64 event_code) |
| 32 | { |
| 33 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 34 | |
| 35 | return st->event_en; |
| 36 | } |
| 37 | |
| 38 | /** |
| 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. |
| 43 | * |
| 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 |
| 46 | * value. |
| 47 | */ |
| 48 | int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev, |
| 49 | u64 event_code, |
| 50 | int state) |
| 51 | { |
| 52 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 53 | |
| 54 | /* |
| 55 | * Deliberately over the top code splitting to illustrate |
| 56 | * how this is done when multiple events exist. |
| 57 | */ |
| 58 | switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) { |
| 59 | case IIO_VOLTAGE: |
| 60 | switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) { |
| 61 | case IIO_EV_TYPE_THRESH: |
| 62 | if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == |
| 63 | IIO_EV_DIR_RISING) |
| 64 | st->event_en = state; |
| 65 | else |
| 66 | return -EINVAL; |
| 67 | break; |
| 68 | default: |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | default: |
| 72 | return -EINVAL; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /** |
| 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. |
| 83 | * |
| 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. |
| 89 | */ |
| 90 | int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev, |
| 91 | u64 event_code, |
| 92 | int *val) |
| 93 | { |
| 94 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 95 | |
| 96 | *val = st->event_val; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /** |
| 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. |
| 106 | */ |
| 107 | int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev, |
| 108 | u64 event_code, |
| 109 | int val) |
| 110 | { |
| 111 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 112 | |
| 113 | st->event_val = val; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * iio_simple_dummy_event_handler() - identify and pass on event |
| 120 | * @irq: irq of event line |
| 121 | * @private: pointer to device instance state. |
| 122 | * |
| 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 |
| 126 | * grabbed timestamp. |
| 127 | */ |
| 128 | static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private) |
| 129 | { |
| 130 | struct iio_dev *indio_dev = private; |
| 131 | iio_push_event(indio_dev, |
| 132 | IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0, |
| 133 | IIO_EV_DIR_RISING, |
| 134 | IIO_EV_TYPE_THRESH, 0, 0, 0), |
| 135 | iio_get_time_ns()); |
| 136 | return IRQ_HANDLED; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * iio_simple_dummy_events_register() - setup interrupt handling for events |
| 141 | * @indio_dev: device instance data |
| 142 | * |
| 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. |
| 149 | */ |
| 150 | int iio_simple_dummy_events_register(struct iio_dev *indio_dev) |
| 151 | { |
| 152 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 153 | int ret; |
| 154 | |
| 155 | /* Fire up event source - normally not present */ |
| 156 | st->event_irq = iio_dummy_evgen_get_irq(); |
| 157 | if (st->event_irq < 0) { |
| 158 | ret = st->event_irq; |
| 159 | goto error_ret; |
| 160 | } |
| 161 | ret = request_threaded_irq(st->event_irq, |
| 162 | NULL, |
| 163 | &iio_simple_dummy_event_handler, |
| 164 | IRQF_ONESHOT, |
| 165 | "iio_simple_event", |
| 166 | indio_dev); |
| 167 | if (ret < 0) |
| 168 | goto error_free_evgen; |
| 169 | return 0; |
| 170 | |
| 171 | error_free_evgen: |
| 172 | iio_dummy_evgen_release_irq(st->event_irq); |
| 173 | error_ret: |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove |
| 179 | * @indio_dev: device instance data |
| 180 | */ |
| 181 | int iio_simple_dummy_events_unregister(struct iio_dev *indio_dev) |
| 182 | { |
| 183 | struct iio_dummy_state *st = iio_priv(indio_dev); |
| 184 | |
| 185 | free_irq(st->event_irq, indio_dev); |
| 186 | /* Not part of normal driver */ |
| 187 | iio_dummy_evgen_release_irq(st->event_irq); |
| 188 | |
| 189 | return 0; |
| 190 | } |