162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci /* 362306a36Sopenharmony_ci * Copyright (c) 2012 Analog Devices, Inc. 462306a36Sopenharmony_ci * Author: Lars-Peter Clausen <lars@metafoo.de> 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include <linux/kernel.h> 862306a36Sopenharmony_ci#include <linux/export.h> 962306a36Sopenharmony_ci#include <linux/module.h> 1062306a36Sopenharmony_ci#include <linux/iio/iio.h> 1162306a36Sopenharmony_ci#include <linux/iio/buffer.h> 1262306a36Sopenharmony_ci#include <linux/iio/buffer_impl.h> 1362306a36Sopenharmony_ci#include <linux/iio/kfifo_buf.h> 1462306a36Sopenharmony_ci#include <linux/iio/triggered_buffer.h> 1562306a36Sopenharmony_ci#include <linux/iio/trigger_consumer.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci/** 1862306a36Sopenharmony_ci * iio_triggered_buffer_setup_ext() - Setup triggered buffer and pollfunc 1962306a36Sopenharmony_ci * @indio_dev: IIO device structure 2062306a36Sopenharmony_ci * @h: Function which will be used as pollfunc top half 2162306a36Sopenharmony_ci * @thread: Function which will be used as pollfunc bottom half 2262306a36Sopenharmony_ci * @direction: Direction of the data stream (in/out). 2362306a36Sopenharmony_ci * @setup_ops: Buffer setup functions to use for this device. 2462306a36Sopenharmony_ci * If NULL the default setup functions for triggered 2562306a36Sopenharmony_ci * buffers will be used. 2662306a36Sopenharmony_ci * @buffer_attrs: Extra sysfs buffer attributes for this IIO buffer 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * This function combines some common tasks which will normally be performed 2962306a36Sopenharmony_ci * when setting up a triggered buffer. It will allocate the buffer and the 3062306a36Sopenharmony_ci * pollfunc. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * Before calling this function the indio_dev structure should already be 3362306a36Sopenharmony_ci * completely initialized, but not yet registered. In practice this means that 3462306a36Sopenharmony_ci * this function should be called right before iio_device_register(). 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * To free the resources allocated by this function call 3762306a36Sopenharmony_ci * iio_triggered_buffer_cleanup(). 3862306a36Sopenharmony_ci */ 3962306a36Sopenharmony_ciint iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev, 4062306a36Sopenharmony_ci irqreturn_t (*h)(int irq, void *p), 4162306a36Sopenharmony_ci irqreturn_t (*thread)(int irq, void *p), 4262306a36Sopenharmony_ci enum iio_buffer_direction direction, 4362306a36Sopenharmony_ci const struct iio_buffer_setup_ops *setup_ops, 4462306a36Sopenharmony_ci const struct iio_dev_attr **buffer_attrs) 4562306a36Sopenharmony_ci{ 4662306a36Sopenharmony_ci struct iio_buffer *buffer; 4762306a36Sopenharmony_ci int ret; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci /* 5062306a36Sopenharmony_ci * iio_triggered_buffer_cleanup() assumes that the buffer allocated here 5162306a36Sopenharmony_ci * is assigned to indio_dev->buffer but this is only the case if this 5262306a36Sopenharmony_ci * function is the first caller to iio_device_attach_buffer(). If 5362306a36Sopenharmony_ci * indio_dev->buffer is already set then we can't proceed otherwise the 5462306a36Sopenharmony_ci * cleanup function will try to free a buffer that was not allocated here. 5562306a36Sopenharmony_ci */ 5662306a36Sopenharmony_ci if (indio_dev->buffer) 5762306a36Sopenharmony_ci return -EADDRINUSE; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci buffer = iio_kfifo_allocate(); 6062306a36Sopenharmony_ci if (!buffer) { 6162306a36Sopenharmony_ci ret = -ENOMEM; 6262306a36Sopenharmony_ci goto error_ret; 6362306a36Sopenharmony_ci } 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci indio_dev->pollfunc = iio_alloc_pollfunc(h, 6662306a36Sopenharmony_ci thread, 6762306a36Sopenharmony_ci IRQF_ONESHOT, 6862306a36Sopenharmony_ci indio_dev, 6962306a36Sopenharmony_ci "%s_consumer%d", 7062306a36Sopenharmony_ci indio_dev->name, 7162306a36Sopenharmony_ci iio_device_id(indio_dev)); 7262306a36Sopenharmony_ci if (indio_dev->pollfunc == NULL) { 7362306a36Sopenharmony_ci ret = -ENOMEM; 7462306a36Sopenharmony_ci goto error_kfifo_free; 7562306a36Sopenharmony_ci } 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci /* Ring buffer functions - here trigger setup related */ 7862306a36Sopenharmony_ci indio_dev->setup_ops = setup_ops; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci /* Flag that polled ring buffering is possible */ 8162306a36Sopenharmony_ci indio_dev->modes |= INDIO_BUFFER_TRIGGERED; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci buffer->direction = direction; 8462306a36Sopenharmony_ci buffer->attrs = buffer_attrs; 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci ret = iio_device_attach_buffer(indio_dev, buffer); 8762306a36Sopenharmony_ci if (ret < 0) 8862306a36Sopenharmony_ci goto error_dealloc_pollfunc; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci return 0; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_cierror_dealloc_pollfunc: 9362306a36Sopenharmony_ci iio_dealloc_pollfunc(indio_dev->pollfunc); 9462306a36Sopenharmony_cierror_kfifo_free: 9562306a36Sopenharmony_ci iio_kfifo_free(buffer); 9662306a36Sopenharmony_cierror_ret: 9762306a36Sopenharmony_ci return ret; 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ciEXPORT_SYMBOL(iio_triggered_buffer_setup_ext); 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci/** 10262306a36Sopenharmony_ci * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup_ext() 10362306a36Sopenharmony_ci * @indio_dev: IIO device structure 10462306a36Sopenharmony_ci */ 10562306a36Sopenharmony_civoid iio_triggered_buffer_cleanup(struct iio_dev *indio_dev) 10662306a36Sopenharmony_ci{ 10762306a36Sopenharmony_ci iio_dealloc_pollfunc(indio_dev->pollfunc); 10862306a36Sopenharmony_ci iio_kfifo_free(indio_dev->buffer); 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ciEXPORT_SYMBOL(iio_triggered_buffer_cleanup); 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_cistatic void devm_iio_triggered_buffer_clean(void *indio_dev) 11362306a36Sopenharmony_ci{ 11462306a36Sopenharmony_ci iio_triggered_buffer_cleanup(indio_dev); 11562306a36Sopenharmony_ci} 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ciint devm_iio_triggered_buffer_setup_ext(struct device *dev, 11862306a36Sopenharmony_ci struct iio_dev *indio_dev, 11962306a36Sopenharmony_ci irqreturn_t (*h)(int irq, void *p), 12062306a36Sopenharmony_ci irqreturn_t (*thread)(int irq, void *p), 12162306a36Sopenharmony_ci enum iio_buffer_direction direction, 12262306a36Sopenharmony_ci const struct iio_buffer_setup_ops *ops, 12362306a36Sopenharmony_ci const struct iio_dev_attr **buffer_attrs) 12462306a36Sopenharmony_ci{ 12562306a36Sopenharmony_ci int ret; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, direction, 12862306a36Sopenharmony_ci ops, buffer_attrs); 12962306a36Sopenharmony_ci if (ret) 13062306a36Sopenharmony_ci return ret; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci return devm_add_action_or_reset(dev, devm_iio_triggered_buffer_clean, 13362306a36Sopenharmony_ci indio_dev); 13462306a36Sopenharmony_ci} 13562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup_ext); 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ciMODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 13862306a36Sopenharmony_ciMODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers"); 13962306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 140