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>
21 #include "iio_core_trigger.h"
22 #include "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_IDR(iio_trigger_idr
);
36 static DEFINE_SPINLOCK(iio_trigger_idr_lock
);
38 /* Single list of all available triggers */
39 static LIST_HEAD(iio_trigger_list
);
40 static DEFINE_MUTEX(iio_trigger_list_lock
);
43 * iio_trigger_read_name() - retrieve useful identifying name
45 static ssize_t
iio_trigger_read_name(struct device
*dev
,
46 struct device_attribute
*attr
,
49 struct iio_trigger
*trig
= dev_get_drvdata(dev
);
50 return sprintf(buf
, "%s\n", trig
->name
);
53 static DEVICE_ATTR(name
, S_IRUGO
, iio_trigger_read_name
, NULL
);
56 * iio_trigger_register_sysfs() - create a device for this trigger
57 * @trig_info: the trigger
59 * Also adds any control attribute registered by the trigger driver
61 static int iio_trigger_register_sysfs(struct iio_trigger
*trig_info
)
63 return sysfs_add_file_to_group(&trig_info
->dev
.kobj
,
68 static void iio_trigger_unregister_sysfs(struct iio_trigger
*trig_info
)
70 sysfs_remove_file_from_group(&trig_info
->dev
.kobj
,
77 * iio_trigger_register_id() - get a unique id for this trigger
78 * @trig_info: the trigger
80 static int iio_trigger_register_id(struct iio_trigger
*trig_info
)
85 if (unlikely(idr_pre_get(&iio_trigger_idr
, GFP_KERNEL
) == 0))
88 spin_lock(&iio_trigger_idr_lock
);
89 ret
= idr_get_new(&iio_trigger_idr
, NULL
, &trig_info
->id
);
90 spin_unlock(&iio_trigger_idr_lock
);
91 if (unlikely(ret
== -EAGAIN
))
93 else if (likely(!ret
))
94 trig_info
->id
= trig_info
->id
& MAX_ID_MASK
;
100 * iio_trigger_unregister_id() - free up unique id for use by another trigger
101 * @trig_info: the trigger
103 static void iio_trigger_unregister_id(struct iio_trigger
*trig_info
)
105 spin_lock(&iio_trigger_idr_lock
);
106 idr_remove(&iio_trigger_idr
, trig_info
->id
);
107 spin_unlock(&iio_trigger_idr_lock
);
110 int iio_trigger_register(struct iio_trigger
*trig_info
)
114 ret
= iio_trigger_register_id(trig_info
);
117 /* Set the name used for the sysfs directory etc */
118 dev_set_name(&trig_info
->dev
, "trigger%ld",
119 (unsigned long) trig_info
->id
);
121 ret
= device_add(&trig_info
->dev
);
123 goto error_unregister_id
;
125 ret
= iio_trigger_register_sysfs(trig_info
);
127 goto error_device_del
;
129 /* Add to list of available triggers held by the IIO core */
130 mutex_lock(&iio_trigger_list_lock
);
131 list_add_tail(&trig_info
->list
, &iio_trigger_list
);
132 mutex_unlock(&iio_trigger_list_lock
);
137 device_del(&trig_info
->dev
);
139 iio_trigger_unregister_id(trig_info
);
143 EXPORT_SYMBOL(iio_trigger_register
);
145 void iio_trigger_unregister(struct iio_trigger
*trig_info
)
147 mutex_lock(&iio_trigger_list_lock
);
148 list_del(&trig_info
->list
);
149 mutex_unlock(&iio_trigger_list_lock
);
151 iio_trigger_unregister_sysfs(trig_info
);
152 iio_trigger_unregister_id(trig_info
);
153 /* Possible issue in here */
154 device_unregister(&trig_info
->dev
);
156 EXPORT_SYMBOL(iio_trigger_unregister
);
158 static struct iio_trigger
*iio_trigger_find_by_name(const char *name
,
161 struct iio_trigger
*trig
= NULL
, *iter
;
163 mutex_lock(&iio_trigger_list_lock
);
164 list_for_each_entry(iter
, &iio_trigger_list
, list
)
165 if (sysfs_streq(iter
->name
, name
)) {
169 mutex_unlock(&iio_trigger_list_lock
);
174 void iio_trigger_poll(struct iio_trigger
*trig
, s64 time
)
177 if (!trig
->use_count
)
178 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++)
179 if (trig
->subirqs
[i
].enabled
) {
181 generic_handle_irq(trig
->subirq_base
+ i
);
184 EXPORT_SYMBOL(iio_trigger_poll
);
186 irqreturn_t
iio_trigger_generic_data_rdy_poll(int irq
, void *private)
188 iio_trigger_poll(private, iio_get_time_ns());
191 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll
);
193 void iio_trigger_poll_chained(struct iio_trigger
*trig
, s64 time
)
196 if (!trig
->use_count
) {
197 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++)
198 if (trig
->subirqs
[i
].enabled
) {
200 handle_nested_irq(trig
->subirq_base
+ i
);
204 EXPORT_SYMBOL(iio_trigger_poll_chained
);
206 void iio_trigger_notify_done(struct iio_trigger
*trig
)
209 if (trig
->use_count
== 0 && trig
->ops
&& trig
->ops
->try_reenable
)
210 if (trig
->ops
->try_reenable(trig
)) {
211 /* Missed and interrupt so launch new poll now */
212 iio_trigger_poll(trig
, 0);
215 EXPORT_SYMBOL(iio_trigger_notify_done
);
217 /* Trigger Consumer related functions */
218 static int iio_trigger_get_irq(struct iio_trigger
*trig
)
221 mutex_lock(&trig
->pool_lock
);
222 ret
= bitmap_find_free_region(trig
->pool
,
223 CONFIG_IIO_CONSUMERS_PER_TRIGGER
,
225 mutex_unlock(&trig
->pool_lock
);
227 ret
+= trig
->subirq_base
;
232 static void iio_trigger_put_irq(struct iio_trigger
*trig
, int irq
)
234 mutex_lock(&trig
->pool_lock
);
235 clear_bit(irq
- trig
->subirq_base
, trig
->pool
);
236 mutex_unlock(&trig
->pool_lock
);
239 /* Complexity in here. With certain triggers (datardy) an acknowledgement
240 * may be needed if the pollfuncs do not include the data read for the
242 * This is not currently handled. Alternative of not enabling trigger unless
243 * the relevant function is in there may be the best option.
245 /* Worth protecting against double additions?*/
246 static int iio_trigger_attach_poll_func(struct iio_trigger
*trig
,
247 struct iio_poll_func
*pf
)
251 = bitmap_empty(trig
->pool
, CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
253 /* Prevent the module being removed whilst attached to a trigger */
254 __module_get(pf
->indio_dev
->info
->driver_module
);
255 pf
->irq
= iio_trigger_get_irq(trig
);
256 ret
= request_threaded_irq(pf
->irq
, pf
->h
, pf
->thread
,
259 if (trig
->ops
&& trig
->ops
->set_trigger_state
&& notinuse
)
260 ret
= trig
->ops
->set_trigger_state(trig
, true);
265 static int iio_trigger_dettach_poll_func(struct iio_trigger
*trig
,
266 struct iio_poll_func
*pf
)
270 = (bitmap_weight(trig
->pool
,
271 CONFIG_IIO_CONSUMERS_PER_TRIGGER
)
273 if (trig
->ops
&& trig
->ops
->set_trigger_state
&& no_other_users
) {
274 ret
= trig
->ops
->set_trigger_state(trig
, false);
278 iio_trigger_put_irq(trig
, pf
->irq
);
279 free_irq(pf
->irq
, pf
);
280 module_put(pf
->indio_dev
->info
->driver_module
);
286 irqreturn_t
iio_pollfunc_store_time(int irq
, void *p
)
288 struct iio_poll_func
*pf
= p
;
289 pf
->timestamp
= iio_get_time_ns();
290 return IRQ_WAKE_THREAD
;
292 EXPORT_SYMBOL(iio_pollfunc_store_time
);
295 *iio_alloc_pollfunc(irqreturn_t (*h
)(int irq
, void *p
),
296 irqreturn_t (*thread
)(int irq
, void *p
),
298 struct iio_dev
*indio_dev
,
303 struct iio_poll_func
*pf
;
305 pf
= kmalloc(sizeof *pf
, GFP_KERNEL
);
308 va_start(vargs
, fmt
);
309 pf
->name
= kvasprintf(GFP_KERNEL
, fmt
, vargs
);
311 if (pf
->name
== NULL
) {
318 pf
->indio_dev
= indio_dev
;
322 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc
);
324 void iio_dealloc_pollfunc(struct iio_poll_func
*pf
)
329 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc
);
332 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
334 * For trigger consumers the current_trigger interface allows the trigger
335 * used by the device to be queried.
337 static ssize_t
iio_trigger_read_current(struct device
*dev
,
338 struct device_attribute
*attr
,
341 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
344 return sprintf(buf
, "%s\n", dev_info
->trig
->name
);
349 * iio_trigger_write_current() trigger consumer sysfs set current trigger
351 * For trigger consumers the current_trigger interface allows the trigger
352 * used for this device to be specified at run time based on the triggers
355 static ssize_t
iio_trigger_write_current(struct device
*dev
,
356 struct device_attribute
*attr
,
360 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
361 struct iio_trigger
*oldtrig
= dev_info
->trig
;
362 struct iio_trigger
*trig
;
365 mutex_lock(&dev_info
->mlock
);
366 if (dev_info
->currentmode
== INDIO_RING_TRIGGERED
) {
367 mutex_unlock(&dev_info
->mlock
);
370 mutex_unlock(&dev_info
->mlock
);
372 trig
= iio_trigger_find_by_name(buf
, len
);
374 if (trig
&& dev_info
->info
->validate_trigger
) {
375 ret
= dev_info
->info
->validate_trigger(dev_info
, trig
);
380 if (trig
&& trig
->ops
&& trig
->ops
->validate_device
) {
381 ret
= trig
->ops
->validate_device(trig
, dev_info
);
386 dev_info
->trig
= trig
;
388 if (oldtrig
&& dev_info
->trig
!= oldtrig
)
389 iio_put_trigger(oldtrig
);
391 iio_get_trigger(dev_info
->trig
);
396 static DEVICE_ATTR(current_trigger
, S_IRUGO
| S_IWUSR
,
397 iio_trigger_read_current
,
398 iio_trigger_write_current
);
400 static struct attribute
*iio_trigger_consumer_attrs
[] = {
401 &dev_attr_current_trigger
.attr
,
405 static const struct attribute_group iio_trigger_consumer_attr_group
= {
407 .attrs
= iio_trigger_consumer_attrs
,
410 static void iio_trig_release(struct device
*device
)
412 struct iio_trigger
*trig
= to_iio_trigger(device
);
415 if (trig
->subirq_base
) {
416 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
417 irq_modify_status(trig
->subirq_base
+ i
,
419 IRQ_NOREQUEST
| IRQ_NOPROBE
);
420 irq_set_chip(trig
->subirq_base
+ i
,
422 irq_set_handler(trig
->subirq_base
+ i
,
426 irq_free_descs(trig
->subirq_base
,
427 CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
434 static struct device_type iio_trig_type
= {
435 .release
= iio_trig_release
,
438 static void iio_trig_subirqmask(struct irq_data
*d
)
440 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
441 struct iio_trigger
*trig
443 struct iio_trigger
, subirq_chip
);
444 trig
->subirqs
[d
->irq
- trig
->subirq_base
].enabled
= false;
447 static void iio_trig_subirqunmask(struct irq_data
*d
)
449 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
450 struct iio_trigger
*trig
452 struct iio_trigger
, subirq_chip
);
453 trig
->subirqs
[d
->irq
- trig
->subirq_base
].enabled
= true;
456 struct iio_trigger
*iio_allocate_trigger(const char *fmt
, ...)
459 struct iio_trigger
*trig
;
460 trig
= kzalloc(sizeof *trig
, GFP_KERNEL
);
463 trig
->dev
.type
= &iio_trig_type
;
464 trig
->dev
.bus
= &iio_bus_type
;
465 device_initialize(&trig
->dev
);
466 dev_set_drvdata(&trig
->dev
, (void *)trig
);
468 mutex_init(&trig
->pool_lock
);
470 = irq_alloc_descs(-1, 0,
471 CONFIG_IIO_CONSUMERS_PER_TRIGGER
,
473 if (trig
->subirq_base
< 0) {
477 va_start(vargs
, fmt
);
478 trig
->name
= kvasprintf(GFP_KERNEL
, fmt
, vargs
);
480 if (trig
->name
== NULL
) {
481 irq_free_descs(trig
->subirq_base
,
482 CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
486 trig
->subirq_chip
.name
= trig
->name
;
487 trig
->subirq_chip
.irq_mask
= &iio_trig_subirqmask
;
488 trig
->subirq_chip
.irq_unmask
= &iio_trig_subirqunmask
;
489 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
490 irq_set_chip(trig
->subirq_base
+ i
,
492 irq_set_handler(trig
->subirq_base
+ i
,
494 irq_modify_status(trig
->subirq_base
+ i
,
495 IRQ_NOREQUEST
| IRQ_NOAUTOEN
,
499 get_device(&trig
->dev
);
503 EXPORT_SYMBOL(iio_allocate_trigger
);
505 void iio_free_trigger(struct iio_trigger
*trig
)
508 put_device(&trig
->dev
);
510 EXPORT_SYMBOL(iio_free_trigger
);
512 int iio_device_register_trigger_consumer(struct iio_dev
*dev_info
)
514 return sysfs_create_group(&dev_info
->dev
.kobj
,
515 &iio_trigger_consumer_attr_group
);
518 void iio_device_unregister_trigger_consumer(struct iio_dev
*dev_info
)
520 /* Clean up and associated but not attached triggers references */
522 iio_put_trigger(dev_info
->trig
);
523 sysfs_remove_group(&dev_info
->dev
.kobj
,
524 &iio_trigger_consumer_attr_group
);
527 int iio_triggered_ring_postenable(struct iio_dev
*indio_dev
)
529 return indio_dev
->trig
530 ? iio_trigger_attach_poll_func(indio_dev
->trig
,
534 EXPORT_SYMBOL(iio_triggered_ring_postenable
);
536 int iio_triggered_ring_predisable(struct iio_dev
*indio_dev
)
538 return indio_dev
->trig
539 ? iio_trigger_dettach_poll_func(indio_dev
->trig
,
543 EXPORT_SYMBOL(iio_triggered_ring_predisable
);