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 * name_show() - 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 name_show(struct device *dev, struct device_attribute *attr,
52 struct iio_trigger *trig = to_iio_trigger(dev);
53 return sysfs_emit(buf, "%s\n", trig->name);
56 static DEVICE_ATTR_RO(name);
58 static struct attribute *iio_trig_dev_attrs[] = {
62 ATTRIBUTE_GROUPS(iio_trig_dev);
64 static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
66 int iio_trigger_register(struct iio_trigger *trig_info)
70 trig_info->id = ida_alloc(&iio_trigger_ida, GFP_KERNEL);
71 if (trig_info->id < 0)
74 /* Set the name used for the sysfs directory etc */
75 dev_set_name(&trig_info->dev, "trigger%d", trig_info->id);
77 ret = device_add(&trig_info->dev);
79 goto error_unregister_id;
81 /* Add to list of available triggers held by the IIO core */
82 mutex_lock(&iio_trigger_list_lock);
83 if (__iio_trigger_find_by_name(trig_info->name)) {
84 pr_err("Duplicate trigger name '%s'\n", trig_info->name);
86 goto error_device_del;
88 list_add_tail(&trig_info->list, &iio_trigger_list);
89 mutex_unlock(&iio_trigger_list_lock);
94 mutex_unlock(&iio_trigger_list_lock);
95 device_del(&trig_info->dev);
97 ida_free(&iio_trigger_ida, trig_info->id);
100 EXPORT_SYMBOL(iio_trigger_register);
102 void iio_trigger_unregister(struct iio_trigger *trig_info)
104 mutex_lock(&iio_trigger_list_lock);
105 list_del(&trig_info->list);
106 mutex_unlock(&iio_trigger_list_lock);
108 ida_free(&iio_trigger_ida, trig_info->id);
109 /* Possible issue in here */
110 device_del(&trig_info->dev);
112 EXPORT_SYMBOL(iio_trigger_unregister);
114 int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
116 struct iio_dev_opaque *iio_dev_opaque;
118 if (!indio_dev || !trig)
121 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
122 mutex_lock(&indio_dev->mlock);
123 WARN_ON(iio_dev_opaque->trig_readonly);
125 indio_dev->trig = iio_trigger_get(trig);
126 iio_dev_opaque->trig_readonly = true;
127 mutex_unlock(&indio_dev->mlock);
131 EXPORT_SYMBOL(iio_trigger_set_immutable);
133 /* Search for trigger by name, assuming iio_trigger_list_lock held */
134 static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
136 struct iio_trigger *iter;
138 list_for_each_entry(iter, &iio_trigger_list, list)
139 if (!strcmp(iter->name, name))
145 static struct iio_trigger *iio_trigger_acquire_by_name(const char *name)
147 struct iio_trigger *trig = NULL, *iter;
149 mutex_lock(&iio_trigger_list_lock);
150 list_for_each_entry(iter, &iio_trigger_list, list)
151 if (sysfs_streq(iter->name, name)) {
153 iio_trigger_get(trig);
156 mutex_unlock(&iio_trigger_list_lock);
161 static void iio_reenable_work_fn(struct work_struct *work)
163 struct iio_trigger *trig = container_of(work, struct iio_trigger,
167 * This 'might' occur after the trigger state is set to disabled -
168 * in that case the driver should skip reenabling.
170 trig->ops->reenable(trig);
174 * In general, reenable callbacks may need to sleep and this path is
175 * not performance sensitive, so just queue up a work item
176 * to reneable the trigger for us.
178 * Races that can cause this.
179 * 1) A handler occurs entirely in interrupt context so the counter
180 * the final decrement is still in this interrupt.
181 * 2) The trigger has been removed, but one last interrupt gets through.
183 * For (1) we must call reenable, but not in atomic context.
184 * For (2) it should be safe to call reenanble, if drivers never blindly
185 * reenable after state is off.
187 static void iio_trigger_notify_done_atomic(struct iio_trigger *trig)
189 if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
191 schedule_work(&trig->reenable_work);
194 void iio_trigger_poll(struct iio_trigger *trig)
198 if (!atomic_read(&trig->use_count)) {
199 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
201 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
202 if (trig->subirqs[i].enabled)
203 generic_handle_irq(trig->subirq_base + i);
205 iio_trigger_notify_done_atomic(trig);
209 EXPORT_SYMBOL(iio_trigger_poll);
211 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
213 iio_trigger_poll(private);
216 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
218 void iio_trigger_poll_chained(struct iio_trigger *trig)
222 if (!atomic_read(&trig->use_count)) {
223 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
225 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
226 if (trig->subirqs[i].enabled)
227 handle_nested_irq(trig->subirq_base + i);
229 iio_trigger_notify_done(trig);
233 EXPORT_SYMBOL(iio_trigger_poll_chained);
235 void iio_trigger_notify_done(struct iio_trigger *trig)
237 if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
239 trig->ops->reenable(trig);
241 EXPORT_SYMBOL(iio_trigger_notify_done);
243 /* Trigger Consumer related functions */
244 static int iio_trigger_get_irq(struct iio_trigger *trig)
248 mutex_lock(&trig->pool_lock);
249 ret = bitmap_find_free_region(trig->pool,
250 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
252 mutex_unlock(&trig->pool_lock);
254 ret += trig->subirq_base;
259 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
261 mutex_lock(&trig->pool_lock);
262 clear_bit(irq - trig->subirq_base, trig->pool);
263 mutex_unlock(&trig->pool_lock);
266 /* Complexity in here. With certain triggers (datardy) an acknowledgement
267 * may be needed if the pollfuncs do not include the data read for the
269 * This is not currently handled. Alternative of not enabling trigger unless
270 * the relevant function is in there may be the best option.
272 /* Worth protecting against double additions? */
273 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
274 struct iio_poll_func *pf)
276 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
278 bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
281 /* Prevent the module from being removed whilst attached to a trigger */
282 __module_get(iio_dev_opaque->driver_module);
285 pf->irq = iio_trigger_get_irq(trig);
287 pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
288 trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
293 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
299 /* Enable trigger in driver */
300 if (trig->ops && trig->ops->set_trigger_state && notinuse) {
301 ret = trig->ops->set_trigger_state(trig, true);
307 * Check if we just registered to our own trigger: we determine that
308 * this is the case if the IIO device and the trigger device share the
309 * same parent device.
311 if (pf->indio_dev->dev.parent == trig->dev.parent)
312 trig->attached_own_device = true;
317 free_irq(pf->irq, pf);
319 iio_trigger_put_irq(trig, pf->irq);
321 module_put(iio_dev_opaque->driver_module);
325 int iio_trigger_detach_poll_func(struct iio_trigger *trig,
326 struct iio_poll_func *pf)
328 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
329 bool no_other_users =
330 bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1;
333 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
334 ret = trig->ops->set_trigger_state(trig, false);
338 if (pf->indio_dev->dev.parent == trig->dev.parent)
339 trig->attached_own_device = false;
340 iio_trigger_put_irq(trig, pf->irq);
341 free_irq(pf->irq, pf);
342 module_put(iio_dev_opaque->driver_module);
347 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
349 struct iio_poll_func *pf = p;
351 pf->timestamp = iio_get_time_ns(pf->indio_dev);
352 return IRQ_WAKE_THREAD;
354 EXPORT_SYMBOL(iio_pollfunc_store_time);
357 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
358 irqreturn_t (*thread)(int irq, void *p),
360 struct iio_dev *indio_dev,
365 struct iio_poll_func *pf;
367 pf = kmalloc(sizeof(*pf), GFP_KERNEL);
370 va_start(vargs, fmt);
371 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
373 if (pf->name == NULL) {
380 pf->indio_dev = indio_dev;
384 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
386 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
391 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
394 * current_trigger_show() - trigger consumer sysfs query current trigger
395 * @dev: device associated with an industrial I/O device
396 * @attr: pointer to the device_attribute structure that
398 * @buf: buffer where the current trigger name will be printed into
400 * For trigger consumers the current_trigger interface allows the trigger
401 * used by the device to be queried.
403 * Return: a negative number on failure, the number of characters written
404 * on success or 0 if no trigger is available
406 static ssize_t current_trigger_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
409 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
412 return sysfs_emit(buf, "%s\n", indio_dev->trig->name);
417 * current_trigger_store() - trigger consumer sysfs set current trigger
418 * @dev: device associated with an industrial I/O device
419 * @attr: device attribute that is being processed
420 * @buf: string buffer that holds the name of the trigger
421 * @len: length of the trigger name held by buf
423 * For trigger consumers the current_trigger interface allows the trigger
424 * used for this device to be specified at run time based on the trigger's
427 * Return: negative error code on failure or length of the buffer
430 static ssize_t current_trigger_store(struct device *dev,
431 struct device_attribute *attr,
432 const char *buf, size_t len)
434 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
435 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
436 struct iio_trigger *oldtrig = indio_dev->trig;
437 struct iio_trigger *trig;
440 mutex_lock(&indio_dev->mlock);
441 if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
442 mutex_unlock(&indio_dev->mlock);
445 if (iio_dev_opaque->trig_readonly) {
446 mutex_unlock(&indio_dev->mlock);
449 mutex_unlock(&indio_dev->mlock);
451 trig = iio_trigger_acquire_by_name(buf);
452 if (oldtrig == trig) {
454 goto out_trigger_put;
457 if (trig && indio_dev->info->validate_trigger) {
458 ret = indio_dev->info->validate_trigger(indio_dev, trig);
460 goto out_trigger_put;
463 if (trig && trig->ops && trig->ops->validate_device) {
464 ret = trig->ops->validate_device(trig, indio_dev);
466 goto out_trigger_put;
469 indio_dev->trig = trig;
472 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
473 iio_trigger_detach_poll_func(oldtrig,
474 indio_dev->pollfunc_event);
475 iio_trigger_put(oldtrig);
477 if (indio_dev->trig) {
478 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
479 iio_trigger_attach_poll_func(indio_dev->trig,
480 indio_dev->pollfunc_event);
487 iio_trigger_put(trig);
491 static DEVICE_ATTR_RW(current_trigger);
493 static struct attribute *iio_trigger_consumer_attrs[] = {
494 &dev_attr_current_trigger.attr,
498 static const struct attribute_group iio_trigger_consumer_attr_group = {
500 .attrs = iio_trigger_consumer_attrs,
503 static void iio_trig_release(struct device *device)
505 struct iio_trigger *trig = to_iio_trigger(device);
508 if (trig->subirq_base) {
509 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
510 irq_modify_status(trig->subirq_base + i,
512 IRQ_NOREQUEST | IRQ_NOPROBE);
513 irq_set_chip(trig->subirq_base + i,
515 irq_set_handler(trig->subirq_base + i,
519 irq_free_descs(trig->subirq_base,
520 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
526 static const struct device_type iio_trig_type = {
527 .release = iio_trig_release,
528 .groups = iio_trig_dev_groups,
531 static void iio_trig_subirqmask(struct irq_data *d)
533 struct irq_chip *chip = irq_data_get_irq_chip(d);
534 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
536 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
539 static void iio_trig_subirqunmask(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 = true;
547 static __printf(3, 0)
548 struct iio_trigger *viio_trigger_alloc(struct device *parent,
549 struct module *this_mod,
553 struct iio_trigger *trig;
556 trig = kzalloc(sizeof(*trig), GFP_KERNEL);
560 trig->dev.parent = parent;
561 trig->dev.type = &iio_trig_type;
562 trig->dev.bus = &iio_bus_type;
563 device_initialize(&trig->dev);
564 INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
566 mutex_init(&trig->pool_lock);
567 trig->subirq_base = irq_alloc_descs(-1, 0,
568 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
570 if (trig->subirq_base < 0)
573 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
574 if (trig->name == NULL)
577 INIT_LIST_HEAD(&trig->list);
579 trig->owner = this_mod;
581 trig->subirq_chip.name = trig->name;
582 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
583 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
584 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
585 irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
586 irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
587 irq_modify_status(trig->subirq_base + i,
588 IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
594 irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
601 * __iio_trigger_alloc - Allocate a trigger
602 * @parent: Device to allocate iio_trigger for
603 * @this_mod: module allocating the trigger
604 * @fmt: trigger name format. If it includes format
605 * specifiers, the additional arguments following
606 * format are formatted and inserted in the resulting
607 * string replacing their respective specifiers.
609 * Pointer to allocated iio_trigger on success, NULL on failure.
611 struct iio_trigger *__iio_trigger_alloc(struct device *parent,
612 struct module *this_mod,
613 const char *fmt, ...)
615 struct iio_trigger *trig;
618 va_start(vargs, fmt);
619 trig = viio_trigger_alloc(parent, this_mod, 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 * @this_mod: module allocating the trigger
644 * @fmt: trigger name format. If it includes format
645 * specifiers, the additional arguments following
646 * format are formatted and inserted in the resulting
647 * string replacing their respective specifiers.
651 * Pointer to allocated iio_trigger on success, NULL on failure.
653 struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent,
654 struct module *this_mod,
655 const char *fmt, ...)
657 struct iio_trigger **ptr, *trig;
660 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
665 /* use raw alloc_dr for kmalloc caller tracing */
666 va_start(vargs, fmt);
667 trig = viio_trigger_alloc(parent, this_mod, fmt, vargs);
671 devres_add(parent, ptr);
678 EXPORT_SYMBOL_GPL(__devm_iio_trigger_alloc);
680 static void devm_iio_trigger_unreg(void *trigger_info)
682 iio_trigger_unregister(trigger_info);
686 * devm_iio_trigger_register - Resource-managed iio_trigger_register()
687 * @dev: device this trigger was allocated for
688 * @trig_info: trigger to register
690 * Managed iio_trigger_register(). The IIO trigger registered with this
691 * function is automatically unregistered on driver detach. This function
692 * calls iio_trigger_register() internally. Refer to that function for more
696 * 0 on success, negative error number on failure.
698 int devm_iio_trigger_register(struct device *dev,
699 struct iio_trigger *trig_info)
703 ret = iio_trigger_register(trig_info);
707 return devm_add_action_or_reset(dev, devm_iio_trigger_unreg, trig_info);
709 EXPORT_SYMBOL_GPL(devm_iio_trigger_register);
711 bool iio_trigger_using_own(struct iio_dev *indio_dev)
713 return indio_dev->trig->attached_own_device;
715 EXPORT_SYMBOL(iio_trigger_using_own);
718 * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
720 * @trig: The IIO trigger to check
721 * @indio_dev: the IIO device to check
723 * This function can be used as the validate_device callback for triggers that
724 * can only be attached to their own device.
726 * Return: 0 if both the trigger and the IIO device belong to the same
727 * device, -EINVAL otherwise.
729 int iio_trigger_validate_own_device(struct iio_trigger *trig,
730 struct iio_dev *indio_dev)
732 if (indio_dev->dev.parent != trig->dev.parent)
736 EXPORT_SYMBOL(iio_trigger_validate_own_device);
738 int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
740 return iio_device_register_sysfs_group(indio_dev,
741 &iio_trigger_consumer_attr_group);
744 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
746 /* Clean up an associated but not attached trigger reference */
748 iio_trigger_put(indio_dev->trig);