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 void iio_trigger_poll(struct iio_trigger *trig)
169 if (!atomic_read(&trig->use_count)) {
170 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
172 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
173 if (trig->subirqs[i].enabled)
174 generic_handle_irq(trig->subirq_base + i);
176 iio_trigger_notify_done(trig);
180 EXPORT_SYMBOL(iio_trigger_poll);
182 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
184 iio_trigger_poll(private);
187 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
189 void iio_trigger_poll_chained(struct iio_trigger *trig)
193 if (!atomic_read(&trig->use_count)) {
194 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
196 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
197 if (trig->subirqs[i].enabled)
198 handle_nested_irq(trig->subirq_base + i);
200 iio_trigger_notify_done(trig);
204 EXPORT_SYMBOL(iio_trigger_poll_chained);
206 void iio_trigger_notify_done(struct iio_trigger *trig)
208 if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
210 trig->ops->reenable(trig);
212 EXPORT_SYMBOL(iio_trigger_notify_done);
214 /* Trigger Consumer related functions */
215 static int iio_trigger_get_irq(struct iio_trigger *trig)
219 mutex_lock(&trig->pool_lock);
220 ret = bitmap_find_free_region(trig->pool,
221 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
223 mutex_unlock(&trig->pool_lock);
225 ret += trig->subirq_base;
230 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
232 mutex_lock(&trig->pool_lock);
233 clear_bit(irq - trig->subirq_base, trig->pool);
234 mutex_unlock(&trig->pool_lock);
237 /* Complexity in here. With certain triggers (datardy) an acknowledgement
238 * may be needed if the pollfuncs do not include the data read for the
240 * This is not currently handled. Alternative of not enabling trigger unless
241 * the relevant function is in there may be the best option.
243 /* Worth protecting against double additions? */
244 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
245 struct iio_poll_func *pf)
247 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
249 bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
252 /* Prevent the module from being removed whilst attached to a trigger */
253 __module_get(iio_dev_opaque->driver_module);
256 pf->irq = iio_trigger_get_irq(trig);
258 pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
259 trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
264 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
270 /* Enable trigger in driver */
271 if (trig->ops && trig->ops->set_trigger_state && notinuse) {
272 ret = trig->ops->set_trigger_state(trig, true);
278 * Check if we just registered to our own trigger: we determine that
279 * this is the case if the IIO device and the trigger device share the
280 * same parent device.
282 if (pf->indio_dev->dev.parent == trig->dev.parent)
283 trig->attached_own_device = true;
288 free_irq(pf->irq, pf);
290 iio_trigger_put_irq(trig, pf->irq);
292 module_put(iio_dev_opaque->driver_module);
296 int iio_trigger_detach_poll_func(struct iio_trigger *trig,
297 struct iio_poll_func *pf)
299 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
300 bool no_other_users =
301 bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1;
304 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
305 ret = trig->ops->set_trigger_state(trig, false);
309 if (pf->indio_dev->dev.parent == trig->dev.parent)
310 trig->attached_own_device = false;
311 iio_trigger_put_irq(trig, pf->irq);
312 free_irq(pf->irq, pf);
313 module_put(iio_dev_opaque->driver_module);
318 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
320 struct iio_poll_func *pf = p;
322 pf->timestamp = iio_get_time_ns(pf->indio_dev);
323 return IRQ_WAKE_THREAD;
325 EXPORT_SYMBOL(iio_pollfunc_store_time);
328 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
329 irqreturn_t (*thread)(int irq, void *p),
331 struct iio_dev *indio_dev,
336 struct iio_poll_func *pf;
338 pf = kmalloc(sizeof *pf, GFP_KERNEL);
341 va_start(vargs, fmt);
342 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
344 if (pf->name == NULL) {
351 pf->indio_dev = indio_dev;
355 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
357 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
362 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
365 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
366 * @dev: device associated with an industrial I/O device
367 * @attr: pointer to the device_attribute structure that
369 * @buf: buffer where the current trigger name will be printed into
371 * For trigger consumers the current_trigger interface allows the trigger
372 * used by the device to be queried.
374 * Return: a negative number on failure, the number of characters written
375 * on success or 0 if no trigger is available
377 static ssize_t iio_trigger_read_current(struct device *dev,
378 struct device_attribute *attr,
381 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
384 return sysfs_emit(buf, "%s\n", indio_dev->trig->name);
389 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
390 * @dev: device associated with an industrial I/O device
391 * @attr: device attribute that is being processed
392 * @buf: string buffer that holds the name of the trigger
393 * @len: length of the trigger name held by buf
395 * For trigger consumers the current_trigger interface allows the trigger
396 * used for this device to be specified at run time based on the trigger's
399 * Return: negative error code on failure or length of the buffer
402 static ssize_t iio_trigger_write_current(struct device *dev,
403 struct device_attribute *attr,
407 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
408 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
409 struct iio_trigger *oldtrig = indio_dev->trig;
410 struct iio_trigger *trig;
413 mutex_lock(&indio_dev->mlock);
414 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
415 mutex_unlock(&indio_dev->mlock);
418 if (iio_dev_opaque->trig_readonly) {
419 mutex_unlock(&indio_dev->mlock);
422 mutex_unlock(&indio_dev->mlock);
424 trig = iio_trigger_acquire_by_name(buf);
425 if (oldtrig == trig) {
427 goto out_trigger_put;
430 if (trig && indio_dev->info->validate_trigger) {
431 ret = indio_dev->info->validate_trigger(indio_dev, trig);
433 goto out_trigger_put;
436 if (trig && trig->ops && trig->ops->validate_device) {
437 ret = trig->ops->validate_device(trig, indio_dev);
439 goto out_trigger_put;
442 indio_dev->trig = trig;
445 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
446 iio_trigger_detach_poll_func(oldtrig,
447 indio_dev->pollfunc_event);
448 iio_trigger_put(oldtrig);
450 if (indio_dev->trig) {
451 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
452 iio_trigger_attach_poll_func(indio_dev->trig,
453 indio_dev->pollfunc_event);
460 iio_trigger_put(trig);
464 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
465 iio_trigger_read_current,
466 iio_trigger_write_current);
468 static struct attribute *iio_trigger_consumer_attrs[] = {
469 &dev_attr_current_trigger.attr,
473 static const struct attribute_group iio_trigger_consumer_attr_group = {
475 .attrs = iio_trigger_consumer_attrs,
478 static void iio_trig_release(struct device *device)
480 struct iio_trigger *trig = to_iio_trigger(device);
483 if (trig->subirq_base) {
484 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
485 irq_modify_status(trig->subirq_base + i,
487 IRQ_NOREQUEST | IRQ_NOPROBE);
488 irq_set_chip(trig->subirq_base + i,
490 irq_set_handler(trig->subirq_base + i,
494 irq_free_descs(trig->subirq_base,
495 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
501 static const struct device_type iio_trig_type = {
502 .release = iio_trig_release,
503 .groups = iio_trig_dev_groups,
506 static void iio_trig_subirqmask(struct irq_data *d)
508 struct irq_chip *chip = irq_data_get_irq_chip(d);
509 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
511 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
514 static void iio_trig_subirqunmask(struct irq_data *d)
516 struct irq_chip *chip = irq_data_get_irq_chip(d);
517 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
519 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
522 static __printf(2, 0)
523 struct iio_trigger *viio_trigger_alloc(struct device *parent,
527 struct iio_trigger *trig;
530 trig = kzalloc(sizeof *trig, GFP_KERNEL);
534 trig->dev.parent = parent;
535 trig->dev.type = &iio_trig_type;
536 trig->dev.bus = &iio_bus_type;
537 device_initialize(&trig->dev);
539 mutex_init(&trig->pool_lock);
540 trig->subirq_base = irq_alloc_descs(-1, 0,
541 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
543 if (trig->subirq_base < 0)
546 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
547 if (trig->name == NULL)
550 trig->subirq_chip.name = trig->name;
551 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
552 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
553 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
554 irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
555 irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
556 irq_modify_status(trig->subirq_base + i,
557 IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
563 irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
570 * iio_trigger_alloc - Allocate a trigger
571 * @parent: Device to allocate iio_trigger for
572 * @fmt: trigger name format. If it includes format
573 * specifiers, the additional arguments following
574 * format are formatted and inserted in the resulting
575 * string replacing their respective specifiers.
577 * Pointer to allocated iio_trigger on success, NULL on failure.
579 struct iio_trigger *iio_trigger_alloc(struct device *parent, const char *fmt, ...)
581 struct iio_trigger *trig;
584 va_start(vargs, fmt);
585 trig = viio_trigger_alloc(parent, fmt, vargs);
590 EXPORT_SYMBOL(iio_trigger_alloc);
592 void iio_trigger_free(struct iio_trigger *trig)
595 put_device(&trig->dev);
597 EXPORT_SYMBOL(iio_trigger_free);
599 static void devm_iio_trigger_release(struct device *dev, void *res)
601 iio_trigger_free(*(struct iio_trigger **)res);
605 * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
606 * Managed iio_trigger_alloc. iio_trigger allocated with this function is
607 * automatically freed on driver detach.
608 * @parent: Device to allocate iio_trigger for
609 * @fmt: trigger name format. If it includes format
610 * specifiers, the additional arguments following
611 * format are formatted and inserted in the resulting
612 * string replacing their respective specifiers.
616 * Pointer to allocated iio_trigger on success, NULL on failure.
618 struct iio_trigger *devm_iio_trigger_alloc(struct device *parent, const char *fmt, ...)
620 struct iio_trigger **ptr, *trig;
623 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
628 /* use raw alloc_dr for kmalloc caller tracing */
629 va_start(vargs, fmt);
630 trig = viio_trigger_alloc(parent, fmt, vargs);
634 devres_add(parent, ptr);
641 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
643 static void devm_iio_trigger_unreg(void *trigger_info)
645 iio_trigger_unregister(trigger_info);
649 * __devm_iio_trigger_register - Resource-managed iio_trigger_register()
650 * @dev: device this trigger was allocated for
651 * @trig_info: trigger to register
652 * @this_mod: module registering the trigger
654 * Managed iio_trigger_register(). The IIO trigger registered with this
655 * function is automatically unregistered on driver detach. This function
656 * calls iio_trigger_register() internally. Refer to that function for more
660 * 0 on success, negative error number on failure.
662 int __devm_iio_trigger_register(struct device *dev,
663 struct iio_trigger *trig_info,
664 struct module *this_mod)
668 ret = __iio_trigger_register(trig_info, this_mod);
672 return devm_add_action_or_reset(dev, devm_iio_trigger_unreg, trig_info);
674 EXPORT_SYMBOL_GPL(__devm_iio_trigger_register);
676 bool iio_trigger_using_own(struct iio_dev *indio_dev)
678 return indio_dev->trig->attached_own_device;
680 EXPORT_SYMBOL(iio_trigger_using_own);
683 * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
685 * @trig: The IIO trigger to check
686 * @indio_dev: the IIO device to check
688 * This function can be used as the validate_device callback for triggers that
689 * can only be attached to their own device.
691 * Return: 0 if both the trigger and the IIO device belong to the same
692 * device, -EINVAL otherwise.
694 int iio_trigger_validate_own_device(struct iio_trigger *trig,
695 struct iio_dev *indio_dev)
697 if (indio_dev->dev.parent != trig->dev.parent)
701 EXPORT_SYMBOL(iio_trigger_validate_own_device);
703 int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
705 return iio_device_register_sysfs_group(indio_dev,
706 &iio_trigger_consumer_attr_group);
709 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
711 /* Clean up an associated but not attached trigger reference */
713 iio_trigger_put(indio_dev->trig);