Lines Matching +full:iio +full:- +full:consumer

1 // SPDX-License-Identifier: GPL-2.0-only
15 #include <linux/iio/iio.h>
16 #include <linux/iio/iio-opaque.h>
17 #include <linux/iio/trigger.h>
20 #include <linux/iio/trigger_consumer.h>
22 /* RFC - Question of approach
40 * name_show() - retrieve useful identifying name
54 return sysfs_emit(buf, "%s\n", trig->name); in name_show()
71 trig_info->id = ida_alloc(&iio_trigger_ida, GFP_KERNEL); in iio_trigger_register()
72 if (trig_info->id < 0) in iio_trigger_register()
73 return trig_info->id; in iio_trigger_register()
76 dev_set_name(&trig_info->dev, "trigger%d", trig_info->id); in iio_trigger_register()
78 ret = device_add(&trig_info->dev); in iio_trigger_register()
82 /* Add to list of available triggers held by the IIO core */ in iio_trigger_register()
84 if (__iio_trigger_find_by_name(trig_info->name)) { in iio_trigger_register()
85 pr_err("Duplicate trigger name '%s'\n", trig_info->name); in iio_trigger_register()
86 ret = -EEXIST; in iio_trigger_register()
89 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()
109 ida_free(&iio_trigger_ida, trig_info->id); in iio_trigger_unregister()
111 device_del(&trig_info->dev); in iio_trigger_unregister()
120 return -EINVAL; in iio_trigger_set_immutable()
123 mutex_lock(&indio_dev->mlock); in iio_trigger_set_immutable()
124 WARN_ON(iio_dev_opaque->trig_readonly); in iio_trigger_set_immutable()
126 indio_dev->trig = iio_trigger_get(trig); in iio_trigger_set_immutable()
127 iio_dev_opaque->trig_readonly = true; in iio_trigger_set_immutable()
128 mutex_unlock(&indio_dev->mlock); in iio_trigger_set_immutable()
140 if (!strcmp(iter->name, name)) in __iio_trigger_find_by_name()
152 if (sysfs_streq(iter->name, name)) { in iio_trigger_acquire_by_name()
168 * This 'might' occur after the trigger state is set to disabled - in iio_reenable_work_fn()
171 trig->ops->reenable(trig); in iio_reenable_work_fn()
190 if (atomic_dec_and_test(&trig->use_count) && trig->ops && in iio_trigger_notify_done_atomic()
191 trig->ops->reenable) in iio_trigger_notify_done_atomic()
192 schedule_work(&trig->reenable_work); in iio_trigger_notify_done_atomic()
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()
223 if (!atomic_read(&trig->use_count)) { in iio_trigger_poll_chained()
224 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in iio_trigger_poll_chained()
227 if (trig->subirqs[i].enabled) in iio_trigger_poll_chained()
228 handle_nested_irq(trig->subirq_base + i); in iio_trigger_poll_chained()
238 if (atomic_dec_and_test(&trig->use_count) && trig->ops && in iio_trigger_notify_done()
239 trig->ops->reenable) in iio_trigger_notify_done()
240 trig->ops->reenable(trig); in iio_trigger_notify_done()
244 /* Trigger Consumer related functions */
249 mutex_lock(&trig->pool_lock); in iio_trigger_get_irq()
250 ret = bitmap_find_free_region(trig->pool, in iio_trigger_get_irq()
253 mutex_unlock(&trig->pool_lock); in iio_trigger_get_irq()
255 ret += trig->subirq_base; in iio_trigger_get_irq()
262 mutex_lock(&trig->pool_lock); in iio_trigger_put_irq()
263 clear_bit(irq - trig->subirq_base, trig->pool); in iio_trigger_put_irq()
264 mutex_unlock(&trig->pool_lock); in iio_trigger_put_irq()
277 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev); in iio_trigger_attach_poll_func()
279 bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in iio_trigger_attach_poll_func()
283 __module_get(iio_dev_opaque->driver_module); in iio_trigger_attach_poll_func()
286 pf->irq = iio_trigger_get_irq(trig); in iio_trigger_attach_poll_func()
287 if (pf->irq < 0) { in iio_trigger_attach_poll_func()
289 trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in iio_trigger_attach_poll_func()
294 ret = request_threaded_irq(pf->irq, pf->h, pf->thread, in iio_trigger_attach_poll_func()
295 pf->type, pf->name, in iio_trigger_attach_poll_func()
301 if (trig->ops && trig->ops->set_trigger_state && notinuse) { in iio_trigger_attach_poll_func()
302 ret = trig->ops->set_trigger_state(trig, true); in iio_trigger_attach_poll_func()
309 * this is the case if the IIO device and the trigger device share the in iio_trigger_attach_poll_func()
312 if (pf->indio_dev->dev.parent == trig->dev.parent) in iio_trigger_attach_poll_func()
313 trig->attached_own_device = true; in iio_trigger_attach_poll_func()
318 free_irq(pf->irq, pf); in iio_trigger_attach_poll_func()
320 iio_trigger_put_irq(trig, pf->irq); in iio_trigger_attach_poll_func()
322 module_put(iio_dev_opaque->driver_module); in iio_trigger_attach_poll_func()
329 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev); in iio_trigger_detach_poll_func()
331 bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1; in iio_trigger_detach_poll_func()
334 if (trig->ops && trig->ops->set_trigger_state && no_other_users) { in iio_trigger_detach_poll_func()
335 ret = trig->ops->set_trigger_state(trig, false); in iio_trigger_detach_poll_func()
339 if (pf->indio_dev->dev.parent == trig->dev.parent) in iio_trigger_detach_poll_func()
340 trig->attached_own_device = false; in iio_trigger_detach_poll_func()
341 iio_trigger_put_irq(trig, pf->irq); in iio_trigger_detach_poll_func()
342 free_irq(pf->irq, pf); in iio_trigger_detach_poll_func()
343 module_put(iio_dev_opaque->driver_module); in iio_trigger_detach_poll_func()
352 pf->timestamp = iio_get_time_ns(pf->indio_dev); in iio_pollfunc_store_time()
372 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs); in iio_alloc_pollfunc()
374 if (pf->name == NULL) { in iio_alloc_pollfunc()
378 pf->h = h; in iio_alloc_pollfunc()
379 pf->thread = thread; in iio_alloc_pollfunc()
380 pf->type = type; in iio_alloc_pollfunc()
381 pf->indio_dev = indio_dev; in iio_alloc_pollfunc()
389 kfree(pf->name); in iio_dealloc_pollfunc()
395 * current_trigger_show() - trigger consumer sysfs query current trigger
412 if (indio_dev->trig) in current_trigger_show()
413 return sysfs_emit(buf, "%s\n", indio_dev->trig->name); in current_trigger_show()
418 * current_trigger_store() - trigger consumer sysfs set current trigger
437 struct iio_trigger *oldtrig = indio_dev->trig; in current_trigger_store()
441 mutex_lock(&indio_dev->mlock); in current_trigger_store()
442 if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) { in current_trigger_store()
443 mutex_unlock(&indio_dev->mlock); in current_trigger_store()
444 return -EBUSY; in current_trigger_store()
446 if (iio_dev_opaque->trig_readonly) { in current_trigger_store()
447 mutex_unlock(&indio_dev->mlock); in current_trigger_store()
448 return -EPERM; in current_trigger_store()
450 mutex_unlock(&indio_dev->mlock); in current_trigger_store()
458 if (trig && indio_dev->info->validate_trigger) { in current_trigger_store()
459 ret = indio_dev->info->validate_trigger(indio_dev, trig); in current_trigger_store()
464 if (trig && trig->ops && trig->ops->validate_device) { in current_trigger_store()
465 ret = trig->ops->validate_device(trig, indio_dev); in current_trigger_store()
470 indio_dev->trig = trig; in current_trigger_store()
473 if (indio_dev->modes & INDIO_EVENT_TRIGGERED) in current_trigger_store()
475 indio_dev->pollfunc_event); in current_trigger_store()
478 if (indio_dev->trig) { in current_trigger_store()
479 if (indio_dev->modes & INDIO_EVENT_TRIGGERED) in current_trigger_store()
480 iio_trigger_attach_poll_func(indio_dev->trig, in current_trigger_store()
481 indio_dev->pollfunc_event); in current_trigger_store()
509 if (trig->subirq_base) { in iio_trig_release()
511 irq_modify_status(trig->subirq_base + i, in iio_trig_release()
514 irq_set_chip(trig->subirq_base + i, in iio_trig_release()
516 irq_set_handler(trig->subirq_base + i, in iio_trig_release()
520 irq_free_descs(trig->subirq_base, in iio_trig_release()
523 kfree(trig->name); in iio_trig_release()
537 trig->subirqs[d->irq - trig->subirq_base].enabled = false; in iio_trig_subirqmask()
545 trig->subirqs[d->irq - trig->subirq_base].enabled = true; in iio_trig_subirqunmask()
561 trig->dev.parent = parent; in viio_trigger_alloc()
562 trig->dev.type = &iio_trig_type; in viio_trigger_alloc()
563 trig->dev.bus = &iio_bus_type; in viio_trigger_alloc()
564 device_initialize(&trig->dev); in viio_trigger_alloc()
565 INIT_WORK(&trig->reenable_work, iio_reenable_work_fn); in viio_trigger_alloc()
567 mutex_init(&trig->pool_lock); in viio_trigger_alloc()
568 trig->subirq_base = irq_alloc_descs(-1, 0, in viio_trigger_alloc()
571 if (trig->subirq_base < 0) in viio_trigger_alloc()
574 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs); in viio_trigger_alloc()
575 if (trig->name == NULL) in viio_trigger_alloc()
578 INIT_LIST_HEAD(&trig->list); in viio_trigger_alloc()
580 trig->owner = this_mod; in viio_trigger_alloc()
582 trig->subirq_chip.name = trig->name; in viio_trigger_alloc()
583 trig->subirq_chip.irq_mask = &iio_trig_subirqmask; in viio_trigger_alloc()
584 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask; in viio_trigger_alloc()
586 irq_set_chip(trig->subirq_base + i, &trig->subirq_chip); in viio_trigger_alloc()
587 irq_set_handler(trig->subirq_base + i, &handle_simple_irq); in viio_trigger_alloc()
588 irq_modify_status(trig->subirq_base + i, in viio_trigger_alloc()
595 irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER); in viio_trigger_alloc()
602 * __iio_trigger_alloc - Allocate a trigger
630 put_device(&trig->dev); in iio_trigger_free()
640 * __devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
687 * devm_iio_trigger_register - Resource-managed iio_trigger_register()
691 * Managed iio_trigger_register(). The IIO trigger registered with this
714 return indio_dev->trig->attached_own_device; in iio_trigger_using_own()
719 * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
721 * @trig: The IIO trigger to check
722 * @indio_dev: the IIO device to check
727 * Return: 0 if both the trigger and the IIO device belong to the same
728 * device, -EINVAL otherwise.
733 if (indio_dev->dev.parent != trig->dev.parent) in iio_trigger_validate_own_device()
734 return -EINVAL; in iio_trigger_validate_own_device()
748 if (indio_dev->trig) in iio_device_unregister_trigger_consumer()
749 iio_trigger_put(indio_dev->trig); in iio_device_unregister_trigger_consumer()