Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / buffer / industrialio-triggered-buffer.c
blob92b8aea3e0634aef4d5e858cd3baf84afd9afdaa
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2012 Analog Devices, Inc.
4 * Author: Lars-Peter Clausen <lars@metafoo.de>
5 */
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/buffer_impl.h>
13 #include <linux/iio/kfifo_buf.h>
14 #include <linux/iio/triggered_buffer.h>
15 #include <linux/iio/trigger_consumer.h>
17 /**
18 * iio_triggered_buffer_setup_ext() - Setup triggered buffer and pollfunc
19 * @indio_dev: IIO device structure
20 * @h: Function which will be used as pollfunc top half
21 * @thread: Function which will be used as pollfunc bottom half
22 * @setup_ops: Buffer setup functions to use for this device.
23 * If NULL the default setup functions for triggered
24 * buffers will be used.
25 * @buffer_attrs: Extra sysfs buffer attributes for this IIO buffer
27 * This function combines some common tasks which will normally be performed
28 * when setting up a triggered buffer. It will allocate the buffer and the
29 * pollfunc.
31 * Before calling this function the indio_dev structure should already be
32 * completely initialized, but not yet registered. In practice this means that
33 * this function should be called right before iio_device_register().
35 * To free the resources allocated by this function call
36 * iio_triggered_buffer_cleanup().
38 int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev,
39 irqreturn_t (*h)(int irq, void *p),
40 irqreturn_t (*thread)(int irq, void *p),
41 const struct iio_buffer_setup_ops *setup_ops,
42 const struct attribute **buffer_attrs)
44 struct iio_buffer *buffer;
45 int ret;
47 buffer = iio_kfifo_allocate();
48 if (!buffer) {
49 ret = -ENOMEM;
50 goto error_ret;
53 iio_device_attach_buffer(indio_dev, buffer);
55 indio_dev->pollfunc = iio_alloc_pollfunc(h,
56 thread,
57 IRQF_ONESHOT,
58 indio_dev,
59 "%s_consumer%d",
60 indio_dev->name,
61 indio_dev->id);
62 if (indio_dev->pollfunc == NULL) {
63 ret = -ENOMEM;
64 goto error_kfifo_free;
67 /* Ring buffer functions - here trigger setup related */
68 indio_dev->setup_ops = setup_ops;
70 /* Flag that polled ring buffering is possible */
71 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
73 buffer->attrs = buffer_attrs;
75 return 0;
77 error_kfifo_free:
78 iio_kfifo_free(indio_dev->buffer);
79 error_ret:
80 return ret;
82 EXPORT_SYMBOL(iio_triggered_buffer_setup_ext);
84 /**
85 * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup_ext()
86 * @indio_dev: IIO device structure
88 void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
90 iio_dealloc_pollfunc(indio_dev->pollfunc);
91 iio_kfifo_free(indio_dev->buffer);
93 EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
95 static void devm_iio_triggered_buffer_clean(struct device *dev, void *res)
97 iio_triggered_buffer_cleanup(*(struct iio_dev **)res);
100 int devm_iio_triggered_buffer_setup_ext(struct device *dev,
101 struct iio_dev *indio_dev,
102 irqreturn_t (*h)(int irq, void *p),
103 irqreturn_t (*thread)(int irq, void *p),
104 const struct iio_buffer_setup_ops *ops,
105 const struct attribute **buffer_attrs)
107 struct iio_dev **ptr;
108 int ret;
110 ptr = devres_alloc(devm_iio_triggered_buffer_clean, sizeof(*ptr),
111 GFP_KERNEL);
112 if (!ptr)
113 return -ENOMEM;
115 *ptr = indio_dev;
117 ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, ops,
118 buffer_attrs);
119 if (!ret)
120 devres_add(dev, ptr);
121 else
122 devres_free(ptr);
124 return ret;
126 EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup_ext);
128 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
129 MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
130 MODULE_LICENSE("GPL");