^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015 Cogent Embedded, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/iio/triggered_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/iio/trigger_consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * iio_triggered_event_setup() - Setup pollfunc_event for triggered event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @indio_dev: IIO device structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @h: Function which will be used as pollfunc_event top half
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * @thread: Function which will be used as pollfunc_event bottom half
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * This function combines some common tasks which will normally be performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * when setting up a triggered event. It will allocate the pollfunc_event and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * set mode to use it for triggered event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Before calling this function the indio_dev structure should already be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * completely initialized, but not yet registered. In practice this means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * this function should be called right before iio_device_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * To free the resources allocated by this function call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * iio_triggered_event_cleanup().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int iio_triggered_event_setup(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) irqreturn_t (*h)(int irq, void *p),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) irqreturn_t (*thread)(int irq, void *p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) indio_dev->pollfunc_event = iio_alloc_pollfunc(h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) "%s_consumer%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) indio_dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) indio_dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (indio_dev->pollfunc_event == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Flag that events polling is possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) indio_dev->modes |= INDIO_EVENT_TRIGGERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) EXPORT_SYMBOL(iio_triggered_event_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * iio_triggered_event_cleanup() - Free resources allocated by iio_triggered_event_setup()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * @indio_dev: IIO device structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void iio_triggered_event_cleanup(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) indio_dev->modes &= ~INDIO_EVENT_TRIGGERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) iio_dealloc_pollfunc(indio_dev->pollfunc_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) EXPORT_SYMBOL(iio_triggered_event_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) MODULE_AUTHOR("Vladimir Barinov");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) MODULE_DESCRIPTION("IIO helper functions for setting up triggered events");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) MODULE_LICENSE("GPL");