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 static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
69 int iio_trigger_register(struct iio_trigger *trig_info)
73 /* trig_info->ops is required for the module member */
77 trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
78 if (trig_info->id < 0)
81 /* Set the name used for the sysfs directory etc */
82 dev_set_name(&trig_info->dev, "trigger%ld",
83 (unsigned long) trig_info->id);
85 ret = device_add(&trig_info->dev);
87 goto error_unregister_id;
89 /* Add to list of available triggers held by the IIO core */
90 mutex_lock(&iio_trigger_list_lock);
91 if (__iio_trigger_find_by_name(trig_info->name)) {
92 pr_err("Duplicate trigger name '%s'\n", trig_info->name);
94 goto error_device_del;
96 list_add_tail(&trig_info->list, &iio_trigger_list);
97 mutex_unlock(&iio_trigger_list_lock);
102 mutex_unlock(&iio_trigger_list_lock);
103 device_del(&trig_info->dev);
105 ida_simple_remove(&iio_trigger_ida, trig_info->id);
108 EXPORT_SYMBOL(iio_trigger_register);
110 void iio_trigger_unregister(struct iio_trigger *trig_info)
112 mutex_lock(&iio_trigger_list_lock);
113 list_del(&trig_info->list);
114 mutex_unlock(&iio_trigger_list_lock);
116 ida_simple_remove(&iio_trigger_ida, trig_info->id);
117 /* Possible issue in here */
118 device_del(&trig_info->dev);
120 EXPORT_SYMBOL(iio_trigger_unregister);
122 /* Search for trigger by name, assuming iio_trigger_list_lock held */
123 static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
125 struct iio_trigger *iter;
127 list_for_each_entry(iter, &iio_trigger_list, list)
128 if (!strcmp(iter->name, name))
134 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
137 struct iio_trigger *trig = NULL, *iter;
139 mutex_lock(&iio_trigger_list_lock);
140 list_for_each_entry(iter, &iio_trigger_list, list)
141 if (sysfs_streq(iter->name, name)) {
145 mutex_unlock(&iio_trigger_list_lock);
150 void iio_trigger_poll(struct iio_trigger *trig)
154 if (!atomic_read(&trig->use_count)) {
155 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
157 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
158 if (trig->subirqs[i].enabled)
159 generic_handle_irq(trig->subirq_base + i);
161 iio_trigger_notify_done(trig);
165 EXPORT_SYMBOL(iio_trigger_poll);
167 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
169 iio_trigger_poll(private);
172 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
174 void iio_trigger_poll_chained(struct iio_trigger *trig)
178 if (!atomic_read(&trig->use_count)) {
179 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
181 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
182 if (trig->subirqs[i].enabled)
183 handle_nested_irq(trig->subirq_base + i);
185 iio_trigger_notify_done(trig);
189 EXPORT_SYMBOL(iio_trigger_poll_chained);
191 void iio_trigger_notify_done(struct iio_trigger *trig)
193 if (atomic_dec_and_test(&trig->use_count) && trig->ops->try_reenable)
194 if (trig->ops->try_reenable(trig))
195 /* Missed an interrupt so launch new poll now */
196 iio_trigger_poll(trig);
198 EXPORT_SYMBOL(iio_trigger_notify_done);
200 /* Trigger Consumer related functions */
201 static int iio_trigger_get_irq(struct iio_trigger *trig)
204 mutex_lock(&trig->pool_lock);
205 ret = bitmap_find_free_region(trig->pool,
206 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
208 mutex_unlock(&trig->pool_lock);
210 ret += trig->subirq_base;
215 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
217 mutex_lock(&trig->pool_lock);
218 clear_bit(irq - trig->subirq_base, trig->pool);
219 mutex_unlock(&trig->pool_lock);
222 /* Complexity in here. With certain triggers (datardy) an acknowledgement
223 * may be needed if the pollfuncs do not include the data read for the
225 * This is not currently handled. Alternative of not enabling trigger unless
226 * the relevant function is in there may be the best option.
228 /* Worth protecting against double additions? */
229 static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
230 struct iio_poll_func *pf)
234 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
236 /* Prevent the module from being removed whilst attached to a trigger */
237 __module_get(pf->indio_dev->info->driver_module);
240 pf->irq = iio_trigger_get_irq(trig);
245 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
251 /* Enable trigger in driver */
252 if (trig->ops->set_trigger_state && notinuse) {
253 ret = trig->ops->set_trigger_state(trig, true);
261 free_irq(pf->irq, pf);
263 iio_trigger_put_irq(trig, pf->irq);
265 module_put(pf->indio_dev->info->driver_module);
269 static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
270 struct iio_poll_func *pf)
274 = (bitmap_weight(trig->pool,
275 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
277 if (trig->ops->set_trigger_state && no_other_users) {
278 ret = trig->ops->set_trigger_state(trig, false);
282 iio_trigger_put_irq(trig, pf->irq);
283 free_irq(pf->irq, pf);
284 module_put(pf->indio_dev->info->driver_module);
289 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
291 struct iio_poll_func *pf = p;
292 pf->timestamp = iio_get_time_ns(pf->indio_dev);
293 return IRQ_WAKE_THREAD;
295 EXPORT_SYMBOL(iio_pollfunc_store_time);
298 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
299 irqreturn_t (*thread)(int irq, void *p),
301 struct iio_dev *indio_dev,
306 struct iio_poll_func *pf;
308 pf = kmalloc(sizeof *pf, GFP_KERNEL);
311 va_start(vargs, fmt);
312 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
314 if (pf->name == NULL) {
321 pf->indio_dev = indio_dev;
325 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
327 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
332 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
335 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
336 * @dev: device associated with an industrial I/O device
337 * @attr: pointer to the device_attribute structure that
339 * @buf: buffer where the current trigger name will be printed into
341 * For trigger consumers the current_trigger interface allows the trigger
342 * used by the device to be queried.
344 * Return: a negative number on failure, the number of characters written
345 * on success or 0 if no trigger is available
347 static ssize_t iio_trigger_read_current(struct device *dev,
348 struct device_attribute *attr,
351 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
354 return sprintf(buf, "%s\n", indio_dev->trig->name);
359 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
360 * @dev: device associated with an industrial I/O device
361 * @attr: device attribute that is being processed
362 * @buf: string buffer that holds the name of the trigger
363 * @len: length of the trigger name held by buf
365 * For trigger consumers the current_trigger interface allows the trigger
366 * used for this device to be specified at run time based on the trigger's
369 * Return: negative error code on failure or length of the buffer
372 static ssize_t iio_trigger_write_current(struct device *dev,
373 struct device_attribute *attr,
377 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
378 struct iio_trigger *oldtrig = indio_dev->trig;
379 struct iio_trigger *trig;
382 mutex_lock(&indio_dev->mlock);
383 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
384 mutex_unlock(&indio_dev->mlock);
387 mutex_unlock(&indio_dev->mlock);
389 trig = iio_trigger_find_by_name(buf, len);
393 if (trig && indio_dev->info->validate_trigger) {
394 ret = indio_dev->info->validate_trigger(indio_dev, trig);
399 if (trig && trig->ops->validate_device) {
400 ret = trig->ops->validate_device(trig, indio_dev);
405 indio_dev->trig = trig;
408 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
409 iio_trigger_detach_poll_func(oldtrig,
410 indio_dev->pollfunc_event);
411 iio_trigger_put(oldtrig);
413 if (indio_dev->trig) {
414 iio_trigger_get(indio_dev->trig);
415 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
416 iio_trigger_attach_poll_func(indio_dev->trig,
417 indio_dev->pollfunc_event);
423 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
424 iio_trigger_read_current,
425 iio_trigger_write_current);
427 static struct attribute *iio_trigger_consumer_attrs[] = {
428 &dev_attr_current_trigger.attr,
432 static const struct attribute_group iio_trigger_consumer_attr_group = {
434 .attrs = iio_trigger_consumer_attrs,
437 static void iio_trig_release(struct device *device)
439 struct iio_trigger *trig = to_iio_trigger(device);
442 if (trig->subirq_base) {
443 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
444 irq_modify_status(trig->subirq_base + i,
446 IRQ_NOREQUEST | IRQ_NOPROBE);
447 irq_set_chip(trig->subirq_base + i,
449 irq_set_handler(trig->subirq_base + i,
453 irq_free_descs(trig->subirq_base,
454 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
460 static struct device_type iio_trig_type = {
461 .release = iio_trig_release,
462 .groups = iio_trig_dev_groups,
465 static void iio_trig_subirqmask(struct irq_data *d)
467 struct irq_chip *chip = irq_data_get_irq_chip(d);
468 struct iio_trigger *trig
470 struct iio_trigger, subirq_chip);
471 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
474 static void iio_trig_subirqunmask(struct irq_data *d)
476 struct irq_chip *chip = irq_data_get_irq_chip(d);
477 struct iio_trigger *trig
479 struct iio_trigger, subirq_chip);
480 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
483 static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
485 struct iio_trigger *trig;
486 trig = kzalloc(sizeof *trig, GFP_KERNEL);
489 trig->dev.type = &iio_trig_type;
490 trig->dev.bus = &iio_bus_type;
491 device_initialize(&trig->dev);
493 mutex_init(&trig->pool_lock);
495 = irq_alloc_descs(-1, 0,
496 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
498 if (trig->subirq_base < 0) {
503 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
504 if (trig->name == NULL) {
505 irq_free_descs(trig->subirq_base,
506 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
510 trig->subirq_chip.name = trig->name;
511 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
512 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
513 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
514 irq_set_chip(trig->subirq_base + i,
516 irq_set_handler(trig->subirq_base + i,
518 irq_modify_status(trig->subirq_base + i,
519 IRQ_NOREQUEST | IRQ_NOAUTOEN,
522 get_device(&trig->dev);
528 struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
530 struct iio_trigger *trig;
533 va_start(vargs, fmt);
534 trig = viio_trigger_alloc(fmt, vargs);
539 EXPORT_SYMBOL(iio_trigger_alloc);
541 void iio_trigger_free(struct iio_trigger *trig)
544 put_device(&trig->dev);
546 EXPORT_SYMBOL(iio_trigger_free);
548 static void devm_iio_trigger_release(struct device *dev, void *res)
550 iio_trigger_free(*(struct iio_trigger **)res);
553 static int devm_iio_trigger_match(struct device *dev, void *res, void *data)
555 struct iio_trigger **r = res;
566 * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
567 * @dev: Device to allocate iio_trigger for
568 * @fmt: trigger name format. If it includes format
569 * specifiers, the additional arguments following
570 * format are formatted and inserted in the resulting
571 * string replacing their respective specifiers.
573 * Managed iio_trigger_alloc. iio_trigger allocated with this function is
574 * automatically freed on driver detach.
576 * If an iio_trigger allocated with this function needs to be freed separately,
577 * devm_iio_trigger_free() must be used.
580 * Pointer to allocated iio_trigger on success, NULL on failure.
582 struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
583 const char *fmt, ...)
585 struct iio_trigger **ptr, *trig;
588 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
593 /* use raw alloc_dr for kmalloc caller tracing */
594 va_start(vargs, fmt);
595 trig = viio_trigger_alloc(fmt, vargs);
599 devres_add(dev, ptr);
606 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
609 * devm_iio_trigger_free - Resource-managed iio_trigger_free()
610 * @dev: Device this iio_dev belongs to
611 * @iio_trig: the iio_trigger associated with the device
613 * Free iio_trigger allocated with devm_iio_trigger_alloc().
615 void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
619 rc = devres_release(dev, devm_iio_trigger_release,
620 devm_iio_trigger_match, iio_trig);
623 EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
625 void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
627 indio_dev->groups[indio_dev->groupcounter++] =
628 &iio_trigger_consumer_attr_group;
631 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
633 /* Clean up an associated but not attached trigger reference */
635 iio_trigger_put(indio_dev->trig);
638 int iio_triggered_buffer_postenable(struct iio_dev *indio_dev)
640 return iio_trigger_attach_poll_func(indio_dev->trig,
641 indio_dev->pollfunc);
643 EXPORT_SYMBOL(iio_triggered_buffer_postenable);
645 int iio_triggered_buffer_predisable(struct iio_dev *indio_dev)
647 return iio_trigger_detach_poll_func(indio_dev->trig,
648 indio_dev->pollfunc);
650 EXPORT_SYMBOL(iio_triggered_buffer_predisable);