1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012 Analog Devices, Inc.
4 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 #include <linux/kernel.h>
8 #include <linux/export.h>
9 #include <linux/module.h>
10 #include <linux/iio/iio.h>
11 #include <linux/iio/buffer.h>
12 #include <linux/iio/kfifo_buf.h>
13 #include <linux/iio/triggered_buffer.h>
14 #include <linux/iio/trigger_consumer.h>
17 * iio_triggered_buffer_setup() - Setup triggered buffer and pollfunc
18 * @indio_dev: IIO device structure
19 * @h: Function which will be used as pollfunc top half
20 * @thread: Function which will be used as pollfunc bottom half
21 * @setup_ops: Buffer setup functions to use for this device.
22 * If NULL the default setup functions for triggered
23 * buffers will be used.
25 * This function combines some common tasks which will normally be performed
26 * when setting up a triggered buffer. It will allocate the buffer and the
29 * Before calling this function the indio_dev structure should already be
30 * completely initialized, but not yet registered. In practice this means that
31 * this function should be called right before iio_device_register().
33 * To free the resources allocated by this function call
34 * iio_triggered_buffer_cleanup().
36 int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
37 irqreturn_t (*h)(int irq, void *p),
38 irqreturn_t (*thread)(int irq, void *p),
39 const struct iio_buffer_setup_ops *setup_ops)
41 struct iio_buffer *buffer;
44 buffer = iio_kfifo_allocate();
50 iio_device_attach_buffer(indio_dev, buffer);
52 indio_dev->pollfunc = iio_alloc_pollfunc(h,
59 if (indio_dev->pollfunc == NULL) {
61 goto error_kfifo_free;
64 /* Ring buffer functions - here trigger setup related */
65 indio_dev->setup_ops = setup_ops;
67 /* Flag that polled ring buffering is possible */
68 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
73 iio_kfifo_free(indio_dev->buffer);
77 EXPORT_SYMBOL(iio_triggered_buffer_setup);
80 * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup()
81 * @indio_dev: IIO device structure
83 void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
85 iio_dealloc_pollfunc(indio_dev->pollfunc);
86 iio_kfifo_free(indio_dev->buffer);
88 EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
90 static void devm_iio_triggered_buffer_clean(struct device *dev, void *res)
92 iio_triggered_buffer_cleanup(*(struct iio_dev **)res);
95 int devm_iio_triggered_buffer_setup(struct device *dev,
96 struct iio_dev *indio_dev,
97 irqreturn_t (*h)(int irq, void *p),
98 irqreturn_t (*thread)(int irq, void *p),
99 const struct iio_buffer_setup_ops *ops)
101 struct iio_dev **ptr;
104 ptr = devres_alloc(devm_iio_triggered_buffer_clean, sizeof(*ptr),
111 ret = iio_triggered_buffer_setup(indio_dev, h, thread, ops);
113 devres_add(dev, ptr);
119 EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup);
121 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
122 MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
123 MODULE_LICENSE("GPL");