aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLars-Peter Clausen <lars@metafoo.de>2012-06-18 12:33:48 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-06-18 20:26:09 -0400
commit23f2d735a932c7833d2d00da5e3ecdf4a6836210 (patch)
tree2e0d4da782473ccd226fc6f4221b2c0bc92ba411 /drivers/iio
parentf5b81ddd12da71bd00b2963203c23ff929e0c182 (diff)
iio: Add helper function for initializing triggered buffers
Add a helper function for executing the common tasks which are usually involved in setting up a simple software ringbuffer. It will allocate the buffer, allocate the pollfunc and register the buffer. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Acked-by: Jonathan Cameron <jic23@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/Kconfig7
-rw-r--r--drivers/iio/Makefile1
-rw-r--r--drivers/iio/industrialio-triggered-buffer.c110
3 files changed, 118 insertions, 0 deletions
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index 103349f2b3b5..612073f6c540 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -30,6 +30,13 @@ config IIO_KFIFO_BUF
30 no buffer events so it is up to userspace to work out how 30 no buffer events so it is up to userspace to work out how
31 often to read from the buffer. 31 often to read from the buffer.
32 32
33config IIO_TRIGGERED_BUFFER
34 tristate
35 select IIO_TRIGGER
36 select IIO_KFIFO_BUF
37 help
38 Provides helper functions for setting up triggered buffers.
39
33endif # IIO_BUFFER 40endif # IIO_BUFFER
34 41
35config IIO_TRIGGER 42config IIO_TRIGGER
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index c38fa2a40af2..34309abb7979 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -7,6 +7,7 @@ industrialio-y := industrialio-core.o industrialio-event.o inkern.o
7industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o 7industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o
8industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o 8industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
9 9
10obj-$(CONFIG_IIO_TRIGGERED_BUFFER) += industrialio-triggered-buffer.o
10obj-$(CONFIG_IIO_KFIFO_BUF) += kfifo_buf.o 11obj-$(CONFIG_IIO_KFIFO_BUF) += kfifo_buf.o
11 12
12obj-y += adc/ 13obj-y += adc/
diff --git a/drivers/iio/industrialio-triggered-buffer.c b/drivers/iio/industrialio-triggered-buffer.c
new file mode 100644
index 000000000000..46c619b0d8c5
--- /dev/null
+++ b/drivers/iio/industrialio-triggered-buffer.c
@@ -0,0 +1,110 @@
1 /*
2 * Copyright (c) 2012 Analog Devices, Inc.
3 * Author: Lars-Peter Clausen <lars@metafoo.de>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/export.h>
12#include <linux/module.h>
13#include <linux/iio/iio.h>
14#include <linux/iio/buffer.h>
15#include <linux/iio/kfifo_buf.h>
16#include <linux/iio/triggered_buffer.h>
17#include <linux/iio/trigger_consumer.h>
18
19static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
20 .preenable = &iio_sw_buffer_preenable,
21 .postenable = &iio_triggered_buffer_postenable,
22 .predisable = &iio_triggered_buffer_predisable,
23};
24
25/**
26 * iio_triggered_buffer_setup() - Setup triggered buffer and pollfunc
27 * @indio_dev: IIO device structure
28 * @pollfunc_bh: Function which will be used as pollfunc bottom half
29 * @pollfunc_th: Function which will be used as pollfunc top half
30 * @setup_ops: Buffer setup functions to use for this device.
31 * If NULL the default setup functions for triggered
32 * buffers will be used.
33 *
34 * This function combines some common tasks which will normally be performed
35 * when setting up a triggered buffer. It will allocate the buffer and the
36 * pollfunc, as well as register the buffer with the IIO core.
37 *
38 * Before calling this function the indio_dev structure should already be
39 * completely initialized, but not yet registered. In practice this means that
40 * this function should be called right before iio_device_register().
41 *
42 * To free the resources allocated by this function call
43 * iio_triggered_buffer_cleanup().
44 */
45int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
46 irqreturn_t (*pollfunc_bh)(int irq, void *p),
47 irqreturn_t (*pollfunc_th)(int irq, void *p),
48 const struct iio_buffer_setup_ops *setup_ops)
49{
50 int ret;
51
52 indio_dev->buffer = iio_kfifo_allocate(indio_dev);
53 if (!indio_dev->buffer) {
54 ret = -ENOMEM;
55 goto error_ret;
56 }
57
58 indio_dev->pollfunc = iio_alloc_pollfunc(pollfunc_bh,
59 pollfunc_th,
60 IRQF_ONESHOT,
61 indio_dev,
62 "%s_consumer%d",
63 indio_dev->name,
64 indio_dev->id);
65 if (indio_dev->pollfunc == NULL) {
66 ret = -ENOMEM;
67 goto error_kfifo_free;
68 }
69
70 /* Ring buffer functions - here trigger setup related */
71 if (setup_ops)
72 indio_dev->setup_ops = setup_ops;
73 else
74 indio_dev->setup_ops = &iio_triggered_buffer_setup_ops;
75
76 /* Flag that polled ring buffering is possible */
77 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
78
79 ret = iio_buffer_register(indio_dev,
80 indio_dev->channels,
81 indio_dev->num_channels);
82 if (ret)
83 goto error_dealloc_pollfunc;
84
85 return 0;
86
87error_dealloc_pollfunc:
88 iio_dealloc_pollfunc(indio_dev->pollfunc);
89error_kfifo_free:
90 iio_kfifo_free(indio_dev->buffer);
91error_ret:
92 return ret;
93}
94EXPORT_SYMBOL(iio_triggered_buffer_setup);
95
96/**
97 * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup()
98 * @indio_dev: IIO device structure
99 */
100void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
101{
102 iio_buffer_unregister(indio_dev);
103 iio_dealloc_pollfunc(indio_dev->pollfunc);
104 iio_kfifo_free(indio_dev->buffer);
105}
106EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
107
108MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
109MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
110MODULE_LICENSE("GPL");