]>
Commit | Line | Data |
---|---|---|
1637db44 JC |
1 | /* The industrial I/O core, trigger handling functions |
2 | * | |
3 | * Copyright (c) 2008 Jonathan Cameron | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published by | |
7 | * the Free Software Foundation. | |
8 | */ | |
d96d1337 JC |
9 | #include <linux/irq.h> |
10 | ||
1637db44 JC |
11 | #ifndef _IIO_TRIGGER_H_ |
12 | #define _IIO_TRIGGER_H_ | |
1637db44 | 13 | |
d96d1337 JC |
14 | struct iio_subirq { |
15 | bool enabled; | |
16 | }; | |
17 | ||
1637db44 JC |
18 | /** |
19 | * struct iio_trigger - industrial I/O trigger device | |
20 | * | |
21 | * @id: [INTERN] unique id number | |
22 | * @name: [DRIVER] unique name | |
23 | * @dev: [DRIVER] associated device (if relevant) | |
1637db44 JC |
24 | * @private_data: [DRIVER] device specific data |
25 | * @list: [INTERN] used in maintenance of global trigger list | |
26 | * @alloc_list: [DRIVER] used for driver specific trigger list | |
4c572605 | 27 | * @pollfunc_list_lock: [INTERN] protection of the polling function list |
1637db44 JC |
28 | * @pollfunc_list: [INTERN] list of functions to run on trigger. |
29 | * @control_attrs: [DRIVER] sysfs attributes relevant to trigger type | |
1637db44 | 30 | * @owner: [DRIVER] used to monitor usage count of the trigger. |
4c572605 RD |
31 | * @use_count: use count for the trigger |
32 | * @set_trigger_state: [DRIVER] switch on/off the trigger on demand | |
33 | * @try_reenable: function to reenable the trigger when the | |
34 | * use count is zero (may be NULL) | |
1637db44 JC |
35 | **/ |
36 | struct iio_trigger { | |
37 | int id; | |
38 | const char *name; | |
39 | struct device dev; | |
40 | ||
41 | void *private_data; | |
42 | struct list_head list; | |
43 | struct list_head alloc_list; | |
44 | spinlock_t pollfunc_list_lock; | |
45 | struct list_head pollfunc_list; | |
46 | const struct attribute_group *control_attrs; | |
1637db44 JC |
47 | struct module *owner; |
48 | int use_count; | |
49 | ||
50 | int (*set_trigger_state)(struct iio_trigger *trig, bool state); | |
51 | int (*try_reenable)(struct iio_trigger *trig); | |
d96d1337 JC |
52 | |
53 | struct irq_chip subirq_chip; | |
54 | int subirq_base; | |
55 | ||
56 | struct iio_subirq subirqs[CONFIG_IIO_CONSUMERS_PER_TRIGGER]; | |
57 | unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)]; | |
58 | struct mutex pool_lock; | |
1637db44 JC |
59 | }; |
60 | ||
61 | static inline struct iio_trigger *to_iio_trigger(struct device *d) | |
62 | { | |
63 | return container_of(d, struct iio_trigger, dev); | |
64 | }; | |
65 | ||
66 | static inline void iio_put_trigger(struct iio_trigger *trig) | |
67 | { | |
68 | put_device(&trig->dev); | |
69 | module_put(trig->owner); | |
70 | }; | |
71 | ||
72 | static inline void iio_get_trigger(struct iio_trigger *trig) | |
73 | { | |
74 | __module_get(trig->owner); | |
75 | get_device(&trig->dev); | |
76 | }; | |
77 | ||
78 | /** | |
79 | * iio_trigger_read_name() - sysfs access function to get the trigger name | |
4c572605 RD |
80 | * @dev: the system device |
81 | * @attr: device attributes for the device | |
82 | * @buf: output buffer to store the trigger name | |
1637db44 JC |
83 | **/ |
84 | ssize_t iio_trigger_read_name(struct device *dev, | |
85 | struct device_attribute *attr, | |
86 | char *buf); | |
87 | ||
88 | #define IIO_TRIGGER_NAME_ATTR DEVICE_ATTR(name, S_IRUGO, \ | |
89 | iio_trigger_read_name, \ | |
90 | NULL); | |
91 | ||
1637db44 JC |
92 | /** |
93 | * iio_trigger_register() - register a trigger with the IIO core | |
94 | * @trig_info: trigger to be registered | |
95 | **/ | |
96 | int iio_trigger_register(struct iio_trigger *trig_info); | |
97 | ||
98 | /** | |
99 | * iio_trigger_unregister() - unregister a trigger from the core | |
4c572605 | 100 | * @trig_info: trigger to be unregistered |
1637db44 JC |
101 | **/ |
102 | void iio_trigger_unregister(struct iio_trigger *trig_info); | |
103 | ||
104 | /** | |
105 | * iio_trigger_attach_poll_func() - add a function pair to be run on trigger | |
106 | * @trig: trigger to which the function pair are being added | |
4c572605 | 107 | * @pf: poll function pair |
1637db44 JC |
108 | **/ |
109 | int iio_trigger_attach_poll_func(struct iio_trigger *trig, | |
110 | struct iio_poll_func *pf); | |
111 | ||
112 | /** | |
113 | * iio_trigger_dettach_poll_func() - remove function pair from those to be | |
4c572605 RD |
114 | * run on trigger |
115 | * @trig: trigger from which the function is being removed | |
1637db44 JC |
116 | * @pf: poll function pair |
117 | **/ | |
118 | int iio_trigger_dettach_poll_func(struct iio_trigger *trig, | |
119 | struct iio_poll_func *pf); | |
120 | ||
121 | /** | |
4c572605 RD |
122 | * iio_trigger_poll() - called on a trigger occurring |
123 | * @trig: trigger which occurred | |
124 | * | |
1637db44 JC |
125 | * Typically called in relevant hardware interrupt handler. |
126 | **/ | |
7b2c33b1 | 127 | void iio_trigger_poll(struct iio_trigger *trig, s64 time); |
4c572605 | 128 | void iio_trigger_notify_done(struct iio_trigger *trig); |
1637db44 | 129 | |
d96d1337 JC |
130 | static inline int iio_trigger_get_irq(struct iio_trigger *trig) |
131 | { | |
132 | int ret; | |
133 | mutex_lock(&trig->pool_lock); | |
134 | ret = bitmap_find_free_region(trig->pool, | |
135 | CONFIG_IIO_CONSUMERS_PER_TRIGGER, | |
136 | ilog2(1)); | |
137 | mutex_unlock(&trig->pool_lock); | |
138 | if (ret >= 0) | |
139 | ret += trig->subirq_base; | |
140 | ||
141 | return ret; | |
142 | }; | |
143 | ||
144 | static inline void iio_trigger_put_irq(struct iio_trigger *trig, int irq) | |
145 | { | |
146 | mutex_lock(&trig->pool_lock); | |
147 | clear_bit(irq - trig->subirq_base, trig->pool); | |
148 | mutex_unlock(&trig->pool_lock); | |
149 | }; | |
150 | ||
1637db44 JC |
151 | /** |
152 | * struct iio_poll_func - poll function pair | |
153 | * | |
154 | * @list: associate this with a triggers pollfunc_list | |
155 | * @private_data: data specific to device (passed into poll func) | |
156 | * @poll_func_immediate: function in here is run first. They should be | |
157 | * extremely lightweight. Typically used for latch | |
158 | * control on sensor supporting it. | |
159 | * @poll_func_main: function in here is run after all immediates. | |
160 | * Reading from sensor etc typically involves | |
4c572605 | 161 | * scheduling from here. |
d96d1337 JC |
162 | * @h: the function that is actually run on trigger |
163 | * @thread: threaded interrupt part | |
164 | * @type: the type of interrupt (basically if oneshot) | |
165 | * @irq: the corresponding irq as allocated from the | |
166 | * trigger pool | |
167 | * @timestamp: some devices need a timestamp grabbed as soon | |
168 | * as possible after the trigger - hence handler | |
169 | * passes it via here. | |
1637db44 JC |
170 | **/ |
171 | struct iio_poll_func { | |
172 | struct list_head list; | |
173 | void *private_data; | |
174 | void (*poll_func_immediate)(struct iio_dev *indio_dev); | |
7b2c33b1 | 175 | void (*poll_func_main)(struct iio_dev *private_data, s64 time); |
1637db44 | 176 | |
d96d1337 JC |
177 | irqreturn_t (*h)(int irq, void *p); |
178 | irqreturn_t (*thread)(int irq, void *p); | |
179 | int type; | |
180 | char *name; | |
181 | int irq; | |
182 | s64 timestamp; | |
1637db44 JC |
183 | }; |
184 | ||
15744090 JC |
185 | int iio_alloc_pollfunc(struct iio_dev *indio_dev, |
186 | void (*immediate)(struct iio_dev *indio_dev), | |
7b2c33b1 | 187 | void (*main)(struct iio_dev *private_data, s64 time)); |
15744090 | 188 | |
d96d1337 JC |
189 | irqreturn_t iio_pollfunc_store_time(int irq, void *p); |
190 | ||
c3db00cc JC |
191 | /* |
192 | * Two functions for common case where all that happens is a pollfunc | |
04b70812 | 193 | * is attached and detached from a trigger |
c3db00cc JC |
194 | */ |
195 | int iio_triggered_ring_postenable(struct iio_dev *indio_dev); | |
196 | int iio_triggered_ring_predisable(struct iio_dev *indio_dev); | |
197 | ||
1637db44 | 198 | struct iio_trigger *iio_allocate_trigger(void); |
d96d1337 | 199 | struct iio_trigger *iio_allocate_trigger_named(const char *name); |
1637db44 JC |
200 | void iio_free_trigger(struct iio_trigger *trig); |
201 | ||
1637db44 | 202 | #endif /* _IIO_TRIGGER_H_ */ |