2 * Copyright (c) 2012 Analog Devices, Inc.
3 * Author: Lars-Peter Clausen <lars@metafoo.de>
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.
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>
19 static 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
,
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.
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.
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().
42 * To free the resources allocated by this function call
43 * iio_triggered_buffer_cleanup().
45 int 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
)
50 struct iio_buffer
*buffer
;
53 buffer
= iio_kfifo_allocate(indio_dev
);
59 iio_device_attach_buffer(indio_dev
, buffer
);
61 indio_dev
->pollfunc
= iio_alloc_pollfunc(pollfunc_bh
,
68 if (indio_dev
->pollfunc
== NULL
) {
70 goto error_kfifo_free
;
73 /* Ring buffer functions - here trigger setup related */
75 indio_dev
->setup_ops
= setup_ops
;
77 indio_dev
->setup_ops
= &iio_triggered_buffer_setup_ops
;
79 /* Flag that polled ring buffering is possible */
80 indio_dev
->modes
|= INDIO_BUFFER_TRIGGERED
;
82 ret
= iio_buffer_register(indio_dev
,
84 indio_dev
->num_channels
);
86 goto error_dealloc_pollfunc
;
90 error_dealloc_pollfunc
:
91 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
93 iio_kfifo_free(indio_dev
->buffer
);
97 EXPORT_SYMBOL(iio_triggered_buffer_setup
);
100 * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup()
101 * @indio_dev: IIO device structure
103 void iio_triggered_buffer_cleanup(struct iio_dev
*indio_dev
)
105 iio_buffer_unregister(indio_dev
);
106 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
107 iio_kfifo_free(indio_dev
->buffer
);
109 EXPORT_SYMBOL(iio_triggered_buffer_cleanup
);
111 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
112 MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
113 MODULE_LICENSE("GPL");