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 buffer = iio_kfifo_allocate(); 50 if (!buffer) { 51 ret = -ENOMEM; 52 goto error_ret; 53 } 54 55 indio_dev->pollfunc = iio_alloc_pollfunc(h, 56 thread, 57 IRQF_ONESHOT, 58 indio_dev, 59 "%s_consumer%d", 60 indio_dev->name, 61 iio_device_id(indio_dev)); 62 if (indio_dev->pollfunc == NULL) { 63 ret = -ENOMEM; 64 goto error_kfifo_free; 65 } 66 67 /* Ring buffer functions - here trigger setup related */ 68 indio_dev->setup_ops = setup_ops; 69 70 /* Flag that polled ring buffering is possible */ 71 indio_dev->modes |= INDIO_BUFFER_TRIGGERED; 72 73 buffer->direction = direction; 74 buffer->attrs = buffer_attrs; 75 76 ret = iio_device_attach_buffer(indio_dev, buffer); 77 if (ret < 0) 78 goto error_dealloc_pollfunc; 79 80 return 0; 81 82 error_dealloc_pollfunc: 83 iio_dealloc_pollfunc(indio_dev->pollfunc); 84 error_kfifo_free: 85 iio_kfifo_free(buffer); 86 error_ret: 87 return ret; 88 } 89 EXPORT_SYMBOL(iio_triggered_buffer_setup_ext); 90 91 /** 92 * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup_ext() 93 * @indio_dev: IIO device structure 94 */ 95 void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev) 96 { 97 iio_dealloc_pollfunc(indio_dev->pollfunc); 98 iio_kfifo_free(indio_dev->buffer); 99 } 100 EXPORT_SYMBOL(iio_triggered_buffer_cleanup); 101 102 static void devm_iio_triggered_buffer_clean(void *indio_dev) 103 { 104 iio_triggered_buffer_cleanup(indio_dev); 105 } 106 107 int devm_iio_triggered_buffer_setup_ext(struct device *dev, 108 struct iio_dev *indio_dev, 109 irqreturn_t (*h)(int irq, void *p), 110 irqreturn_t (*thread)(int irq, void *p), 111 enum iio_buffer_direction direction, 112 const struct iio_buffer_setup_ops *ops, 113 const struct iio_dev_attr **buffer_attrs) 114 { 115 int ret; 116 117 ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, direction, 118 ops, buffer_attrs); 119 if (ret) 120 return ret; 121 122 return devm_add_action_or_reset(dev, devm_iio_triggered_buffer_clean, 123 indio_dev); 124 } 125 EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup_ext); 126 127 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 128 MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers"); 129 MODULE_LICENSE("GPL"); 130