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_register_sysfs() - create a device for this trigger
43 * @trig_info: the trigger
45 * Also adds any control attribute registered by the trigger driver
47 static int iio_trigger_register_sysfs(struct iio_trigger
*trig_info
)
51 if (trig_info
->control_attrs
)
52 ret
= sysfs_create_group(&trig_info
->dev
.kobj
,
53 trig_info
->control_attrs
);
58 static void iio_trigger_unregister_sysfs(struct iio_trigger
*trig_info
)
60 if (trig_info
->control_attrs
)
61 sysfs_remove_group(&trig_info
->dev
.kobj
,
62 trig_info
->control_attrs
);
67 * iio_trigger_register_id() - get a unique id for this trigger
68 * @trig_info: the trigger
70 static int iio_trigger_register_id(struct iio_trigger
*trig_info
)
75 if (unlikely(idr_pre_get(&iio_trigger_idr
, GFP_KERNEL
) == 0))
78 spin_lock(&iio_trigger_idr_lock
);
79 ret
= idr_get_new(&iio_trigger_idr
, NULL
, &trig_info
->id
);
80 spin_unlock(&iio_trigger_idr_lock
);
81 if (unlikely(ret
== -EAGAIN
))
83 else if (likely(!ret
))
84 trig_info
->id
= trig_info
->id
& MAX_ID_MASK
;
90 * iio_trigger_unregister_id() - free up unique id for use by another trigger
91 * @trig_info: the trigger
93 static void iio_trigger_unregister_id(struct iio_trigger
*trig_info
)
95 spin_lock(&iio_trigger_idr_lock
);
96 idr_remove(&iio_trigger_idr
, trig_info
->id
);
97 spin_unlock(&iio_trigger_idr_lock
);
100 int iio_trigger_register(struct iio_trigger
*trig_info
)
104 ret
= iio_trigger_register_id(trig_info
);
107 /* Set the name used for the sysfs directory etc */
108 dev_set_name(&trig_info
->dev
, "trigger%ld",
109 (unsigned long) trig_info
->id
);
111 ret
= device_add(&trig_info
->dev
);
113 goto error_unregister_id
;
115 ret
= iio_trigger_register_sysfs(trig_info
);
117 goto error_device_del
;
119 /* Add to list of available triggers held by the IIO core */
120 mutex_lock(&iio_trigger_list_lock
);
121 list_add_tail(&trig_info
->list
, &iio_trigger_list
);
122 mutex_unlock(&iio_trigger_list_lock
);
127 device_del(&trig_info
->dev
);
129 iio_trigger_unregister_id(trig_info
);
133 EXPORT_SYMBOL(iio_trigger_register
);
135 void iio_trigger_unregister(struct iio_trigger
*trig_info
)
137 mutex_lock(&iio_trigger_list_lock
);
138 list_del(&trig_info
->list
);
139 mutex_unlock(&iio_trigger_list_lock
);
141 iio_trigger_unregister_sysfs(trig_info
);
142 iio_trigger_unregister_id(trig_info
);
143 /* Possible issue in here */
144 device_unregister(&trig_info
->dev
);
146 EXPORT_SYMBOL(iio_trigger_unregister
);
148 static struct iio_trigger
*iio_trigger_find_by_name(const char *name
,
151 struct iio_trigger
*trig
= NULL
, *iter
;
153 mutex_lock(&iio_trigger_list_lock
);
154 list_for_each_entry(iter
, &iio_trigger_list
, list
)
155 if (sysfs_streq(iter
->name
, name
)) {
159 mutex_unlock(&iio_trigger_list_lock
);
164 void iio_trigger_poll(struct iio_trigger
*trig
, s64 time
)
167 if (!trig
->use_count
) {
168 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++)
169 if (trig
->subirqs
[i
].enabled
) {
171 generic_handle_irq(trig
->subirq_base
+ i
);
175 EXPORT_SYMBOL(iio_trigger_poll
);
177 irqreturn_t
iio_trigger_generic_data_rdy_poll(int irq
, void *private)
179 iio_trigger_poll(private, iio_get_time_ns());
182 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll
);
184 void iio_trigger_notify_done(struct iio_trigger
*trig
)
187 if (trig
->use_count
== 0 && trig
->try_reenable
)
188 if (trig
->try_reenable(trig
)) {
189 /* Missed and interrupt so launch new poll now */
190 iio_trigger_poll(trig
, 0);
193 EXPORT_SYMBOL(iio_trigger_notify_done
);
196 * iio_trigger_read_name() - retrieve useful identifying name
198 ssize_t
iio_trigger_read_name(struct device
*dev
,
199 struct device_attribute
*attr
,
202 struct iio_trigger
*trig
= dev_get_drvdata(dev
);
203 return sprintf(buf
, "%s\n", trig
->name
);
205 EXPORT_SYMBOL(iio_trigger_read_name
);
207 /* Trigger Consumer related functions */
209 /* Complexity in here. With certain triggers (datardy) an acknowledgement
210 * may be needed if the pollfuncs do not include the data read for the
212 * This is not currently handled. Alternative of not enabling trigger unless
213 * the relevant function is in there may be the best option.
215 /* Worth protecting against double additions?*/
216 int iio_trigger_attach_poll_func(struct iio_trigger
*trig
,
217 struct iio_poll_func
*pf
)
221 = bitmap_empty(trig
->pool
, CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
223 pf
->irq
= iio_trigger_get_irq(trig
);
224 ret
= request_threaded_irq(pf
->irq
, pf
->h
, pf
->thread
,
227 if (trig
->set_trigger_state
&& notinuse
)
228 ret
= trig
->set_trigger_state(trig
, true);
232 EXPORT_SYMBOL(iio_trigger_attach_poll_func
);
234 int iio_trigger_dettach_poll_func(struct iio_trigger
*trig
,
235 struct iio_poll_func
*pf
)
239 = (bitmap_weight(trig
->pool
,
240 CONFIG_IIO_CONSUMERS_PER_TRIGGER
)
242 if (trig
->set_trigger_state
&& no_other_users
) {
243 ret
= trig
->set_trigger_state(trig
, false);
247 iio_trigger_put_irq(trig
, pf
->irq
);
248 free_irq(pf
->irq
, pf
);
253 EXPORT_SYMBOL(iio_trigger_dettach_poll_func
);
255 irqreturn_t
iio_pollfunc_store_time(int irq
, void *p
)
257 struct iio_poll_func
*pf
= p
;
258 pf
->timestamp
= iio_get_time_ns();
259 return IRQ_WAKE_THREAD
;
261 EXPORT_SYMBOL(iio_pollfunc_store_time
);
264 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
266 * For trigger consumers the current_trigger interface allows the trigger
267 * used by the device to be queried.
269 static ssize_t
iio_trigger_read_current(struct device
*dev
,
270 struct device_attribute
*attr
,
273 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
278 dev_info
->trig
->name
);
283 * iio_trigger_write_current() trigger consumer sysfs set current trigger
285 * For trigger consumers the current_trigger interface allows the trigger
286 * used for this device to be specified at run time based on the triggers
289 static ssize_t
iio_trigger_write_current(struct device
*dev
,
290 struct device_attribute
*attr
,
294 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
295 struct iio_trigger
*oldtrig
= dev_info
->trig
;
296 mutex_lock(&dev_info
->mlock
);
297 if (dev_info
->currentmode
== INDIO_RING_TRIGGERED
) {
298 mutex_unlock(&dev_info
->mlock
);
301 mutex_unlock(&dev_info
->mlock
);
303 dev_info
->trig
= iio_trigger_find_by_name(buf
, len
);
304 if (oldtrig
&& dev_info
->trig
!= oldtrig
)
305 iio_put_trigger(oldtrig
);
307 iio_get_trigger(dev_info
->trig
);
312 static DEVICE_ATTR(current_trigger
, S_IRUGO
| S_IWUSR
,
313 iio_trigger_read_current
,
314 iio_trigger_write_current
);
316 static struct attribute
*iio_trigger_consumer_attrs
[] = {
317 &dev_attr_current_trigger
.attr
,
321 static const struct attribute_group iio_trigger_consumer_attr_group
= {
323 .attrs
= iio_trigger_consumer_attrs
,
326 static void iio_trig_release(struct device
*device
)
328 struct iio_trigger
*trig
= to_iio_trigger(device
);
331 if (trig
->subirq_base
) {
332 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
333 irq_modify_status(trig
->subirq_base
+ i
,
335 IRQ_NOREQUEST
| IRQ_NOPROBE
);
336 irq_set_chip(trig
->subirq_base
+ i
,
338 irq_set_handler(trig
->subirq_base
+ i
,
342 irq_free_descs(trig
->subirq_base
,
343 CONFIG_IIO_CONSUMERS_PER_TRIGGER
);
349 static struct device_type iio_trig_type
= {
350 .release
= iio_trig_release
,
353 static void iio_trig_subirqmask(struct irq_data
*d
)
355 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
356 struct iio_trigger
*trig
358 struct iio_trigger
, subirq_chip
);
359 trig
->subirqs
[d
->irq
- trig
->subirq_base
].enabled
= false;
362 static void iio_trig_subirqunmask(struct irq_data
*d
)
364 struct irq_chip
*chip
= irq_data_get_irq_chip(d
);
365 struct iio_trigger
*trig
367 struct iio_trigger
, subirq_chip
);
368 trig
->subirqs
[d
->irq
- trig
->subirq_base
].enabled
= true;
371 struct iio_trigger
*iio_allocate_trigger_named(const char *name
)
373 struct iio_trigger
*trig
;
374 trig
= kzalloc(sizeof *trig
, GFP_KERNEL
);
377 trig
->dev
.type
= &iio_trig_type
;
378 trig
->dev
.bus
= &iio_bus_type
;
379 device_initialize(&trig
->dev
);
380 dev_set_drvdata(&trig
->dev
, (void *)trig
);
383 mutex_init(&trig
->pool_lock
);
385 = irq_alloc_descs(-1, 0,
386 CONFIG_IIO_CONSUMERS_PER_TRIGGER
,
388 if (trig
->subirq_base
< 0) {
393 trig
->subirq_chip
.name
= name
;
394 trig
->subirq_chip
.irq_mask
= &iio_trig_subirqmask
;
395 trig
->subirq_chip
.irq_unmask
= &iio_trig_subirqunmask
;
396 for (i
= 0; i
< CONFIG_IIO_CONSUMERS_PER_TRIGGER
; i
++) {
397 irq_set_chip(trig
->subirq_base
+ i
,
399 irq_set_handler(trig
->subirq_base
+ i
,
401 irq_modify_status(trig
->subirq_base
+ i
,
402 IRQ_NOREQUEST
| IRQ_NOAUTOEN
,
410 EXPORT_SYMBOL(iio_allocate_trigger_named
);
412 struct iio_trigger
*iio_allocate_trigger(void)
414 return iio_allocate_trigger_named(NULL
);
416 EXPORT_SYMBOL(iio_allocate_trigger
);
418 void iio_free_trigger(struct iio_trigger
*trig
)
421 put_device(&trig
->dev
);
423 EXPORT_SYMBOL(iio_free_trigger
);
425 int iio_device_register_trigger_consumer(struct iio_dev
*dev_info
)
428 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
429 &iio_trigger_consumer_attr_group
);
432 EXPORT_SYMBOL(iio_device_register_trigger_consumer
);
434 int iio_device_unregister_trigger_consumer(struct iio_dev
*dev_info
)
436 sysfs_remove_group(&dev_info
->dev
.kobj
,
437 &iio_trigger_consumer_attr_group
);
440 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer
);
442 int iio_triggered_ring_postenable(struct iio_dev
*indio_dev
)
444 return indio_dev
->trig
445 ? iio_trigger_attach_poll_func(indio_dev
->trig
,
449 EXPORT_SYMBOL(iio_triggered_ring_postenable
);
451 int iio_triggered_ring_predisable(struct iio_dev
*indio_dev
)
453 return indio_dev
->trig
454 ? iio_trigger_dettach_poll_func(indio_dev
->trig
,
458 EXPORT_SYMBOL(iio_triggered_ring_predisable
);