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/module.h>
12 #include <linux/idr.h>
13 #include <linux/err.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
21 #include "trigger_consumer.h"
23 /* RFC - Question of approach
24 * Make the common case (single sensor single trigger)
25 * simple by starting trigger capture from when first sensors
28 * Complex simultaneous start requires use of 'hold' functionality
29 * of the trigger. (not implemented)
31 * Any other suggestions?
34 static DEFINE_IDR(iio_trigger_idr
);
35 static DEFINE_SPINLOCK(iio_trigger_idr_lock
);
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
44 static ssize_t
iio_trigger_read_name(struct device
*dev
,
45 struct device_attribute
*attr
,
48 struct iio_trigger
*trig
= dev_get_drvdata(dev
);
49 return sprintf(buf
, "%s\n", trig
->name
);
52 static DEVICE_ATTR(name
, S_IRUGO
, iio_trigger_read_name
, NULL
);
55 * iio_trigger_register_sysfs() - create a device for this trigger
56 * @trig_info: the trigger
58 * Also adds any control attribute registered by the trigger driver
60 static int iio_trigger_register_sysfs(struct iio_trigger
*trig_info
)
62 return sysfs_add_file_to_group(&trig_info
->dev
.kobj
,
67 static void iio_trigger_unregister_sysfs(struct iio_trigger
*trig_info
)
69 sysfs_remove_file_from_group(&trig_info
->dev
.kobj
,
76 * iio_trigger_register_id() - get a unique id for this trigger
77 * @trig_info: the trigger
79 static int iio_trigger_register_id(struct iio_trigger
*trig_info
)
84 if (unlikely(idr_pre_get(&iio_trigger_idr
, GFP_KERNEL
) == 0))
87 spin_lock(&iio_trigger_idr_lock
);
88 ret
= idr_get_new(&iio_trigger_idr
, NULL
, &trig_info
->id
);
89 spin_unlock(&iio_trigger_idr_lock
);
90 if (unlikely(ret
== -EAGAIN
))
92 else if (likely(!ret
))
93 trig_info
->id
= trig_info
->id
& MAX_ID_MASK
;
99 * iio_trigger_unregister_id() - free up unique id for use by another trigger
100 * @trig_info: the trigger
102 static void iio_trigger_unregister_id(struct iio_trigger
*trig_info
)
104 spin_lock(&iio_trigger_idr_lock
);
105 idr_remove(&iio_trigger_idr
, trig_info
->id
);
106 spin_unlock(&iio_trigger_idr_lock
);
109 int iio_trigger_register(struct iio_trigger
*trig_info
)
113 ret
= iio_trigger_register_id(trig_info
);
116 /* Set the name used for the sysfs directory etc */
117 dev_set_name(&trig_info
->dev
, "trigger%ld",
118 (unsigned long) trig_info
->id
);
120 ret
= device_add(&trig_info
->dev
);
122 goto error_unregister_id
;
124 ret
= iio_trigger_register_sysfs(trig_info
);
126 goto error_device_del
;
128 /* Add to list of available triggers held by the IIO core */
129 mutex_lock(&iio_trigger_list_lock
);
130 list_add_tail(&trig_info
->list
, &iio_trigger_list
);
131 mutex_unlock(&iio_trigger_list_lock
);
136 device_del(&trig_info
->dev
);
138 iio_trigger_unregister_id(trig_info
);
142 EXPORT_SYMBOL(iio_trigger_register
);
144 void iio_trigger_unregister(struct iio_trigger
*trig_info
)
146 mutex_lock(&iio_trigger_list_lock
);
147 list_del(&trig_info
->list
);
148 mutex_unlock(&iio_trigger_list_lock
);
150 iio_trigger_unregister_sysfs(trig_info
);
151 iio_trigger_unregister_id(trig_info
);
152 /* Possible issue in here */
153 device_unregister(&trig_info
->dev
);
155 EXPORT_SYMBOL(iio_trigger_unregister
);
157 static struct iio_trigger
*iio_trigger_find_by_name(const char *name
,
160 struct iio_trigger
*trig
= NULL
, *iter
;
162 mutex_lock(&iio_trigger_list_lock
);
163 list_for_each_entry(iter
, &iio_trigger_list
, list
)
164 if (sysfs_streq(iter
->name
, name
)) {
168 mutex_unlock(&iio_trigger_list_lock
);
173 void iio_trigger_poll(struct iio_trigger
*trig
, s64 time
)
176 if (!trig
->use_count
) {
177 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++)
178 if (trig
->subirqs
[i
].enabled
) {
180 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
->try_reenable
)
210 if (trig
->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 */
219 /* Complexity in here. With certain triggers (datardy) an acknowledgement
220 * may be needed if the pollfuncs do not include the data read for the
222 * This is not currently handled. Alternative of not enabling trigger unless
223 * the relevant function is in there may be the best option.
225 /* Worth protecting against double additions?*/
226 int iio_trigger_attach_poll_func(struct iio_trigger
*trig
,
227 struct iio_poll_func
*pf
)
231 = bitmap_empty(trig
->pool
, CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
233 pf
->irq
= iio_trigger_get_irq(trig
);
234 ret
= request_threaded_irq(pf
->irq
, pf
->h
, pf
->thread
,
237 if (trig
->set_trigger_state
&& notinuse
)
238 ret
= trig
->set_trigger_state(trig
, true);
242 EXPORT_SYMBOL(iio_trigger_attach_poll_func
);
244 int iio_trigger_dettach_poll_func(struct iio_trigger
*trig
,
245 struct iio_poll_func
*pf
)
249 = (bitmap_weight(trig
->pool
,
250 CONFIG_IIO_CONSUMERS_PER_TRIGGER
)
252 if (trig
->set_trigger_state
&& no_other_users
) {
253 ret
= trig
->set_trigger_state(trig
, false);
257 iio_trigger_put_irq(trig
, pf
->irq
);
258 free_irq(pf
->irq
, pf
);
263 EXPORT_SYMBOL(iio_trigger_dettach_poll_func
);
265 irqreturn_t
iio_pollfunc_store_time(int irq
, void *p
)
267 struct iio_poll_func
*pf
= p
;
268 pf
->timestamp
= iio_get_time_ns();
269 return IRQ_WAKE_THREAD
;
271 EXPORT_SYMBOL(iio_pollfunc_store_time
);
274 *iio_alloc_pollfunc(irqreturn_t (*h
)(int irq
, void *p
),
275 irqreturn_t (*thread
)(int irq
, void *p
),
282 struct iio_poll_func
*pf
;
284 pf
= kmalloc(sizeof *pf
, GFP_KERNEL
);
287 va_start(vargs
, fmt
);
288 pf
->name
= kvasprintf(GFP_KERNEL
, fmt
, vargs
);
290 if (pf
->name
== NULL
) {
297 pf
->private_data
= private;
301 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc
);
303 void iio_dealloc_pollfunc(struct iio_poll_func
*pf
)
308 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc
);
311 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
313 * For trigger consumers the current_trigger interface allows the trigger
314 * used by the device to be queried.
316 static ssize_t
iio_trigger_read_current(struct device
*dev
,
317 struct device_attribute
*attr
,
320 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
325 dev_info
->trig
->name
);
330 * iio_trigger_write_current() trigger consumer sysfs set current trigger
332 * For trigger consumers the current_trigger interface allows the trigger
333 * used for this device to be specified at run time based on the triggers
336 static ssize_t
iio_trigger_write_current(struct device
*dev
,
337 struct device_attribute
*attr
,
341 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
342 struct iio_trigger
*oldtrig
= dev_info
->trig
;
343 struct iio_trigger
*trig
;
346 mutex_lock(&dev_info
->mlock
);
347 if (dev_info
->currentmode
== INDIO_RING_TRIGGERED
) {
348 mutex_unlock(&dev_info
->mlock
);
351 mutex_unlock(&dev_info
->mlock
);
353 trig
= iio_trigger_find_by_name(buf
, len
);
355 if (trig
&& dev_info
->info
->validate_trigger
) {
356 ret
= dev_info
->info
->validate_trigger(dev_info
, trig
);
361 if (trig
&& trig
->validate_device
) {
362 ret
= trig
->validate_device(trig
, dev_info
);
367 dev_info
->trig
= trig
;
369 if (oldtrig
&& dev_info
->trig
!= oldtrig
)
370 iio_put_trigger(oldtrig
);
372 iio_get_trigger(dev_info
->trig
);
377 static DEVICE_ATTR(current_trigger
, S_IRUGO
| S_IWUSR
,
378 iio_trigger_read_current
,
379 iio_trigger_write_current
);
381 static struct attribute
*iio_trigger_consumer_attrs
[] = {
382 &dev_attr_current_trigger
.attr
,
386 static const struct attribute_group iio_trigger_consumer_attr_group
= {
388 .attrs
= iio_trigger_consumer_attrs
,
391 static void iio_trig_release(struct device
*device
)
393 struct iio_trigger
*trig
= to_iio_trigger(device
);
396 if (trig
->subirq_base
) {
397 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
398 irq_modify_status(trig
->subirq_base
+ i
,
400 IRQ_NOREQUEST
| IRQ_NOPROBE
);
401 irq_set_chip(trig
->subirq_base
+ i
,
403 irq_set_handler(trig
->subirq_base
+ i
,
407 irq_free_descs(trig
->subirq_base
,
408 CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
415 static struct device_type iio_trig_type
= {
416 .release
= iio_trig_release
,
419 static void iio_trig_subirqmask(struct irq_data
*d
)
421 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
422 struct iio_trigger
*trig
424 struct iio_trigger
, subirq_chip
);
425 trig
->subirqs
[d
->irq
- trig
->subirq_base
].enabled
= false;
428 static void iio_trig_subirqunmask(struct irq_data
*d
)
430 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
431 struct iio_trigger
*trig
433 struct iio_trigger
, subirq_chip
);
434 trig
->subirqs
[d
->irq
- trig
->subirq_base
].enabled
= true;
437 struct iio_trigger
*iio_allocate_trigger(const char *fmt
, ...)
440 struct iio_trigger
*trig
;
441 trig
= kzalloc(sizeof *trig
, GFP_KERNEL
);
444 trig
->dev
.type
= &iio_trig_type
;
445 trig
->dev
.bus
= &iio_bus_type
;
446 device_initialize(&trig
->dev
);
447 dev_set_drvdata(&trig
->dev
, (void *)trig
);
449 mutex_init(&trig
->pool_lock
);
451 = irq_alloc_descs(-1, 0,
452 CONFIG_IIO_CONSUMERS_PER_TRIGGER
,
454 if (trig
->subirq_base
< 0) {
458 va_start(vargs
, fmt
);
459 trig
->name
= kvasprintf(GFP_KERNEL
, fmt
, vargs
);
461 if (trig
->name
== NULL
) {
462 irq_free_descs(trig
->subirq_base
,
463 CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
467 trig
->subirq_chip
.name
= trig
->name
;
468 trig
->subirq_chip
.irq_mask
= &iio_trig_subirqmask
;
469 trig
->subirq_chip
.irq_unmask
= &iio_trig_subirqunmask
;
470 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
471 irq_set_chip(trig
->subirq_base
+ i
,
473 irq_set_handler(trig
->subirq_base
+ i
,
475 irq_modify_status(trig
->subirq_base
+ i
,
476 IRQ_NOREQUEST
| IRQ_NOAUTOEN
,
483 EXPORT_SYMBOL(iio_allocate_trigger
);
485 void iio_free_trigger(struct iio_trigger
*trig
)
488 put_device(&trig
->dev
);
490 EXPORT_SYMBOL(iio_free_trigger
);
492 int iio_device_register_trigger_consumer(struct iio_dev
*dev_info
)
495 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
496 &iio_trigger_consumer_attr_group
);
499 EXPORT_SYMBOL(iio_device_register_trigger_consumer
);
501 int iio_device_unregister_trigger_consumer(struct iio_dev
*dev_info
)
503 sysfs_remove_group(&dev_info
->dev
.kobj
,
504 &iio_trigger_consumer_attr_group
);
507 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer
);
509 int iio_triggered_ring_postenable(struct iio_dev
*indio_dev
)
511 return indio_dev
->trig
512 ? iio_trigger_attach_poll_func(indio_dev
->trig
,
516 EXPORT_SYMBOL(iio_triggered_ring_postenable
);
518 int iio_triggered_ring_predisable(struct iio_dev
*indio_dev
)
520 return indio_dev
->trig
521 ? iio_trigger_dettach_poll_func(indio_dev
->trig
,
525 EXPORT_SYMBOL(iio_triggered_ring_predisable
);