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 */
6
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>
16
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
26 *
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.
30 *
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().
34 *
35 * To free the resources allocated by this function call
36 * iio_triggered_buffer_cleanup().
37 */
iio_triggered_buffer_setup_ext(struct iio_dev * indio_dev,irqreturn_t (* h)(int irq,void * p),irqreturn_t (* thread)(int irq,void * p),const struct iio_buffer_setup_ops * setup_ops,const struct attribute ** buffer_attrs)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)
43 {
44 struct iio_buffer *buffer;
45 int ret;
46
47 /*
48 * iio_triggered_buffer_cleanup() assumes that the buffer allocated here
49 * is assigned to indio_dev->buffer but this is only the case if this
50 * function is the first caller to iio_device_attach_buffer(). If
51 * indio_dev->buffer is already set then we can't proceed otherwise the
52 * cleanup function will try to free a buffer that was not allocated here.
53 */
54 if (indio_dev->buffer)
55 return -EADDRINUSE;
56
57 buffer = iio_kfifo_allocate();
58 if (!buffer) {
59 ret = -ENOMEM;
60 goto error_ret;
61 }
62
63 indio_dev->pollfunc = iio_alloc_pollfunc(h,
64 thread,
65 IRQF_ONESHOT,
66 indio_dev,
67 "%s_consumer%d",
68 indio_dev->name,
69 iio_device_id(indio_dev));
70 if (indio_dev->pollfunc == NULL) {
71 ret = -ENOMEM;
72 goto error_kfifo_free;
73 }
74
75 /* Ring buffer functions - here trigger setup related */
76 indio_dev->setup_ops = setup_ops;
77
78 /* Flag that polled ring buffering is possible */
79 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
80
81 buffer->attrs = buffer_attrs;
82
83 ret = iio_device_attach_buffer(indio_dev, buffer);
84 if (ret < 0)
85 goto error_dealloc_pollfunc;
86
87 return 0;
88
89 error_dealloc_pollfunc:
90 iio_dealloc_pollfunc(indio_dev->pollfunc);
91 error_kfifo_free:
92 iio_kfifo_free(buffer);
93 error_ret:
94 return ret;
95 }
96 EXPORT_SYMBOL(iio_triggered_buffer_setup_ext);
97
98 /**
99 * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup_ext()
100 * @indio_dev: IIO device structure
101 */
iio_triggered_buffer_cleanup(struct iio_dev * indio_dev)102 void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
103 {
104 iio_dealloc_pollfunc(indio_dev->pollfunc);
105 iio_kfifo_free(indio_dev->buffer);
106 }
107 EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
108
devm_iio_triggered_buffer_clean(void * indio_dev)109 static void devm_iio_triggered_buffer_clean(void *indio_dev)
110 {
111 iio_triggered_buffer_cleanup(indio_dev);
112 }
113
devm_iio_triggered_buffer_setup_ext(struct device * dev,struct iio_dev * indio_dev,irqreturn_t (* h)(int irq,void * p),irqreturn_t (* thread)(int irq,void * p),const struct iio_buffer_setup_ops * ops,const struct attribute ** buffer_attrs)114 int devm_iio_triggered_buffer_setup_ext(struct device *dev,
115 struct iio_dev *indio_dev,
116 irqreturn_t (*h)(int irq, void *p),
117 irqreturn_t (*thread)(int irq, void *p),
118 const struct iio_buffer_setup_ops *ops,
119 const struct attribute **buffer_attrs)
120 {
121 int ret;
122
123 ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, ops,
124 buffer_attrs);
125 if (ret)
126 return ret;
127
128 return devm_add_action_or_reset(dev, devm_iio_triggered_buffer_clean,
129 indio_dev);
130 }
131 EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup_ext);
132
133 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
134 MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
135 MODULE_LICENSE("GPL");
136