1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core, trigger handling functions
4 * Copyright (c) 2008 Jonathan Cameron
7 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/list.h>
13 #include <linux/slab.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/iio-opaque.h>
17 #include <linux/iio/trigger.h>
19 #include "iio_core_trigger.h"
20 #include <linux/iio/trigger_consumer.h>
22 /* RFC - Question of approach
23 * Make the common case (single sensor single trigger)
24 * simple by starting trigger capture from when first sensors
27 * Complex simultaneous start requires use of 'hold' functionality
28 * of the trigger. (not implemented)
30 * Any other suggestions?
33 static DEFINE_IDA(iio_trigger_ida);
35 /* Single list of all available triggers */
36 static LIST_HEAD(iio_trigger_list);
37 static DEFINE_MUTEX(iio_trigger_list_lock);
40 * iio_trigger_read_name() - retrieve useful identifying name
41 * @dev: device associated with the iio_trigger
42 * @attr: pointer to the device_attribute structure that is
44 * @buf: buffer to print the name into
46 * Return: a negative number on failure or the number of written
47 * characters on success.
49 static ssize_t iio_trigger_read_name(struct device *dev,
50 struct device_attribute *attr,
53 struct iio_trigger *trig = to_iio_trigger(dev);
54 return sysfs_emit(buf, "%s\n", trig->name);
57 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
59 static struct attribute *iio_trig_dev_attrs[] = {
63 ATTRIBUTE_GROUPS(iio_trig_dev);
65 static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
67 int __iio_trigger_register(struct iio_trigger *trig_info,
68 struct module *this_mod)
72 trig_info->owner = this_mod;
74 trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
75 if (trig_info->id < 0)
78 /* Set the name used for the sysfs directory etc */
79 dev_set_name(&trig_info->dev, "trigger%d", trig_info->id);
81 ret = device_add(&trig_info->dev);
83 goto error_unregister_id;
85 /* Add to list of available triggers held by the IIO core */
86 mutex_lock(&iio_trigger_list_lock);
87 if (__iio_trigger_find_by_name(trig_info->name)) {
88 pr_err("Duplicate trigger name '%s'\n", trig_info->name);
90 goto error_device_del;
92 list_add_tail(&trig_info->list, &iio_trigger_list);
93 mutex_unlock(&iio_trigger_list_lock);
98 mutex_unlock(&iio_trigger_list_lock);
99 device_del(&trig_info->dev);
101 ida_simple_remove(&iio_trigger_ida, trig_info->id);
104 EXPORT_SYMBOL(__iio_trigger_register);
106 void iio_trigger_unregister(struct iio_trigger *trig_info)
108 mutex_lock(&iio_trigger_list_lock);
109 list_del(&trig_info->list);
110 mutex_unlock(&iio_trigger_list_lock);
112 ida_simple_remove(&iio_trigger_ida, trig_info->id);
113 /* Possible issue in here */
114 device_del(&trig_info->dev);
116 EXPORT_SYMBOL(iio_trigger_unregister);
118 int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
120 struct iio_dev_opaque *iio_dev_opaque;
122 if (!indio_dev || !trig)
125 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
126 mutex_lock(&indio_dev->mlock);
127 WARN_ON(iio_dev_opaque->trig_readonly);
129 indio_dev->trig = iio_trigger_get(trig);
130 iio_dev_opaque->trig_readonly = true;
131 mutex_unlock(&indio_dev->mlock);
135 EXPORT_SYMBOL(iio_trigger_set_immutable);
137 /* Search for trigger by name, assuming iio_trigger_list_lock held */
138 static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
140 struct iio_trigger *iter;
142 list_for_each_entry(iter, &iio_trigger_list, list)
143 if (!strcmp(iter->name, name))
149 static struct iio_trigger *iio_trigger_acquire_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)) {
157 iio_trigger_get(trig);
160 mutex_unlock(&iio_trigger_list_lock);
165 static void iio_reenable_work_fn(struct work_struct *work)
167 struct iio_trigger *trig = container_of(work, struct iio_trigger,
171 * This 'might' occur after the trigger state is set to disabled -
172 * in that case the driver should skip reenabling.
174 trig->ops->reenable(trig);
178 * In general, reenable callbacks may need to sleep and this path is
179 * not performance sensitive, so just queue up a work item
180 * to reneable the trigger for us.
182 * Races that can cause this.
183 * 1) A handler occurs entirely in interrupt context so the counter
184 * the final decrement is still in this interrupt.
185 * 2) The trigger has been removed, but one last interrupt gets through.
187 * For (1) we must call reenable, but not in atomic context.
188 * For (2) it should be safe to call reenanble, if drivers never blindly
189 * reenable after state is off.
191 static void iio_trigger_notify_done_atomic(struct iio_trigger *trig)
193 if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
195 schedule_work(&trig->reenable_work);
198 void iio_trigger_poll(struct iio_trigger *trig)
202 if (!atomic_read(&trig->use_count)) {
203 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
205 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
206 if (trig->subirqs[i].enabled)
207 generic_handle_irq(trig->subirq_base + i);
209 iio_trigger_notify_done_atomic(trig);
213 EXPORT_SYMBOL(iio_trigger_poll);
215 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
217 iio_trigger_poll(private);
220 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
222 void iio_trigger_poll_chained(struct iio_trigger *trig)
226 if (!atomic_read(&trig->use_count)) {
227 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
229 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
230 if (trig->subirqs[i].enabled)
231 handle_nested_irq(trig->subirq_base + i);
233 iio_trigger_notify_done(trig);
237 EXPORT_SYMBOL(iio_trigger_poll_chained);
239 void iio_trigger_notify_done(struct iio_trigger *trig)
241 if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
243 trig->ops->reenable(trig);
245 EXPORT_SYMBOL(iio_trigger_notify_done);
247 /* Trigger Consumer related functions */
248 static int iio_trigger_get_irq(struct iio_trigger *trig)
252 mutex_lock(&trig->pool_lock);
253 ret = bitmap_find_free_region(trig->pool,
254 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
256 mutex_unlock(&trig->pool_lock);
258 ret += trig->subirq_base;
263 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
265 mutex_lock(&trig->pool_lock);
266 clear_bit(irq - trig->subirq_base, trig->pool);
267 mutex_unlock(&trig->pool_lock);
270 /* Complexity in here. With certain triggers (datardy) an acknowledgement
271 * may be needed if the pollfuncs do not include the data read for the
273 * This is not currently handled. Alternative of not enabling trigger unless
274 * the relevant function is in there may be the best option.
276 /* Worth protecting against double additions? */
277 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
278 struct iio_poll_func *pf)
280 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
282 bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
285 /* Prevent the module from being removed whilst attached to a trigger */
286 __module_get(iio_dev_opaque->driver_module);
289 pf->irq = iio_trigger_get_irq(trig);
291 pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
292 trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
297 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
303 /* Enable trigger in driver */
304 if (trig->ops && trig->ops->set_trigger_state && notinuse) {
305 ret = trig->ops->set_trigger_state(trig, true);
311 * Check if we just registered to our own trigger: we determine that
312 * this is the case if the IIO device and the trigger device share the
313 * same parent device.
315 if (pf->indio_dev->dev.parent == trig->dev.parent)
316 trig->attached_own_device = true;
321 free_irq(pf->irq, pf);
323 iio_trigger_put_irq(trig, pf->irq);
325 module_put(iio_dev_opaque->driver_module);
329 int iio_trigger_detach_poll_func(struct iio_trigger *trig,
330 struct iio_poll_func *pf)
332 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
333 bool no_other_users =
334 bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1;
337 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
338 ret = trig->ops->set_trigger_state(trig, false);
342 if (pf->indio_dev->dev.parent == trig->dev.parent)
343 trig->attached_own_device = false;
344 iio_trigger_put_irq(trig, pf->irq);
345 free_irq(pf->irq, pf);
346 module_put(iio_dev_opaque->driver_module);
351 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
353 struct iio_poll_func *pf = p;
355 pf->timestamp = iio_get_time_ns(pf->indio_dev);
356 return IRQ_WAKE_THREAD;
358 EXPORT_SYMBOL(iio_pollfunc_store_time);
361 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
362 irqreturn_t (*thread)(int irq, void *p),
364 struct iio_dev *indio_dev,
369 struct iio_poll_func *pf;
371 pf = kmalloc(sizeof *pf, GFP_KERNEL);
374 va_start(vargs, fmt);
375 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
377 if (pf->name == NULL) {
384 pf->indio_dev = indio_dev;
388 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
390 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
395 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
398 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
399 * @dev: device associated with an industrial I/O device
400 * @attr: pointer to the device_attribute structure that
402 * @buf: buffer where the current trigger name will be printed into
404 * For trigger consumers the current_trigger interface allows the trigger
405 * used by the device to be queried.
407 * Return: a negative number on failure, the number of characters written
408 * on success or 0 if no trigger is available
410 static ssize_t iio_trigger_read_current(struct device *dev,
411 struct device_attribute *attr,
414 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
417 return sysfs_emit(buf, "%s\n", indio_dev->trig->name);
422 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
423 * @dev: device associated with an industrial I/O device
424 * @attr: device attribute that is being processed
425 * @buf: string buffer that holds the name of the trigger
426 * @len: length of the trigger name held by buf
428 * For trigger consumers the current_trigger interface allows the trigger
429 * used for this device to be specified at run time based on the trigger's
432 * Return: negative error code on failure or length of the buffer
435 static ssize_t iio_trigger_write_current(struct device *dev,
436 struct device_attribute *attr,
440 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
441 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
442 struct iio_trigger *oldtrig = indio_dev->trig;
443 struct iio_trigger *trig;
446 mutex_lock(&indio_dev->mlock);
447 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
448 mutex_unlock(&indio_dev->mlock);
451 if (iio_dev_opaque->trig_readonly) {
452 mutex_unlock(&indio_dev->mlock);
455 mutex_unlock(&indio_dev->mlock);
457 trig = iio_trigger_acquire_by_name(buf);
458 if (oldtrig == trig) {
460 goto out_trigger_put;
463 if (trig && indio_dev->info->validate_trigger) {
464 ret = indio_dev->info->validate_trigger(indio_dev, trig);
466 goto out_trigger_put;
469 if (trig && trig->ops && trig->ops->validate_device) {
470 ret = trig->ops->validate_device(trig, indio_dev);
472 goto out_trigger_put;
475 indio_dev->trig = trig;
478 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
479 iio_trigger_detach_poll_func(oldtrig,
480 indio_dev->pollfunc_event);
481 iio_trigger_put(oldtrig);
483 if (indio_dev->trig) {
484 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
485 iio_trigger_attach_poll_func(indio_dev->trig,
486 indio_dev->pollfunc_event);
493 iio_trigger_put(trig);
497 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
498 iio_trigger_read_current,
499 iio_trigger_write_current);
501 static struct attribute *iio_trigger_consumer_attrs[] = {
502 &dev_attr_current_trigger.attr,
506 static const struct attribute_group iio_trigger_consumer_attr_group = {
508 .attrs = iio_trigger_consumer_attrs,
511 static void iio_trig_release(struct device *device)
513 struct iio_trigger *trig = to_iio_trigger(device);
516 if (trig->subirq_base) {
517 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
518 irq_modify_status(trig->subirq_base + i,
520 IRQ_NOREQUEST | IRQ_NOPROBE);
521 irq_set_chip(trig->subirq_base + i,
523 irq_set_handler(trig->subirq_base + i,
527 irq_free_descs(trig->subirq_base,
528 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
534 static const struct device_type iio_trig_type = {
535 .release = iio_trig_release,
536 .groups = iio_trig_dev_groups,
539 static void iio_trig_subirqmask(struct irq_data *d)
541 struct irq_chip *chip = irq_data_get_irq_chip(d);
542 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
544 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
547 static void iio_trig_subirqunmask(struct irq_data *d)
549 struct irq_chip *chip = irq_data_get_irq_chip(d);
550 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
552 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
555 static __printf(2, 0)
556 struct iio_trigger *viio_trigger_alloc(struct device *parent,
560 struct iio_trigger *trig;
563 trig = kzalloc(sizeof *trig, GFP_KERNEL);
567 trig->dev.parent = parent;
568 trig->dev.type = &iio_trig_type;
569 trig->dev.bus = &iio_bus_type;
570 device_initialize(&trig->dev);
571 INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
573 mutex_init(&trig->pool_lock);
574 trig->subirq_base = irq_alloc_descs(-1, 0,
575 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
577 if (trig->subirq_base < 0)
580 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
581 if (trig->name == NULL)
584 trig->subirq_chip.name = trig->name;
585 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
586 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
587 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
588 irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
589 irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
590 irq_modify_status(trig->subirq_base + i,
591 IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
597 irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
604 * iio_trigger_alloc - Allocate a trigger
605 * @parent: Device to allocate iio_trigger for
606 * @fmt: trigger name format. If it includes format
607 * specifiers, the additional arguments following
608 * format are formatted and inserted in the resulting
609 * string replacing their respective specifiers.
611 * Pointer to allocated iio_trigger on success, NULL on failure.
613 struct iio_trigger *iio_trigger_alloc(struct device *parent, const char *fmt, ...)
615 struct iio_trigger *trig;
618 va_start(vargs, fmt);
619 trig = viio_trigger_alloc(parent, fmt, vargs);
624 EXPORT_SYMBOL(iio_trigger_alloc);
626 void iio_trigger_free(struct iio_trigger *trig)
629 put_device(&trig->dev);
631 EXPORT_SYMBOL(iio_trigger_free);
633 static void devm_iio_trigger_release(struct device *dev, void *res)
635 iio_trigger_free(*(struct iio_trigger **)res);
639 * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
640 * Managed iio_trigger_alloc. iio_trigger allocated with this function is
641 * automatically freed on driver detach.
642 * @parent: Device to allocate iio_trigger for
643 * @fmt: trigger name format. If it includes format
644 * specifiers, the additional arguments following
645 * format are formatted and inserted in the resulting
646 * string replacing their respective specifiers.
650 * Pointer to allocated iio_trigger on success, NULL on failure.
652 struct iio_trigger *devm_iio_trigger_alloc(struct device *parent, const char *fmt, ...)
654 struct iio_trigger **ptr, *trig;
657 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
662 /* use raw alloc_dr for kmalloc caller tracing */
663 va_start(vargs, fmt);
664 trig = viio_trigger_alloc(parent, fmt, vargs);
668 devres_add(parent, ptr);
675 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
677 static void devm_iio_trigger_unreg(void *trigger_info)
679 iio_trigger_unregister(trigger_info);
683 * __devm_iio_trigger_register - Resource-managed iio_trigger_register()
684 * @dev: device this trigger was allocated for
685 * @trig_info: trigger to register
686 * @this_mod: module registering the trigger
688 * Managed iio_trigger_register(). The IIO trigger registered with this
689 * function is automatically unregistered on driver detach. This function
690 * calls iio_trigger_register() internally. Refer to that function for more
694 * 0 on success, negative error number on failure.
696 int __devm_iio_trigger_register(struct device *dev,
697 struct iio_trigger *trig_info,
698 struct module *this_mod)
702 ret = __iio_trigger_register(trig_info, this_mod);
706 return devm_add_action_or_reset(dev, devm_iio_trigger_unreg, trig_info);
708 EXPORT_SYMBOL_GPL(__devm_iio_trigger_register);
710 bool iio_trigger_using_own(struct iio_dev *indio_dev)
712 return indio_dev->trig->attached_own_device;
714 EXPORT_SYMBOL(iio_trigger_using_own);
717 * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
719 * @trig: The IIO trigger to check
720 * @indio_dev: the IIO device to check
722 * This function can be used as the validate_device callback for triggers that
723 * can only be attached to their own device.
725 * Return: 0 if both the trigger and the IIO device belong to the same
726 * device, -EINVAL otherwise.
728 int iio_trigger_validate_own_device(struct iio_trigger *trig,
729 struct iio_dev *indio_dev)
731 if (indio_dev->dev.parent != trig->dev.parent)
735 EXPORT_SYMBOL(iio_trigger_validate_own_device);
737 int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
739 return iio_device_register_sysfs_group(indio_dev,
740 &iio_trigger_consumer_attr_group);
743 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
745 /* Clean up an associated but not attached trigger reference */
747 iio_trigger_put(indio_dev->trig);