1 /* The industrial I/O core, trigger handling functions
3 * Copyright (c) 2008 Jonathan Cameron
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.
10 #include <linux/kernel.h>
11 #include <linux/idr.h>
12 #include <linux/err.h>
13 #include <linux/device.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/trigger.h>
21 #include "iio_core_trigger.h"
22 #include <linux/iio/trigger_consumer.h>
24 /* RFC - Question of approach
25 * Make the common case (single sensor single trigger)
26 * simple by starting trigger capture from when first sensors
29 * Complex simultaneous start requires use of 'hold' functionality
30 * of the trigger. (not implemented)
32 * Any other suggestions?
35 static DEFINE_IDA(iio_trigger_ida
);
37 /* Single list of all available triggers */
38 static LIST_HEAD(iio_trigger_list
);
39 static DEFINE_MUTEX(iio_trigger_list_lock
);
42 * iio_trigger_read_name() - retrieve useful identifying name
43 * @dev: device associated with the iio_trigger
44 * @attr: pointer to the device_attribute structure that is
46 * @buf: buffer to print the name into
48 * Return: a negative number on failure or the number of written
49 * characters on success.
51 static ssize_t
iio_trigger_read_name(struct device
*dev
,
52 struct device_attribute
*attr
,
55 struct iio_trigger
*trig
= to_iio_trigger(dev
);
56 return sprintf(buf
, "%s\n", trig
->name
);
59 static DEVICE_ATTR(name
, S_IRUGO
, iio_trigger_read_name
, NULL
);
61 static struct attribute
*iio_trig_dev_attrs
[] = {
65 ATTRIBUTE_GROUPS(iio_trig_dev
);
67 int iio_trigger_register(struct iio_trigger
*trig_info
)
71 /* trig_info->ops is required for the module member */
75 trig_info
->id
= ida_simple_get(&iio_trigger_ida
, 0, 0, GFP_KERNEL
);
76 if (trig_info
->id
< 0)
79 /* Set the name used for the sysfs directory etc */
80 dev_set_name(&trig_info
->dev
, "trigger%ld",
81 (unsigned long) trig_info
->id
);
83 ret
= device_add(&trig_info
->dev
);
85 goto error_unregister_id
;
87 /* Add to list of available triggers held by the IIO core */
88 mutex_lock(&iio_trigger_list_lock
);
89 list_add_tail(&trig_info
->list
, &iio_trigger_list
);
90 mutex_unlock(&iio_trigger_list_lock
);
95 ida_simple_remove(&iio_trigger_ida
, trig_info
->id
);
98 EXPORT_SYMBOL(iio_trigger_register
);
100 void iio_trigger_unregister(struct iio_trigger
*trig_info
)
102 mutex_lock(&iio_trigger_list_lock
);
103 list_del(&trig_info
->list
);
104 mutex_unlock(&iio_trigger_list_lock
);
106 ida_simple_remove(&iio_trigger_ida
, trig_info
->id
);
107 /* Possible issue in here */
108 device_del(&trig_info
->dev
);
110 EXPORT_SYMBOL(iio_trigger_unregister
);
112 static struct iio_trigger
*iio_trigger_find_by_name(const char *name
,
115 struct iio_trigger
*trig
= NULL
, *iter
;
117 mutex_lock(&iio_trigger_list_lock
);
118 list_for_each_entry(iter
, &iio_trigger_list
, list
)
119 if (sysfs_streq(iter
->name
, name
)) {
123 mutex_unlock(&iio_trigger_list_lock
);
128 void iio_trigger_poll(struct iio_trigger
*trig
)
132 if (!atomic_read(&trig
->use_count
)) {
133 atomic_set(&trig
->use_count
, CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
135 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
136 if (trig
->subirqs
[i
].enabled
)
137 generic_handle_irq(trig
->subirq_base
+ i
);
139 iio_trigger_notify_done(trig
);
143 EXPORT_SYMBOL(iio_trigger_poll
);
145 irqreturn_t
iio_trigger_generic_data_rdy_poll(int irq
, void *private)
147 iio_trigger_poll(private);
150 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll
);
152 void iio_trigger_poll_chained(struct iio_trigger
*trig
)
156 if (!atomic_read(&trig
->use_count
)) {
157 atomic_set(&trig
->use_count
, CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
159 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
160 if (trig
->subirqs
[i
].enabled
)
161 handle_nested_irq(trig
->subirq_base
+ i
);
163 iio_trigger_notify_done(trig
);
167 EXPORT_SYMBOL(iio_trigger_poll_chained
);
169 void iio_trigger_notify_done(struct iio_trigger
*trig
)
171 if (atomic_dec_and_test(&trig
->use_count
) && trig
->ops
->try_reenable
)
172 if (trig
->ops
->try_reenable(trig
))
173 /* Missed an interrupt so launch new poll now */
174 iio_trigger_poll(trig
);
176 EXPORT_SYMBOL(iio_trigger_notify_done
);
178 /* Trigger Consumer related functions */
179 static int iio_trigger_get_irq(struct iio_trigger
*trig
)
182 mutex_lock(&trig
->pool_lock
);
183 ret
= bitmap_find_free_region(trig
->pool
,
184 CONFIG_IIO_CONSUMERS_PER_TRIGGER
,
186 mutex_unlock(&trig
->pool_lock
);
188 ret
+= trig
->subirq_base
;
193 static void iio_trigger_put_irq(struct iio_trigger
*trig
, int irq
)
195 mutex_lock(&trig
->pool_lock
);
196 clear_bit(irq
- trig
->subirq_base
, trig
->pool
);
197 mutex_unlock(&trig
->pool_lock
);
200 /* Complexity in here. With certain triggers (datardy) an acknowledgement
201 * may be needed if the pollfuncs do not include the data read for the
203 * This is not currently handled. Alternative of not enabling trigger unless
204 * the relevant function is in there may be the best option.
206 /* Worth protecting against double additions? */
207 static int iio_trigger_attach_poll_func(struct iio_trigger
*trig
,
208 struct iio_poll_func
*pf
)
212 = bitmap_empty(trig
->pool
, CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
214 /* Prevent the module from being removed whilst attached to a trigger */
215 __module_get(pf
->indio_dev
->info
->driver_module
);
218 pf
->irq
= iio_trigger_get_irq(trig
);
223 ret
= request_threaded_irq(pf
->irq
, pf
->h
, pf
->thread
,
229 /* Enable trigger in driver */
230 if (trig
->ops
->set_trigger_state
&& notinuse
) {
231 ret
= trig
->ops
->set_trigger_state(trig
, true);
239 free_irq(pf
->irq
, pf
);
241 iio_trigger_put_irq(trig
, pf
->irq
);
243 module_put(pf
->indio_dev
->info
->driver_module
);
247 static int iio_trigger_detach_poll_func(struct iio_trigger
*trig
,
248 struct iio_poll_func
*pf
)
252 = (bitmap_weight(trig
->pool
,
253 CONFIG_IIO_CONSUMERS_PER_TRIGGER
)
255 if (trig
->ops
->set_trigger_state
&& no_other_users
) {
256 ret
= trig
->ops
->set_trigger_state(trig
, false);
260 iio_trigger_put_irq(trig
, pf
->irq
);
261 free_irq(pf
->irq
, pf
);
262 module_put(pf
->indio_dev
->info
->driver_module
);
267 irqreturn_t
iio_pollfunc_store_time(int irq
, void *p
)
269 struct iio_poll_func
*pf
= p
;
270 pf
->timestamp
= iio_get_time_ns();
271 return IRQ_WAKE_THREAD
;
273 EXPORT_SYMBOL(iio_pollfunc_store_time
);
276 *iio_alloc_pollfunc(irqreturn_t (*h
)(int irq
, void *p
),
277 irqreturn_t (*thread
)(int irq
, void *p
),
279 struct iio_dev
*indio_dev
,
284 struct iio_poll_func
*pf
;
286 pf
= kmalloc(sizeof *pf
, GFP_KERNEL
);
289 va_start(vargs
, fmt
);
290 pf
->name
= kvasprintf(GFP_KERNEL
, fmt
, vargs
);
292 if (pf
->name
== NULL
) {
299 pf
->indio_dev
= indio_dev
;
303 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc
);
305 void iio_dealloc_pollfunc(struct iio_poll_func
*pf
)
310 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc
);
313 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
314 * @dev: device associated with an industrial I/O device
315 * @attr: pointer to the device_attribute structure that
317 * @buf: buffer where the current trigger name will be printed into
319 * For trigger consumers the current_trigger interface allows the trigger
320 * used by the device to be queried.
322 * Return: a negative number on failure, the number of characters written
323 * on success or 0 if no trigger is available
325 static ssize_t
iio_trigger_read_current(struct device
*dev
,
326 struct device_attribute
*attr
,
329 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
332 return sprintf(buf
, "%s\n", indio_dev
->trig
->name
);
337 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
338 * @dev: device associated with an industrial I/O device
339 * @attr: device attribute that is being processed
340 * @buf: string buffer that holds the name of the trigger
341 * @len: length of the trigger name held by buf
343 * For trigger consumers the current_trigger interface allows the trigger
344 * used for this device to be specified at run time based on the trigger's
347 * Return: negative error code on failure or length of the buffer
350 static ssize_t
iio_trigger_write_current(struct device
*dev
,
351 struct device_attribute
*attr
,
355 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
356 struct iio_trigger
*oldtrig
= indio_dev
->trig
;
357 struct iio_trigger
*trig
;
360 mutex_lock(&indio_dev
->mlock
);
361 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
) {
362 mutex_unlock(&indio_dev
->mlock
);
365 mutex_unlock(&indio_dev
->mlock
);
367 trig
= iio_trigger_find_by_name(buf
, len
);
371 if (trig
&& indio_dev
->info
->validate_trigger
) {
372 ret
= indio_dev
->info
->validate_trigger(indio_dev
, trig
);
377 if (trig
&& trig
->ops
->validate_device
) {
378 ret
= trig
->ops
->validate_device(trig
, indio_dev
);
383 indio_dev
->trig
= trig
;
386 if (indio_dev
->modes
& INDIO_EVENT_TRIGGERED
)
387 iio_trigger_detach_poll_func(oldtrig
,
388 indio_dev
->pollfunc_event
);
389 iio_trigger_put(oldtrig
);
391 if (indio_dev
->trig
) {
392 iio_trigger_get(indio_dev
->trig
);
393 if (indio_dev
->modes
& INDIO_EVENT_TRIGGERED
)
394 iio_trigger_attach_poll_func(indio_dev
->trig
,
395 indio_dev
->pollfunc_event
);
401 static DEVICE_ATTR(current_trigger
, S_IRUGO
| S_IWUSR
,
402 iio_trigger_read_current
,
403 iio_trigger_write_current
);
405 static struct attribute
*iio_trigger_consumer_attrs
[] = {
406 &dev_attr_current_trigger
.attr
,
410 static const struct attribute_group iio_trigger_consumer_attr_group
= {
412 .attrs
= iio_trigger_consumer_attrs
,
415 static void iio_trig_release(struct device
*device
)
417 struct iio_trigger
*trig
= to_iio_trigger(device
);
420 if (trig
->subirq_base
) {
421 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
422 irq_modify_status(trig
->subirq_base
+ i
,
424 IRQ_NOREQUEST
| IRQ_NOPROBE
);
425 irq_set_chip(trig
->subirq_base
+ i
,
427 irq_set_handler(trig
->subirq_base
+ i
,
431 irq_free_descs(trig
->subirq_base
,
432 CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
438 static struct device_type iio_trig_type
= {
439 .release
= iio_trig_release
,
440 .groups
= iio_trig_dev_groups
,
443 static void iio_trig_subirqmask(struct irq_data
*d
)
445 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
446 struct iio_trigger
*trig
448 struct iio_trigger
, subirq_chip
);
449 trig
->subirqs
[d
->irq
- trig
->subirq_base
].enabled
= false;
452 static void iio_trig_subirqunmask(struct irq_data
*d
)
454 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
455 struct iio_trigger
*trig
457 struct iio_trigger
, subirq_chip
);
458 trig
->subirqs
[d
->irq
- trig
->subirq_base
].enabled
= true;
461 static struct iio_trigger
*viio_trigger_alloc(const char *fmt
, va_list vargs
)
463 struct iio_trigger
*trig
;
464 trig
= kzalloc(sizeof *trig
, GFP_KERNEL
);
467 trig
->dev
.type
= &iio_trig_type
;
468 trig
->dev
.bus
= &iio_bus_type
;
469 device_initialize(&trig
->dev
);
471 mutex_init(&trig
->pool_lock
);
473 = irq_alloc_descs(-1, 0,
474 CONFIG_IIO_CONSUMERS_PER_TRIGGER
,
476 if (trig
->subirq_base
< 0) {
481 trig
->name
= kvasprintf(GFP_KERNEL
, fmt
, vargs
);
482 if (trig
->name
== NULL
) {
483 irq_free_descs(trig
->subirq_base
,
484 CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
488 trig
->subirq_chip
.name
= trig
->name
;
489 trig
->subirq_chip
.irq_mask
= &iio_trig_subirqmask
;
490 trig
->subirq_chip
.irq_unmask
= &iio_trig_subirqunmask
;
491 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
492 irq_set_chip(trig
->subirq_base
+ i
,
494 irq_set_handler(trig
->subirq_base
+ i
,
496 irq_modify_status(trig
->subirq_base
+ i
,
497 IRQ_NOREQUEST
| IRQ_NOAUTOEN
,
500 get_device(&trig
->dev
);
506 struct iio_trigger
*iio_trigger_alloc(const char *fmt
, ...)
508 struct iio_trigger
*trig
;
511 va_start(vargs
, fmt
);
512 trig
= viio_trigger_alloc(fmt
, vargs
);
517 EXPORT_SYMBOL(iio_trigger_alloc
);
519 void iio_trigger_free(struct iio_trigger
*trig
)
522 put_device(&trig
->dev
);
524 EXPORT_SYMBOL(iio_trigger_free
);
526 static void devm_iio_trigger_release(struct device
*dev
, void *res
)
528 iio_trigger_free(*(struct iio_trigger
**)res
);
531 static int devm_iio_trigger_match(struct device
*dev
, void *res
, void *data
)
533 struct iio_trigger
**r
= res
;
544 * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
545 * @dev: Device to allocate iio_trigger for
546 * @fmt: trigger name format. If it includes format
547 * specifiers, the additional arguments following
548 * format are formatted and inserted in the resulting
549 * string replacing their respective specifiers.
551 * Managed iio_trigger_alloc. iio_trigger allocated with this function is
552 * automatically freed on driver detach.
554 * If an iio_trigger allocated with this function needs to be freed separately,
555 * devm_iio_trigger_free() must be used.
558 * Pointer to allocated iio_trigger on success, NULL on failure.
560 struct iio_trigger
*devm_iio_trigger_alloc(struct device
*dev
,
561 const char *fmt
, ...)
563 struct iio_trigger
**ptr
, *trig
;
566 ptr
= devres_alloc(devm_iio_trigger_release
, sizeof(*ptr
),
571 /* use raw alloc_dr for kmalloc caller tracing */
572 va_start(vargs
, fmt
);
573 trig
= viio_trigger_alloc(fmt
, vargs
);
577 devres_add(dev
, ptr
);
584 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc
);
587 * devm_iio_trigger_free - Resource-managed iio_trigger_free()
588 * @dev: Device this iio_dev belongs to
589 * @iio_trig: the iio_trigger associated with the device
591 * Free iio_trigger allocated with devm_iio_trigger_alloc().
593 void devm_iio_trigger_free(struct device
*dev
, struct iio_trigger
*iio_trig
)
597 rc
= devres_release(dev
, devm_iio_trigger_release
,
598 devm_iio_trigger_match
, iio_trig
);
601 EXPORT_SYMBOL_GPL(devm_iio_trigger_free
);
603 void iio_device_register_trigger_consumer(struct iio_dev
*indio_dev
)
605 indio_dev
->groups
[indio_dev
->groupcounter
++] =
606 &iio_trigger_consumer_attr_group
;
609 void iio_device_unregister_trigger_consumer(struct iio_dev
*indio_dev
)
611 /* Clean up an associated but not attached trigger reference */
613 iio_trigger_put(indio_dev
->trig
);
616 int iio_triggered_buffer_postenable(struct iio_dev
*indio_dev
)
618 return iio_trigger_attach_poll_func(indio_dev
->trig
,
619 indio_dev
->pollfunc
);
621 EXPORT_SYMBOL(iio_triggered_buffer_postenable
);
623 int iio_triggered_buffer_predisable(struct iio_dev
*indio_dev
)
625 return iio_trigger_detach_poll_func(indio_dev
->trig
,
626 indio_dev
->pollfunc
);
628 EXPORT_SYMBOL(iio_triggered_buffer_predisable
);