Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1637db44 JC |
2 | /* The industrial I/O core, trigger handling functions |
3 | * | |
4 | * Copyright (c) 2008 Jonathan Cameron | |
1637db44 JC |
5 | */ |
6 | ||
7 | #include <linux/kernel.h> | |
1637db44 JC |
8 | #include <linux/idr.h> |
9 | #include <linux/err.h> | |
10 | #include <linux/device.h> | |
11 | #include <linux/interrupt.h> | |
12 | #include <linux/list.h> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
1637db44 | 14 | |
06458e27 | 15 | #include <linux/iio/iio.h> |
6eaf9f6a | 16 | #include <linux/iio/iio-opaque.h> |
06458e27 | 17 | #include <linux/iio/trigger.h> |
df9c1c42 | 18 | #include "iio_core.h" |
6aea1c36 | 19 | #include "iio_core_trigger.h" |
06458e27 | 20 | #include <linux/iio/trigger_consumer.h> |
1637db44 JC |
21 | |
22 | /* RFC - Question of approach | |
23 | * Make the common case (single sensor single trigger) | |
24 | * simple by starting trigger capture from when first sensors | |
25 | * is added. | |
26 | * | |
27 | * Complex simultaneous start requires use of 'hold' functionality | |
28 | * of the trigger. (not implemented) | |
29 | * | |
30 | * Any other suggestions? | |
31 | */ | |
32 | ||
47c24fdd | 33 | static DEFINE_IDA(iio_trigger_ida); |
1637db44 JC |
34 | |
35 | /* Single list of all available triggers */ | |
36 | static LIST_HEAD(iio_trigger_list); | |
37 | static DEFINE_MUTEX(iio_trigger_list_lock); | |
38 | ||
59c85e82 JC |
39 | /** |
40 | * iio_trigger_read_name() - retrieve useful identifying name | |
8e563b0d CO |
41 | * @dev: device associated with the iio_trigger |
42 | * @attr: pointer to the device_attribute structure that is | |
43 | * being processed | |
44 | * @buf: buffer to print the name into | |
45 | * | |
46 | * Return: a negative number on failure or the number of written | |
47 | * characters on success. | |
48 | */ | |
59c85e82 JC |
49 | static ssize_t iio_trigger_read_name(struct device *dev, |
50 | struct device_attribute *attr, | |
51 | char *buf) | |
52 | { | |
971ff1db | 53 | struct iio_trigger *trig = to_iio_trigger(dev); |
83ca56b6 | 54 | return sysfs_emit(buf, "%s\n", trig->name); |
59c85e82 JC |
55 | } |
56 | ||
57 | static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL); | |
58 | ||
6d459aa0 LPC |
59 | static struct attribute *iio_trig_dev_attrs[] = { |
60 | &dev_attr_name.attr, | |
61 | NULL, | |
62 | }; | |
f59c2576 | 63 | ATTRIBUTE_GROUPS(iio_trig_dev); |
1637db44 | 64 | |
3b8e73ec CDL |
65 | static struct iio_trigger *__iio_trigger_find_by_name(const char *name); |
66 | ||
035c70ae JC |
67 | int __iio_trigger_register(struct iio_trigger *trig_info, |
68 | struct module *this_mod) | |
1637db44 JC |
69 | { |
70 | int ret; | |
71 | ||
035c70ae JC |
72 | trig_info->owner = this_mod; |
73 | ||
47c24fdd | 74 | trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL); |
92825ff9 HK |
75 | if (trig_info->id < 0) |
76 | return trig_info->id; | |
77 | ||
1637db44 | 78 | /* Set the name used for the sysfs directory etc */ |
ebb9493c | 79 | dev_set_name(&trig_info->dev, "trigger%d", trig_info->id); |
1637db44 JC |
80 | |
81 | ret = device_add(&trig_info->dev); | |
82 | if (ret) | |
83 | goto error_unregister_id; | |
84 | ||
1637db44 JC |
85 | /* Add to list of available triggers held by the IIO core */ |
86 | mutex_lock(&iio_trigger_list_lock); | |
3b8e73ec CDL |
87 | if (__iio_trigger_find_by_name(trig_info->name)) { |
88 | pr_err("Duplicate trigger name '%s'\n", trig_info->name); | |
89 | ret = -EEXIST; | |
90 | goto error_device_del; | |
91 | } | |
1637db44 JC |
92 | list_add_tail(&trig_info->list, &iio_trigger_list); |
93 | mutex_unlock(&iio_trigger_list_lock); | |
94 | ||
95 | return 0; | |
96 | ||
3b8e73ec CDL |
97 | error_device_del: |
98 | mutex_unlock(&iio_trigger_list_lock); | |
99 | device_del(&trig_info->dev); | |
1637db44 | 100 | error_unregister_id: |
47c24fdd | 101 | ida_simple_remove(&iio_trigger_ida, trig_info->id); |
1637db44 JC |
102 | return ret; |
103 | } | |
035c70ae | 104 | EXPORT_SYMBOL(__iio_trigger_register); |
1637db44 JC |
105 | |
106 | void iio_trigger_unregister(struct iio_trigger *trig_info) | |
107 | { | |
1637db44 | 108 | mutex_lock(&iio_trigger_list_lock); |
582e5489 | 109 | list_del(&trig_info->list); |
1637db44 JC |
110 | mutex_unlock(&iio_trigger_list_lock); |
111 | ||
47c24fdd | 112 | ida_simple_remove(&iio_trigger_ida, trig_info->id); |
1637db44 | 113 | /* Possible issue in here */ |
8bade406 | 114 | device_del(&trig_info->dev); |
1637db44 JC |
115 | } |
116 | EXPORT_SYMBOL(iio_trigger_unregister); | |
117 | ||
c8cdf708 MR |
118 | int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig) |
119 | { | |
3028e0c2 JC |
120 | struct iio_dev_opaque *iio_dev_opaque; |
121 | ||
c8cdf708 MR |
122 | if (!indio_dev || !trig) |
123 | return -EINVAL; | |
124 | ||
3028e0c2 | 125 | iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
c8cdf708 | 126 | mutex_lock(&indio_dev->mlock); |
3028e0c2 | 127 | WARN_ON(iio_dev_opaque->trig_readonly); |
c8cdf708 MR |
128 | |
129 | indio_dev->trig = iio_trigger_get(trig); | |
3028e0c2 | 130 | iio_dev_opaque->trig_readonly = true; |
c8cdf708 MR |
131 | mutex_unlock(&indio_dev->mlock); |
132 | ||
133 | return 0; | |
134 | } | |
135 | EXPORT_SYMBOL(iio_trigger_set_immutable); | |
136 | ||
3b8e73ec CDL |
137 | /* Search for trigger by name, assuming iio_trigger_list_lock held */ |
138 | static struct iio_trigger *__iio_trigger_find_by_name(const char *name) | |
139 | { | |
140 | struct iio_trigger *iter; | |
141 | ||
142 | list_for_each_entry(iter, &iio_trigger_list, list) | |
143 | if (!strcmp(iter->name, name)) | |
144 | return iter; | |
145 | ||
146 | return NULL; | |
147 | } | |
148 | ||
d5d24bcc | 149 | static struct iio_trigger *iio_trigger_acquire_by_name(const char *name) |
1637db44 | 150 | { |
f6517f22 | 151 | struct iio_trigger *trig = NULL, *iter; |
7c327857 | 152 | |
1637db44 | 153 | mutex_lock(&iio_trigger_list_lock); |
f6517f22 JC |
154 | list_for_each_entry(iter, &iio_trigger_list, list) |
155 | if (sysfs_streq(iter->name, name)) { | |
156 | trig = iter; | |
d5d24bcc | 157 | iio_trigger_get(trig); |
1637db44 JC |
158 | break; |
159 | } | |
1637db44 JC |
160 | mutex_unlock(&iio_trigger_list_lock); |
161 | ||
f6517f22 | 162 | return trig; |
5f87404d | 163 | } |
1637db44 | 164 | |
9020ef65 JC |
165 | static void iio_reenable_work_fn(struct work_struct *work) |
166 | { | |
167 | struct iio_trigger *trig = container_of(work, struct iio_trigger, | |
168 | reenable_work); | |
169 | ||
170 | /* | |
171 | * This 'might' occur after the trigger state is set to disabled - | |
172 | * in that case the driver should skip reenabling. | |
173 | */ | |
174 | trig->ops->reenable(trig); | |
175 | } | |
176 | ||
177 | /* | |
178 | * In general, reenable callbacks may need to sleep and this path is | |
179 | * not performance sensitive, so just queue up a work item | |
180 | * to reneable the trigger for us. | |
181 | * | |
182 | * Races that can cause this. | |
183 | * 1) A handler occurs entirely in interrupt context so the counter | |
184 | * the final decrement is still in this interrupt. | |
185 | * 2) The trigger has been removed, but one last interrupt gets through. | |
186 | * | |
187 | * For (1) we must call reenable, but not in atomic context. | |
188 | * For (2) it should be safe to call reenanble, if drivers never blindly | |
189 | * reenable after state is off. | |
190 | */ | |
191 | static void iio_trigger_notify_done_atomic(struct iio_trigger *trig) | |
192 | { | |
193 | if (atomic_dec_and_test(&trig->use_count) && trig->ops && | |
194 | trig->ops->reenable) | |
195 | schedule_work(&trig->reenable_work); | |
196 | } | |
197 | ||
398fd22b | 198 | void iio_trigger_poll(struct iio_trigger *trig) |
1637db44 | 199 | { |
d96d1337 | 200 | int i; |
a1a8e1dc LPC |
201 | |
202 | if (!atomic_read(&trig->use_count)) { | |
203 | atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER); | |
204 | ||
205 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { | |
206 | if (trig->subirqs[i].enabled) | |
d96d1337 | 207 | generic_handle_irq(trig->subirq_base + i); |
a1a8e1dc | 208 | else |
9020ef65 | 209 | iio_trigger_notify_done_atomic(trig); |
a1a8e1dc LPC |
210 | } |
211 | } | |
1637db44 JC |
212 | } |
213 | EXPORT_SYMBOL(iio_trigger_poll); | |
8384d957 JC |
214 | |
215 | irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private) | |
216 | { | |
398fd22b | 217 | iio_trigger_poll(private); |
8384d957 JC |
218 | return IRQ_HANDLED; |
219 | } | |
220 | EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll); | |
1637db44 | 221 | |
398fd22b | 222 | void iio_trigger_poll_chained(struct iio_trigger *trig) |
1f785681 JC |
223 | { |
224 | int i; | |
a1a8e1dc LPC |
225 | |
226 | if (!atomic_read(&trig->use_count)) { | |
227 | atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER); | |
228 | ||
229 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { | |
230 | if (trig->subirqs[i].enabled) | |
1f785681 | 231 | handle_nested_irq(trig->subirq_base + i); |
a1a8e1dc LPC |
232 | else |
233 | iio_trigger_notify_done(trig); | |
234 | } | |
235 | } | |
1f785681 JC |
236 | } |
237 | EXPORT_SYMBOL(iio_trigger_poll_chained); | |
238 | ||
1637db44 JC |
239 | void iio_trigger_notify_done(struct iio_trigger *trig) |
240 | { | |
04581681 | 241 | if (atomic_dec_and_test(&trig->use_count) && trig->ops && |
eca8523a JC |
242 | trig->ops->reenable) |
243 | trig->ops->reenable(trig); | |
1637db44 JC |
244 | } |
245 | EXPORT_SYMBOL(iio_trigger_notify_done); | |
246 | ||
1637db44 | 247 | /* Trigger Consumer related functions */ |
208b813c JC |
248 | static int iio_trigger_get_irq(struct iio_trigger *trig) |
249 | { | |
250 | int ret; | |
af3bac44 | 251 | |
208b813c JC |
252 | mutex_lock(&trig->pool_lock); |
253 | ret = bitmap_find_free_region(trig->pool, | |
254 | CONFIG_IIO_CONSUMERS_PER_TRIGGER, | |
255 | ilog2(1)); | |
256 | mutex_unlock(&trig->pool_lock); | |
257 | if (ret >= 0) | |
258 | ret += trig->subirq_base; | |
259 | ||
260 | return ret; | |
261 | } | |
262 | ||
263 | static void iio_trigger_put_irq(struct iio_trigger *trig, int irq) | |
264 | { | |
265 | mutex_lock(&trig->pool_lock); | |
266 | clear_bit(irq - trig->subirq_base, trig->pool); | |
267 | mutex_unlock(&trig->pool_lock); | |
268 | } | |
1637db44 JC |
269 | |
270 | /* Complexity in here. With certain triggers (datardy) an acknowledgement | |
271 | * may be needed if the pollfuncs do not include the data read for the | |
272 | * triggering device. | |
273 | * This is not currently handled. Alternative of not enabling trigger unless | |
274 | * the relevant function is in there may be the best option. | |
275 | */ | |
860c9c54 | 276 | /* Worth protecting against double additions? */ |
f11d59d8 LPC |
277 | int iio_trigger_attach_poll_func(struct iio_trigger *trig, |
278 | struct iio_poll_func *pf) | |
1637db44 | 279 | { |
6eaf9f6a | 280 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev); |
af3bac44 AS |
281 | bool notinuse = |
282 | bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER); | |
1637db44 | 283 | int ret = 0; |
51c060a0 | 284 | |
860c9c54 | 285 | /* Prevent the module from being removed whilst attached to a trigger */ |
6eaf9f6a | 286 | __module_get(iio_dev_opaque->driver_module); |
99543823 CDL |
287 | |
288 | /* Get irq number */ | |
51c060a0 | 289 | pf->irq = iio_trigger_get_irq(trig); |
be35d281 MO |
290 | if (pf->irq < 0) { |
291 | pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n", | |
292 | trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER); | |
99543823 | 293 | goto out_put_module; |
be35d281 | 294 | } |
99543823 CDL |
295 | |
296 | /* Request irq */ | |
51c060a0 JC |
297 | ret = request_threaded_irq(pf->irq, pf->h, pf->thread, |
298 | pf->type, pf->name, | |
299 | pf); | |
99543823 CDL |
300 | if (ret < 0) |
301 | goto out_put_irq; | |
5dd72ecb | 302 | |
99543823 | 303 | /* Enable trigger in driver */ |
04581681 | 304 | if (trig->ops && trig->ops->set_trigger_state && notinuse) { |
d29f73db | 305 | ret = trig->ops->set_trigger_state(trig, true); |
5dd72ecb | 306 | if (ret < 0) |
99543823 | 307 | goto out_free_irq; |
5dd72ecb | 308 | } |
d96d1337 | 309 | |
702a7b8e LW |
310 | /* |
311 | * Check if we just registered to our own trigger: we determine that | |
312 | * this is the case if the IIO device and the trigger device share the | |
313 | * same parent device. | |
314 | */ | |
315 | if (pf->indio_dev->dev.parent == trig->dev.parent) | |
316 | trig->attached_own_device = true; | |
317 | ||
1637db44 | 318 | return ret; |
99543823 CDL |
319 | |
320 | out_free_irq: | |
321 | free_irq(pf->irq, pf); | |
322 | out_put_irq: | |
323 | iio_trigger_put_irq(trig, pf->irq); | |
324 | out_put_module: | |
6eaf9f6a | 325 | module_put(iio_dev_opaque->driver_module); |
99543823 | 326 | return ret; |
1637db44 | 327 | } |
1637db44 | 328 | |
f11d59d8 LPC |
329 | int iio_trigger_detach_poll_func(struct iio_trigger *trig, |
330 | struct iio_poll_func *pf) | |
1637db44 | 331 | { |
6eaf9f6a | 332 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev); |
af3bac44 AS |
333 | bool no_other_users = |
334 | bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1; | |
51c060a0 | 335 | int ret = 0; |
af3bac44 | 336 | |
04581681 | 337 | if (trig->ops && trig->ops->set_trigger_state && no_other_users) { |
d29f73db | 338 | ret = trig->ops->set_trigger_state(trig, false); |
51c060a0 | 339 | if (ret) |
92825ff9 | 340 | return ret; |
1637db44 | 341 | } |
702a7b8e LW |
342 | if (pf->indio_dev->dev.parent == trig->dev.parent) |
343 | trig->attached_own_device = false; | |
51c060a0 JC |
344 | iio_trigger_put_irq(trig, pf->irq); |
345 | free_irq(pf->irq, pf); | |
6eaf9f6a | 346 | module_put(iio_dev_opaque->driver_module); |
1637db44 | 347 | |
1637db44 JC |
348 | return ret; |
349 | } | |
1637db44 | 350 | |
d96d1337 JC |
351 | irqreturn_t iio_pollfunc_store_time(int irq, void *p) |
352 | { | |
353 | struct iio_poll_func *pf = p; | |
af3bac44 | 354 | |
bc2b7dab | 355 | pf->timestamp = iio_get_time_ns(pf->indio_dev); |
d96d1337 JC |
356 | return IRQ_WAKE_THREAD; |
357 | } | |
358 | EXPORT_SYMBOL(iio_pollfunc_store_time); | |
359 | ||
21b185f8 JC |
360 | struct iio_poll_func |
361 | *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p), | |
362 | irqreturn_t (*thread)(int irq, void *p), | |
363 | int type, | |
e65bc6ac | 364 | struct iio_dev *indio_dev, |
21b185f8 JC |
365 | const char *fmt, |
366 | ...) | |
367 | { | |
368 | va_list vargs; | |
369 | struct iio_poll_func *pf; | |
370 | ||
371 | pf = kmalloc(sizeof *pf, GFP_KERNEL); | |
372 | if (pf == NULL) | |
373 | return NULL; | |
374 | va_start(vargs, fmt); | |
375 | pf->name = kvasprintf(GFP_KERNEL, fmt, vargs); | |
376 | va_end(vargs); | |
377 | if (pf->name == NULL) { | |
378 | kfree(pf); | |
379 | return NULL; | |
380 | } | |
381 | pf->h = h; | |
382 | pf->thread = thread; | |
383 | pf->type = type; | |
e65bc6ac | 384 | pf->indio_dev = indio_dev; |
21b185f8 JC |
385 | |
386 | return pf; | |
387 | } | |
388 | EXPORT_SYMBOL_GPL(iio_alloc_pollfunc); | |
389 | ||
390 | void iio_dealloc_pollfunc(struct iio_poll_func *pf) | |
391 | { | |
392 | kfree(pf->name); | |
393 | kfree(pf); | |
394 | } | |
395 | EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc); | |
396 | ||
1637db44 | 397 | /** |
860c9c54 | 398 | * iio_trigger_read_current() - trigger consumer sysfs query current trigger |
8e563b0d CO |
399 | * @dev: device associated with an industrial I/O device |
400 | * @attr: pointer to the device_attribute structure that | |
401 | * is being processed | |
402 | * @buf: buffer where the current trigger name will be printed into | |
1637db44 JC |
403 | * |
404 | * For trigger consumers the current_trigger interface allows the trigger | |
405 | * used by the device to be queried. | |
8e563b0d CO |
406 | * |
407 | * Return: a negative number on failure, the number of characters written | |
408 | * on success or 0 if no trigger is available | |
409 | */ | |
1637db44 JC |
410 | static ssize_t iio_trigger_read_current(struct device *dev, |
411 | struct device_attribute *attr, | |
412 | char *buf) | |
413 | { | |
e53f5ac5 | 414 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
cb6c89a0 | 415 | |
f8c6f4e9 | 416 | if (indio_dev->trig) |
83ca56b6 | 417 | return sysfs_emit(buf, "%s\n", indio_dev->trig->name); |
cb6c89a0 | 418 | return 0; |
1637db44 JC |
419 | } |
420 | ||
421 | /** | |
860c9c54 | 422 | * iio_trigger_write_current() - trigger consumer sysfs set current trigger |
8e563b0d CO |
423 | * @dev: device associated with an industrial I/O device |
424 | * @attr: device attribute that is being processed | |
425 | * @buf: string buffer that holds the name of the trigger | |
426 | * @len: length of the trigger name held by buf | |
1637db44 JC |
427 | * |
428 | * For trigger consumers the current_trigger interface allows the trigger | |
17666ef3 | 429 | * used for this device to be specified at run time based on the trigger's |
1637db44 | 430 | * name. |
8e563b0d CO |
431 | * |
432 | * Return: negative error code on failure or length of the buffer | |
433 | * on success | |
434 | */ | |
1637db44 JC |
435 | static ssize_t iio_trigger_write_current(struct device *dev, |
436 | struct device_attribute *attr, | |
437 | const char *buf, | |
438 | size_t len) | |
439 | { | |
e53f5ac5 | 440 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
3028e0c2 | 441 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
f8c6f4e9 | 442 | struct iio_trigger *oldtrig = indio_dev->trig; |
43a4360e MH |
443 | struct iio_trigger *trig; |
444 | int ret; | |
445 | ||
f8c6f4e9 | 446 | mutex_lock(&indio_dev->mlock); |
51570c9d | 447 | if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) { |
f8c6f4e9 | 448 | mutex_unlock(&indio_dev->mlock); |
1637db44 JC |
449 | return -EBUSY; |
450 | } | |
3028e0c2 | 451 | if (iio_dev_opaque->trig_readonly) { |
c8cdf708 MR |
452 | mutex_unlock(&indio_dev->mlock); |
453 | return -EPERM; | |
454 | } | |
f8c6f4e9 | 455 | mutex_unlock(&indio_dev->mlock); |
1637db44 | 456 | |
d5d24bcc AS |
457 | trig = iio_trigger_acquire_by_name(buf); |
458 | if (oldtrig == trig) { | |
459 | ret = len; | |
460 | goto out_trigger_put; | |
461 | } | |
43a4360e | 462 | |
f8c6f4e9 JC |
463 | if (trig && indio_dev->info->validate_trigger) { |
464 | ret = indio_dev->info->validate_trigger(indio_dev, trig); | |
43a4360e | 465 | if (ret) |
d5d24bcc | 466 | goto out_trigger_put; |
43a4360e MH |
467 | } |
468 | ||
04581681 | 469 | if (trig && trig->ops && trig->ops->validate_device) { |
f8c6f4e9 | 470 | ret = trig->ops->validate_device(trig, indio_dev); |
43a4360e | 471 | if (ret) |
d5d24bcc | 472 | goto out_trigger_put; |
43a4360e MH |
473 | } |
474 | ||
f8c6f4e9 | 475 | indio_dev->trig = trig; |
43a4360e | 476 | |
735ad074 VB |
477 | if (oldtrig) { |
478 | if (indio_dev->modes & INDIO_EVENT_TRIGGERED) | |
479 | iio_trigger_detach_poll_func(oldtrig, | |
480 | indio_dev->pollfunc_event); | |
7cbb7537 | 481 | iio_trigger_put(oldtrig); |
735ad074 VB |
482 | } |
483 | if (indio_dev->trig) { | |
735ad074 VB |
484 | if (indio_dev->modes & INDIO_EVENT_TRIGGERED) |
485 | iio_trigger_attach_poll_func(indio_dev->trig, | |
486 | indio_dev->pollfunc_event); | |
487 | } | |
1637db44 JC |
488 | |
489 | return len; | |
d5d24bcc AS |
490 | |
491 | out_trigger_put: | |
4eecbe81 MN |
492 | if (trig) |
493 | iio_trigger_put(trig); | |
d5d24bcc | 494 | return ret; |
1637db44 JC |
495 | } |
496 | ||
74112d3f GKH |
497 | static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR, |
498 | iio_trigger_read_current, | |
499 | iio_trigger_write_current); | |
1637db44 JC |
500 | |
501 | static struct attribute *iio_trigger_consumer_attrs[] = { | |
502 | &dev_attr_current_trigger.attr, | |
503 | NULL, | |
504 | }; | |
505 | ||
506 | static const struct attribute_group iio_trigger_consumer_attr_group = { | |
507 | .name = "trigger", | |
508 | .attrs = iio_trigger_consumer_attrs, | |
509 | }; | |
510 | ||
511 | static void iio_trig_release(struct device *device) | |
512 | { | |
513 | struct iio_trigger *trig = to_iio_trigger(device); | |
d96d1337 JC |
514 | int i; |
515 | ||
516 | if (trig->subirq_base) { | |
517 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { | |
518 | irq_modify_status(trig->subirq_base + i, | |
519 | IRQ_NOAUTOEN, | |
520 | IRQ_NOREQUEST | IRQ_NOPROBE); | |
521 | irq_set_chip(trig->subirq_base + i, | |
522 | NULL); | |
523 | irq_set_handler(trig->subirq_base + i, | |
524 | NULL); | |
525 | } | |
526 | ||
527 | irq_free_descs(trig->subirq_base, | |
528 | CONFIG_IIO_CONSUMERS_PER_TRIGGER); | |
529 | } | |
59c85e82 | 530 | kfree(trig->name); |
1637db44 | 531 | kfree(trig); |
1637db44 JC |
532 | } |
533 | ||
3bdafc49 | 534 | static const struct device_type iio_trig_type = { |
1637db44 | 535 | .release = iio_trig_release, |
f59c2576 | 536 | .groups = iio_trig_dev_groups, |
1637db44 JC |
537 | }; |
538 | ||
d96d1337 JC |
539 | static void iio_trig_subirqmask(struct irq_data *d) |
540 | { | |
541 | struct irq_chip *chip = irq_data_get_irq_chip(d); | |
af3bac44 AS |
542 | struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip); |
543 | ||
d96d1337 JC |
544 | trig->subirqs[d->irq - trig->subirq_base].enabled = false; |
545 | } | |
546 | ||
547 | static void iio_trig_subirqunmask(struct irq_data *d) | |
548 | { | |
549 | struct irq_chip *chip = irq_data_get_irq_chip(d); | |
af3bac44 AS |
550 | struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip); |
551 | ||
d96d1337 JC |
552 | trig->subirqs[d->irq - trig->subirq_base].enabled = true; |
553 | } | |
554 | ||
995071d3 GG |
555 | static __printf(2, 0) |
556 | struct iio_trigger *viio_trigger_alloc(struct device *parent, | |
557 | const char *fmt, | |
558 | va_list vargs) | |
1637db44 JC |
559 | { |
560 | struct iio_trigger *trig; | |
2c99f1a0 DC |
561 | int i; |
562 | ||
1637db44 | 563 | trig = kzalloc(sizeof *trig, GFP_KERNEL); |
2c99f1a0 DC |
564 | if (!trig) |
565 | return NULL; | |
d536321d | 566 | |
995071d3 | 567 | trig->dev.parent = parent; |
2c99f1a0 DC |
568 | trig->dev.type = &iio_trig_type; |
569 | trig->dev.bus = &iio_bus_type; | |
570 | device_initialize(&trig->dev); | |
9020ef65 | 571 | INIT_WORK(&trig->reenable_work, iio_reenable_work_fn); |
2c99f1a0 DC |
572 | |
573 | mutex_init(&trig->pool_lock); | |
574 | trig->subirq_base = irq_alloc_descs(-1, 0, | |
575 | CONFIG_IIO_CONSUMERS_PER_TRIGGER, | |
576 | 0); | |
577 | if (trig->subirq_base < 0) | |
578 | goto free_trig; | |
579 | ||
580 | trig->name = kvasprintf(GFP_KERNEL, fmt, vargs); | |
581 | if (trig->name == NULL) | |
582 | goto free_descs; | |
583 | ||
584 | trig->subirq_chip.name = trig->name; | |
585 | trig->subirq_chip.irq_mask = &iio_trig_subirqmask; | |
586 | trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask; | |
587 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { | |
588 | irq_set_chip(trig->subirq_base + i, &trig->subirq_chip); | |
589 | irq_set_handler(trig->subirq_base + i, &handle_simple_irq); | |
590 | irq_modify_status(trig->subirq_base + i, | |
591 | IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE); | |
1637db44 | 592 | } |
d536321d JA |
593 | |
594 | return trig; | |
2c99f1a0 DC |
595 | |
596 | free_descs: | |
597 | irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER); | |
598 | free_trig: | |
599 | kfree(trig); | |
600 | return NULL; | |
d536321d JA |
601 | } |
602 | ||
995071d3 GG |
603 | /** |
604 | * iio_trigger_alloc - Allocate a trigger | |
605 | * @parent: Device to allocate iio_trigger for | |
606 | * @fmt: trigger name format. If it includes format | |
607 | * specifiers, the additional arguments following | |
608 | * format are formatted and inserted in the resulting | |
609 | * string replacing their respective specifiers. | |
610 | * RETURNS: | |
611 | * Pointer to allocated iio_trigger on success, NULL on failure. | |
612 | */ | |
613 | struct iio_trigger *iio_trigger_alloc(struct device *parent, const char *fmt, ...) | |
d536321d JA |
614 | { |
615 | struct iio_trigger *trig; | |
616 | va_list vargs; | |
617 | ||
618 | va_start(vargs, fmt); | |
995071d3 | 619 | trig = viio_trigger_alloc(parent, fmt, vargs); |
d536321d JA |
620 | va_end(vargs); |
621 | ||
1637db44 JC |
622 | return trig; |
623 | } | |
7cbb7537 | 624 | EXPORT_SYMBOL(iio_trigger_alloc); |
1637db44 | 625 | |
7cbb7537 | 626 | void iio_trigger_free(struct iio_trigger *trig) |
1637db44 JC |
627 | { |
628 | if (trig) | |
629 | put_device(&trig->dev); | |
630 | } | |
7cbb7537 | 631 | EXPORT_SYMBOL(iio_trigger_free); |
1637db44 | 632 | |
d536321d JA |
633 | static void devm_iio_trigger_release(struct device *dev, void *res) |
634 | { | |
635 | iio_trigger_free(*(struct iio_trigger **)res); | |
636 | } | |
637 | ||
a7e57dce SK |
638 | /** |
639 | * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc() | |
995071d3 GG |
640 | * Managed iio_trigger_alloc. iio_trigger allocated with this function is |
641 | * automatically freed on driver detach. | |
642 | * @parent: Device to allocate iio_trigger for | |
a7e57dce SK |
643 | * @fmt: trigger name format. If it includes format |
644 | * specifiers, the additional arguments following | |
645 | * format are formatted and inserted in the resulting | |
646 | * string replacing their respective specifiers. | |
647 | * | |
a7e57dce | 648 | * |
a7e57dce SK |
649 | * RETURNS: |
650 | * Pointer to allocated iio_trigger on success, NULL on failure. | |
651 | */ | |
995071d3 | 652 | struct iio_trigger *devm_iio_trigger_alloc(struct device *parent, const char *fmt, ...) |
d536321d JA |
653 | { |
654 | struct iio_trigger **ptr, *trig; | |
655 | va_list vargs; | |
656 | ||
657 | ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr), | |
658 | GFP_KERNEL); | |
659 | if (!ptr) | |
660 | return NULL; | |
661 | ||
662 | /* use raw alloc_dr for kmalloc caller tracing */ | |
663 | va_start(vargs, fmt); | |
995071d3 | 664 | trig = viio_trigger_alloc(parent, fmt, vargs); |
d536321d JA |
665 | va_end(vargs); |
666 | if (trig) { | |
667 | *ptr = trig; | |
995071d3 | 668 | devres_add(parent, ptr); |
d536321d JA |
669 | } else { |
670 | devres_free(ptr); | |
671 | } | |
672 | ||
673 | return trig; | |
674 | } | |
675 | EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc); | |
676 | ||
171a70af | 677 | static void devm_iio_trigger_unreg(void *trigger_info) |
9083325f | 678 | { |
171a70af | 679 | iio_trigger_unregister(trigger_info); |
9083325f GB |
680 | } |
681 | ||
682 | /** | |
035c70ae | 683 | * __devm_iio_trigger_register - Resource-managed iio_trigger_register() |
9083325f GB |
684 | * @dev: device this trigger was allocated for |
685 | * @trig_info: trigger to register | |
035c70ae | 686 | * @this_mod: module registering the trigger |
9083325f GB |
687 | * |
688 | * Managed iio_trigger_register(). The IIO trigger registered with this | |
689 | * function is automatically unregistered on driver detach. This function | |
690 | * calls iio_trigger_register() internally. Refer to that function for more | |
691 | * information. | |
692 | * | |
9083325f GB |
693 | * RETURNS: |
694 | * 0 on success, negative error number on failure. | |
695 | */ | |
035c70ae JC |
696 | int __devm_iio_trigger_register(struct device *dev, |
697 | struct iio_trigger *trig_info, | |
698 | struct module *this_mod) | |
9083325f | 699 | { |
9083325f GB |
700 | int ret; |
701 | ||
035c70ae | 702 | ret = __iio_trigger_register(trig_info, this_mod); |
171a70af YY |
703 | if (ret) |
704 | return ret; | |
9083325f | 705 | |
171a70af | 706 | return devm_add_action_or_reset(dev, devm_iio_trigger_unreg, trig_info); |
9083325f | 707 | } |
035c70ae | 708 | EXPORT_SYMBOL_GPL(__devm_iio_trigger_register); |
9083325f | 709 | |
702a7b8e LW |
710 | bool iio_trigger_using_own(struct iio_dev *indio_dev) |
711 | { | |
712 | return indio_dev->trig->attached_own_device; | |
713 | } | |
714 | EXPORT_SYMBOL(iio_trigger_using_own); | |
715 | ||
43ece27e LPC |
716 | /** |
717 | * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to | |
718 | * the same device | |
719 | * @trig: The IIO trigger to check | |
720 | * @indio_dev: the IIO device to check | |
721 | * | |
722 | * This function can be used as the validate_device callback for triggers that | |
723 | * can only be attached to their own device. | |
724 | * | |
725 | * Return: 0 if both the trigger and the IIO device belong to the same | |
726 | * device, -EINVAL otherwise. | |
727 | */ | |
728 | int iio_trigger_validate_own_device(struct iio_trigger *trig, | |
af3bac44 | 729 | struct iio_dev *indio_dev) |
43ece27e LPC |
730 | { |
731 | if (indio_dev->dev.parent != trig->dev.parent) | |
732 | return -EINVAL; | |
733 | return 0; | |
734 | } | |
735 | EXPORT_SYMBOL(iio_trigger_validate_own_device); | |
736 | ||
e64506bf | 737 | int iio_device_register_trigger_consumer(struct iio_dev *indio_dev) |
1637db44 | 738 | { |
32f17172 AA |
739 | return iio_device_register_sysfs_group(indio_dev, |
740 | &iio_trigger_consumer_attr_group); | |
1637db44 | 741 | } |
1637db44 | 742 | |
f8c6f4e9 | 743 | void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev) |
1637db44 | 744 | { |
860c9c54 | 745 | /* Clean up an associated but not attached trigger reference */ |
f8c6f4e9 | 746 | if (indio_dev->trig) |
7cbb7537 | 747 | iio_trigger_put(indio_dev->trig); |
1637db44 | 748 | } |