Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 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 | |
| 19 | static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { |
Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 20 | .postenable = &iio_triggered_buffer_postenable, |
| 21 | .predisable = &iio_triggered_buffer_predisable, |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * iio_triggered_buffer_setup() - Setup triggered buffer and pollfunc |
| 26 | * @indio_dev: IIO device structure |
| 27 | * @pollfunc_bh: Function which will be used as pollfunc bottom half |
| 28 | * @pollfunc_th: Function which will be used as pollfunc top half |
| 29 | * @setup_ops: Buffer setup functions to use for this device. |
| 30 | * If NULL the default setup functions for triggered |
| 31 | * buffers will be used. |
| 32 | * |
| 33 | * This function combines some common tasks which will normally be performed |
| 34 | * when setting up a triggered buffer. It will allocate the buffer and the |
Lars-Peter Clausen | 3e1b6c9 | 2014-11-26 18:55:12 +0100 | [diff] [blame] | 35 | * pollfunc. |
Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 36 | * |
| 37 | * Before calling this function the indio_dev structure should already be |
| 38 | * completely initialized, but not yet registered. In practice this means that |
| 39 | * this function should be called right before iio_device_register(). |
| 40 | * |
| 41 | * To free the resources allocated by this function call |
| 42 | * iio_triggered_buffer_cleanup(). |
| 43 | */ |
| 44 | int iio_triggered_buffer_setup(struct iio_dev *indio_dev, |
| 45 | irqreturn_t (*pollfunc_bh)(int irq, void *p), |
| 46 | irqreturn_t (*pollfunc_th)(int irq, void *p), |
| 47 | const struct iio_buffer_setup_ops *setup_ops) |
| 48 | { |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 49 | struct iio_buffer *buffer; |
Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 50 | int ret; |
| 51 | |
Karol Wrona | 7ab374a | 2014-12-19 18:39:24 +0100 | [diff] [blame] | 52 | buffer = iio_kfifo_allocate(); |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 53 | if (!buffer) { |
Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 54 | ret = -ENOMEM; |
| 55 | goto error_ret; |
| 56 | } |
| 57 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 58 | iio_device_attach_buffer(indio_dev, buffer); |
| 59 | |
Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 60 | indio_dev->pollfunc = iio_alloc_pollfunc(pollfunc_bh, |
| 61 | pollfunc_th, |
| 62 | IRQF_ONESHOT, |
| 63 | indio_dev, |
| 64 | "%s_consumer%d", |
| 65 | indio_dev->name, |
| 66 | indio_dev->id); |
| 67 | if (indio_dev->pollfunc == NULL) { |
| 68 | ret = -ENOMEM; |
| 69 | goto error_kfifo_free; |
| 70 | } |
| 71 | |
| 72 | /* Ring buffer functions - here trigger setup related */ |
| 73 | if (setup_ops) |
| 74 | indio_dev->setup_ops = setup_ops; |
| 75 | else |
| 76 | indio_dev->setup_ops = &iio_triggered_buffer_setup_ops; |
| 77 | |
| 78 | /* Flag that polled ring buffering is possible */ |
| 79 | indio_dev->modes |= INDIO_BUFFER_TRIGGERED; |
| 80 | |
Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 81 | return 0; |
| 82 | |
Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 83 | error_kfifo_free: |
| 84 | iio_kfifo_free(indio_dev->buffer); |
| 85 | error_ret: |
| 86 | return ret; |
| 87 | } |
| 88 | EXPORT_SYMBOL(iio_triggered_buffer_setup); |
| 89 | |
| 90 | /** |
| 91 | * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup() |
| 92 | * @indio_dev: IIO device structure |
| 93 | */ |
| 94 | void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev) |
| 95 | { |
Lars-Peter Clausen | 23f2d73 | 2012-06-18 18:33:48 +0200 | [diff] [blame] | 96 | iio_dealloc_pollfunc(indio_dev->pollfunc); |
| 97 | iio_kfifo_free(indio_dev->buffer); |
| 98 | } |
| 99 | EXPORT_SYMBOL(iio_triggered_buffer_cleanup); |
| 100 | |
| 101 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 102 | MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers"); |
| 103 | MODULE_LICENSE("GPL"); |