Lines Matching +full:iio +full:- +full:consumer
1 // SPDX-License-Identifier: GPL-2.0-only
16 #include <linux/iio/iio.h>
17 #include <linux/iio/iio-opaque.h>
18 #include <linux/iio/trigger.h>
21 #include <linux/iio/trigger_consumer.h>
23 /* RFC - Question of approach
41 * name_show() - retrieve useful identifying name
55 return sysfs_emit(buf, "%s\n", trig->name); in name_show()
72 trig_info->id = ida_alloc(&iio_trigger_ida, GFP_KERNEL); in iio_trigger_register()
73 if (trig_info->id < 0) in iio_trigger_register()
74 return trig_info->id; in iio_trigger_register()
77 dev_set_name(&trig_info->dev, "trigger%d", trig_info->id); in iio_trigger_register()
79 ret = device_add(&trig_info->dev); in iio_trigger_register()
83 /* Add to list of available triggers held by the IIO core */ in iio_trigger_register()
85 if (__iio_trigger_find_by_name(trig_info->name)) { in iio_trigger_register()
86 pr_err("Duplicate trigger name '%s'\n", trig_info->name); in iio_trigger_register()
87 ret = -EEXIST; in iio_trigger_register()
90 list_add_tail(&trig_info->list, &iio_trigger_list); in iio_trigger_register()
96 device_del(&trig_info->dev); in iio_trigger_register()
98 ida_free(&iio_trigger_ida, trig_info->id); in iio_trigger_register()
106 list_del(&trig_info->list); in iio_trigger_unregister()
108 ida_free(&iio_trigger_ida, trig_info->id); in iio_trigger_unregister()
110 device_del(&trig_info->dev); in iio_trigger_unregister()
119 return -EINVAL; in iio_trigger_set_immutable()
122 guard(mutex)(&iio_dev_opaque->mlock); in iio_trigger_set_immutable()
123 WARN_ON(iio_dev_opaque->trig_readonly); in iio_trigger_set_immutable()
125 indio_dev->trig = iio_trigger_get(trig); in iio_trigger_set_immutable()
126 iio_dev_opaque->trig_readonly = true; in iio_trigger_set_immutable()
138 if (!strcmp(iter->name, name)) in __iio_trigger_find_by_name()
150 if (sysfs_streq(iter->name, name)) in iio_trigger_acquire_by_name()
162 * This 'might' occur after the trigger state is set to disabled - in iio_reenable_work_fn()
165 trig->ops->reenable(trig); in iio_reenable_work_fn()
184 if (atomic_dec_and_test(&trig->use_count) && trig->ops && in iio_trigger_notify_done_atomic()
185 trig->ops->reenable) in iio_trigger_notify_done_atomic()
186 schedule_work(&trig->reenable_work); in iio_trigger_notify_done_atomic()
190 * iio_trigger_poll() - Call the IRQ trigger handler of the consumers
199 if (!atomic_read(&trig->use_count)) { in iio_trigger_poll()
200 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in iio_trigger_poll()
203 if (trig->subirqs[i].enabled) in iio_trigger_poll()
204 generic_handle_irq(trig->subirq_base + i); in iio_trigger_poll()
220 * iio_trigger_poll_nested() - Call the threaded trigger handler of the
230 if (!atomic_read(&trig->use_count)) { in iio_trigger_poll_nested()
231 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in iio_trigger_poll_nested()
234 if (trig->subirqs[i].enabled) in iio_trigger_poll_nested()
235 handle_nested_irq(trig->subirq_base + i); in iio_trigger_poll_nested()
245 if (atomic_dec_and_test(&trig->use_count) && trig->ops && in iio_trigger_notify_done()
246 trig->ops->reenable) in iio_trigger_notify_done()
247 trig->ops->reenable(trig); in iio_trigger_notify_done()
251 /* Trigger Consumer related functions */
256 scoped_guard(mutex, &trig->pool_lock) { in iio_trigger_get_irq()
257 ret = bitmap_find_free_region(trig->pool, in iio_trigger_get_irq()
264 return ret + trig->subirq_base; in iio_trigger_get_irq()
269 guard(mutex)(&trig->pool_lock); in iio_trigger_put_irq()
270 clear_bit(irq - trig->subirq_base, trig->pool); in iio_trigger_put_irq()
283 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev); in iio_trigger_attach_poll_func()
285 bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in iio_trigger_attach_poll_func()
289 __module_get(iio_dev_opaque->driver_module); in iio_trigger_attach_poll_func()
292 pf->irq = iio_trigger_get_irq(trig); in iio_trigger_attach_poll_func()
293 if (pf->irq < 0) { in iio_trigger_attach_poll_func()
295 trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in iio_trigger_attach_poll_func()
300 ret = request_threaded_irq(pf->irq, pf->h, pf->thread, in iio_trigger_attach_poll_func()
301 pf->type, pf->name, in iio_trigger_attach_poll_func()
307 if (trig->ops && trig->ops->set_trigger_state && notinuse) { in iio_trigger_attach_poll_func()
308 ret = trig->ops->set_trigger_state(trig, true); in iio_trigger_attach_poll_func()
315 * this is the case if the IIO device and the trigger device share the in iio_trigger_attach_poll_func()
318 if (!iio_validate_own_trigger(pf->indio_dev, trig)) in iio_trigger_attach_poll_func()
319 trig->attached_own_device = true; in iio_trigger_attach_poll_func()
324 free_irq(pf->irq, pf); in iio_trigger_attach_poll_func()
326 iio_trigger_put_irq(trig, pf->irq); in iio_trigger_attach_poll_func()
328 module_put(iio_dev_opaque->driver_module); in iio_trigger_attach_poll_func()
335 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev); in iio_trigger_detach_poll_func()
337 bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1; in iio_trigger_detach_poll_func()
340 if (trig->ops && trig->ops->set_trigger_state && no_other_users) { in iio_trigger_detach_poll_func()
341 ret = trig->ops->set_trigger_state(trig, false); in iio_trigger_detach_poll_func()
345 if (pf->indio_dev->dev.parent == trig->dev.parent) in iio_trigger_detach_poll_func()
346 trig->attached_own_device = false; in iio_trigger_detach_poll_func()
347 iio_trigger_put_irq(trig, pf->irq); in iio_trigger_detach_poll_func()
348 free_irq(pf->irq, pf); in iio_trigger_detach_poll_func()
349 module_put(iio_dev_opaque->driver_module); in iio_trigger_detach_poll_func()
350 pf->irq = 0; in iio_trigger_detach_poll_func()
359 pf->timestamp = iio_get_time_ns(pf->indio_dev); in iio_pollfunc_store_time()
379 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs); in iio_alloc_pollfunc()
381 if (pf->name == NULL) { in iio_alloc_pollfunc()
385 pf->h = h; in iio_alloc_pollfunc()
386 pf->thread = thread; in iio_alloc_pollfunc()
387 pf->type = type; in iio_alloc_pollfunc()
388 pf->indio_dev = indio_dev; in iio_alloc_pollfunc()
396 kfree(pf->name); in iio_dealloc_pollfunc()
402 * current_trigger_show() - trigger consumer sysfs query current trigger
419 if (indio_dev->trig) in current_trigger_show()
420 return sysfs_emit(buf, "%s\n", indio_dev->trig->name); in current_trigger_show()
425 * current_trigger_store() - trigger consumer sysfs set current trigger
444 struct iio_trigger *oldtrig = indio_dev->trig; in current_trigger_store()
448 scoped_guard(mutex, &iio_dev_opaque->mlock) { in current_trigger_store()
449 if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) in current_trigger_store()
450 return -EBUSY; in current_trigger_store()
451 if (iio_dev_opaque->trig_readonly) in current_trigger_store()
452 return -EPERM; in current_trigger_store()
461 if (trig && indio_dev->info->validate_trigger) { in current_trigger_store()
462 ret = indio_dev->info->validate_trigger(indio_dev, trig); in current_trigger_store()
467 if (trig && trig->ops && trig->ops->validate_device) { in current_trigger_store()
468 ret = trig->ops->validate_device(trig, indio_dev); in current_trigger_store()
473 indio_dev->trig = trig; in current_trigger_store()
476 if (indio_dev->modes & INDIO_EVENT_TRIGGERED) in current_trigger_store()
478 indio_dev->pollfunc_event); in current_trigger_store()
481 if (indio_dev->trig) { in current_trigger_store()
482 if (indio_dev->modes & INDIO_EVENT_TRIGGERED) in current_trigger_store()
483 iio_trigger_attach_poll_func(indio_dev->trig, in current_trigger_store()
484 indio_dev->pollfunc_event); in current_trigger_store()
512 if (trig->subirq_base) { in iio_trig_release()
514 irq_modify_status(trig->subirq_base + i, in iio_trig_release()
517 irq_set_chip(trig->subirq_base + i, in iio_trig_release()
519 irq_set_handler(trig->subirq_base + i, in iio_trig_release()
523 irq_free_descs(trig->subirq_base, in iio_trig_release()
526 kfree(trig->name); in iio_trig_release()
540 trig->subirqs[d->irq - trig->subirq_base].enabled = false; in iio_trig_subirqmask()
548 trig->subirqs[d->irq - trig->subirq_base].enabled = true; in iio_trig_subirqunmask()
564 trig->dev.parent = parent; in viio_trigger_alloc()
565 trig->dev.type = &iio_trig_type; in viio_trigger_alloc()
566 trig->dev.bus = &iio_bus_type; in viio_trigger_alloc()
567 device_initialize(&trig->dev); in viio_trigger_alloc()
568 INIT_WORK(&trig->reenable_work, iio_reenable_work_fn); in viio_trigger_alloc()
570 mutex_init(&trig->pool_lock); in viio_trigger_alloc()
571 trig->subirq_base = irq_alloc_descs(-1, 0, in viio_trigger_alloc()
574 if (trig->subirq_base < 0) in viio_trigger_alloc()
577 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs); in viio_trigger_alloc()
578 if (trig->name == NULL) in viio_trigger_alloc()
581 INIT_LIST_HEAD(&trig->list); in viio_trigger_alloc()
583 trig->owner = this_mod; in viio_trigger_alloc()
585 trig->subirq_chip.name = trig->name; in viio_trigger_alloc()
586 trig->subirq_chip.irq_mask = &iio_trig_subirqmask; in viio_trigger_alloc()
587 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask; in viio_trigger_alloc()
589 irq_set_chip(trig->subirq_base + i, &trig->subirq_chip); in viio_trigger_alloc()
590 irq_set_handler(trig->subirq_base + i, &handle_simple_irq); in viio_trigger_alloc()
591 irq_modify_status(trig->subirq_base + i, in viio_trigger_alloc()
598 irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in viio_trigger_alloc()
605 * __iio_trigger_alloc - Allocate a trigger
633 put_device(&trig->dev); in iio_trigger_free()
643 * __devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
690 * devm_iio_trigger_register - Resource-managed iio_trigger_register()
694 * Managed iio_trigger_register(). The IIO trigger registered with this
717 return indio_dev->trig->attached_own_device; in iio_trigger_using_own()
722 * iio_validate_own_trigger - Check if a trigger and IIO device belong to
724 * @idev: the IIO device to check
725 * @trig: the IIO trigger to check
730 * Return: 0 if both the trigger and the IIO device belong to the same
731 * device, -EINVAL otherwise.
735 if (idev->dev.parent != trig->dev.parent) in iio_validate_own_trigger()
736 return -EINVAL; in iio_validate_own_trigger()
742 * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
744 * @trig: The IIO trigger to check
745 * @indio_dev: the IIO device to check
750 * Return: 0 if both the trigger and the IIO device belong to the same
751 * device, -EINVAL otherwise.
756 if (indio_dev->dev.parent != trig->dev.parent) in iio_trigger_validate_own_device()
757 return -EINVAL; in iio_trigger_validate_own_device()
771 if (indio_dev->trig) in iio_device_unregister_trigger_consumer()
772 iio_trigger_put(indio_dev->trig); in iio_device_unregister_trigger_consumer()
779 guard(mutex)(&iio_dev_opaque->mlock); in iio_device_suspend_triggering()
781 if ((indio_dev->pollfunc) && (indio_dev->pollfunc->irq > 0)) in iio_device_suspend_triggering()
782 disable_irq(indio_dev->pollfunc->irq); in iio_device_suspend_triggering()
792 guard(mutex)(&iio_dev_opaque->mlock); in iio_device_resume_triggering()
794 if ((indio_dev->pollfunc) && (indio_dev->pollfunc->irq > 0)) in iio_device_resume_triggering()
795 enable_irq(indio_dev->pollfunc->irq); in iio_device_resume_triggering()