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  * @direction:		Direction of the data stream (in/out).
23  * @setup_ops:		Buffer setup functions to use for this device.
24  *			If NULL the default setup functions for triggered
25  *			buffers will be used.
26  * @buffer_attrs:	Extra sysfs buffer attributes for this IIO buffer
27  *
28  * This function combines some common tasks which will normally be performed
29  * when setting up a triggered buffer. It will allocate the buffer and the
30  * pollfunc.
31  *
32  * Before calling this function the indio_dev structure should already be
33  * completely initialized, but not yet registered. In practice this means that
34  * this function should be called right before iio_device_register().
35  *
36  * To free the resources allocated by this function call
37  * iio_triggered_buffer_cleanup().
38  */
39 int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev,
40 	irqreturn_t (*h)(int irq, void *p),
41 	irqreturn_t (*thread)(int irq, void *p),
42 	enum iio_buffer_direction direction,
43 	const struct iio_buffer_setup_ops *setup_ops,
44 	const struct iio_dev_attr **buffer_attrs)
45 {
46 	struct iio_buffer *buffer;
47 	int ret;
48 
49 	/*
50 	 * iio_triggered_buffer_cleanup() assumes that the buffer allocated here
51 	 * is assigned to indio_dev->buffer but this is only the case if this
52 	 * function is the first caller to iio_device_attach_buffer(). If
53 	 * indio_dev->buffer is already set then we can't proceed otherwise the
54 	 * cleanup function will try to free a buffer that was not allocated here.
55 	 */
56 	if (indio_dev->buffer)
57 		return -EADDRINUSE;
58 
59 	buffer = iio_kfifo_allocate();
60 	if (!buffer) {
61 		ret = -ENOMEM;
62 		goto error_ret;
63 	}
64 
65 	indio_dev->pollfunc = iio_alloc_pollfunc(h,
66 						 thread,
67 						 IRQF_ONESHOT,
68 						 indio_dev,
69 						 "%s_consumer%d",
70 						 indio_dev->name,
71 						 iio_device_id(indio_dev));
72 	if (indio_dev->pollfunc == NULL) {
73 		ret = -ENOMEM;
74 		goto error_kfifo_free;
75 	}
76 
77 	/* Ring buffer functions - here trigger setup related */
78 	indio_dev->setup_ops = setup_ops;
79 
80 	/* Flag that polled ring buffering is possible */
81 	indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
82 
83 	buffer->direction = direction;
84 	buffer->attrs = buffer_attrs;
85 
86 	ret = iio_device_attach_buffer(indio_dev, buffer);
87 	if (ret < 0)
88 		goto error_dealloc_pollfunc;
89 
90 	return 0;
91 
92 error_dealloc_pollfunc:
93 	iio_dealloc_pollfunc(indio_dev->pollfunc);
94 error_kfifo_free:
95 	iio_kfifo_free(buffer);
96 error_ret:
97 	return ret;
98 }
99 EXPORT_SYMBOL(iio_triggered_buffer_setup_ext);
100 
101 /**
102  * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup_ext()
103  * @indio_dev: IIO device structure
104  */
105 void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
106 {
107 	iio_dealloc_pollfunc(indio_dev->pollfunc);
108 	iio_kfifo_free(indio_dev->buffer);
109 }
110 EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
111 
112 static void devm_iio_triggered_buffer_clean(void *indio_dev)
113 {
114 	iio_triggered_buffer_cleanup(indio_dev);
115 }
116 
117 int devm_iio_triggered_buffer_setup_ext(struct device *dev,
118 					struct iio_dev *indio_dev,
119 					irqreturn_t (*h)(int irq, void *p),
120 					irqreturn_t (*thread)(int irq, void *p),
121 					enum iio_buffer_direction direction,
122 					const struct iio_buffer_setup_ops *ops,
123 					const struct iio_dev_attr **buffer_attrs)
124 {
125 	int ret;
126 
127 	ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, direction,
128 					     ops, buffer_attrs);
129 	if (ret)
130 		return ret;
131 
132 	return devm_add_action_or_reset(dev, devm_iio_triggered_buffer_clean,
133 					indio_dev);
134 }
135 EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup_ext);
136 
137 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
138 MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
139 MODULE_LICENSE("GPL");
140