// SPDX-License-Identifier: GPL-2.0-only /* * Framework to handle complex IIO aggregate devices. * * The typical architecture is to have one device as the frontend device which * can be "linked" against one or multiple backend devices. All the IIO and * userspace interface is expected to be registers/managed by the frontend * device which will callback into the backends when needed (to get/set some * configuration that it does not directly control). * * ------------------------------------------------------- * ------------------ | ------------ ------------ ------- FPGA| * | ADC |------------------------| | ADC CORE |---------| DMA CORE |------| RAM | | * | (Frontend/IIO) | Serial Data (eg: LVDS) | |(backend) |---------| |------| | | * | |------------------------| ------------ ------------ ------- | * ------------------ ------------------------------------------------------- * * The framework interface is pretty simple: * - Backends should register themselves with devm_iio_backend_register() * - Frontend devices should get backends with devm_iio_backend_get() * * Also to note that the primary target for this framework are converters like * ADC/DACs so iio_backend_ops will have some operations typical of converter * devices. On top of that, this is "generic" for all IIO which means any kind * of device can make use of the framework. That said, If the iio_backend_ops * struct begins to grow out of control, we can always refactor things so that * the industrialio-backend.c is only left with the really generic stuff. Then, * we can build on top of it depending on the needs. * * Copyright (C) 2023-2024 Analog Devices Inc. */ #define dev_fmt(fmt) … #include <linux/cleanup.h> #include <linux/device.h> #include <linux/err.h> #include <linux/errno.h> #include <linux/list.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/property.h> #include <linux/slab.h> #include <linux/types.h> #include <linux/iio/backend.h> #include <linux/iio/iio.h> struct iio_backend { … }; /* * Helper struct for requesting buffers. This ensures that we have all data * that we need to free the buffer in a device managed action. */ struct iio_backend_buffer_pair { … }; static LIST_HEAD(iio_back_list); static DEFINE_MUTEX(iio_back_lock); /* * Helper macros to call backend ops. Makes sure the option is supported. */ #define iio_backend_check_op(back, op) … #define iio_backend_op_call(back, op, args...) … #define iio_backend_ptr_op_call(back, op, args...) … #define iio_backend_void_op_call(back, op, args...) … /** * iio_backend_chan_enable - Enable a backend channel * @back: Backend device * @chan: Channel number * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_chan_disable - Disable a backend channel * @back: Backend device * @chan: Channel number * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan) { … } EXPORT_SYMBOL_NS_GPL(…); static void __iio_backend_disable(void *back) { … } /** * devm_iio_backend_enable - Device managed backend enable * @dev: Consumer device for the backend * @back: Backend device * * RETURNS: * 0 on success, negative error number on failure. */ int devm_iio_backend_enable(struct device *dev, struct iio_backend *back) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_data_format_set - Configure the channel data format * @back: Backend device * @chan: Channel number * @data: Data format * * Properly configure a channel with respect to the expected data format. A * @struct iio_backend_data_fmt must be passed with the settings. * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan, const struct iio_backend_data_fmt *data) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_data_source_set - Select data source * @back: Backend device * @chan: Channel number * @data: Data source * * A given backend may have different sources to stream/sync data. This allows * to choose that source. * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan, enum iio_backend_data_source data) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_set_sampling_freq - Set channel sampling rate * @back: Backend device * @chan: Channel number * @sample_rate_hz: Sample rate * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan, u64 sample_rate_hz) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_test_pattern_set - Configure a test pattern * @back: Backend device * @chan: Channel number * @pattern: Test pattern * * Configure a test pattern on the backend. This is typically used for * calibrating the timings on the data digital interface. * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_test_pattern_set(struct iio_backend *back, unsigned int chan, enum iio_backend_test_pattern pattern) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_chan_status - Get the channel status * @back: Backend device * @chan: Channel number * @error: Error indication * * Get the current state of the backend channel. Typically used to check if * there were any errors sending/receiving data. * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_chan_status(struct iio_backend *back, unsigned int chan, bool *error) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_iodelay_set - Set digital I/O delay * @back: Backend device * @lane: Lane number * @taps: Number of taps * * Controls delays on sending/receiving data. One usecase for this is to * calibrate the data digital interface so we get the best results when * transferring data. Note that @taps has no unit since the actual delay per tap * is very backend specific. Hence, frontend devices typically should go through * an array of @taps (the size of that array should typically match the size of * calibration points on the frontend device) and call this API. * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane, unsigned int taps) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_data_sample_trigger - Control when to sample data * @back: Backend device * @trigger: Data trigger * * Mostly useful for input backends. Configures the backend for when to sample * data (eg: rising vs falling edge). * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_data_sample_trigger(struct iio_backend *back, enum iio_backend_sample_trigger trigger) { … } EXPORT_SYMBOL_NS_GPL(…); static void iio_backend_free_buffer(void *arg) { … } /** * devm_iio_backend_request_buffer - Device managed buffer request * @dev: Consumer device for the backend * @back: Backend device * @indio_dev: IIO device * * Request an IIO buffer from the backend. The type of the buffer (typically * INDIO_BUFFER_HARDWARE) is up to the backend to decide. This is because, * normally, the backend dictates what kind of buffering we can get. * * The backend .free_buffer() hooks is automatically called on @dev detach. * * RETURNS: * 0 on success, negative error number on failure. */ int devm_iio_backend_request_buffer(struct device *dev, struct iio_backend *back, struct iio_dev *indio_dev) { … } EXPORT_SYMBOL_NS_GPL(…); static struct iio_backend *iio_backend_from_indio_dev_parent(const struct device *dev) { … } /** * iio_backend_ext_info_get - IIO ext_info read callback * @indio_dev: IIO device * @private: Data private to the driver * @chan: IIO channel * @buf: Buffer where to place the attribute data * * This helper is intended to be used by backends that extend an IIO channel * (through iio_backend_extend_chan_spec()) with extended info. In that case, * backends are not supposed to give their own callbacks (as they would not have * a way to get the backend from indio_dev). This is the getter. * * RETURNS: * Number of bytes written to buf, negative error number on failure. */ ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private, const struct iio_chan_spec *chan, char *buf) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_ext_info_set - IIO ext_info write callback * @indio_dev: IIO device * @private: Data private to the driver * @chan: IIO channel * @buf: Buffer holding the sysfs attribute * @len: Buffer length * * This helper is intended to be used by backends that extend an IIO channel * (trough iio_backend_extend_chan_spec()) with extended info. In that case, * backends are not supposed to give their own callbacks (as they would not have * a way to get the backend from indio_dev). This is the setter. * * RETURNS: * Buffer length on success, negative error number on failure. */ ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private, const struct iio_chan_spec *chan, const char *buf, size_t len) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_extend_chan_spec - Extend an IIO channel * @indio_dev: IIO device * @back: Backend device * @chan: IIO channel * * Some backends may have their own functionalities and hence capable of * extending a frontend's channel. * * RETURNS: * 0 on success, negative error number on failure. */ int iio_backend_extend_chan_spec(struct iio_dev *indio_dev, struct iio_backend *back, struct iio_chan_spec *chan) { … } EXPORT_SYMBOL_NS_GPL(…); static void iio_backend_release(void *arg) { … } static int __devm_iio_backend_get(struct device *dev, struct iio_backend *back) { … } /** * devm_iio_backend_get - Device managed backend device get * @dev: Consumer device for the backend * @name: Backend name * * Get's the backend associated with @dev. * * RETURNS: * A backend pointer, negative error pointer otherwise. */ struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name) { … } EXPORT_SYMBOL_NS_GPL(…); /** * __devm_iio_backend_get_from_fwnode_lookup - Device managed fwnode backend device get * @dev: Consumer device for the backend * @fwnode: Firmware node of the backend device * * Search the backend list for a device matching @fwnode. * This API should not be used and it's only present for preventing the first * user of this framework to break it's DT ABI. * * RETURNS: * A backend pointer, negative error pointer otherwise. */ struct iio_backend * __devm_iio_backend_get_from_fwnode_lookup(struct device *dev, struct fwnode_handle *fwnode) { … } EXPORT_SYMBOL_NS_GPL(…); /** * iio_backend_get_priv - Get driver private data * @back: Backend device */ void *iio_backend_get_priv(const struct iio_backend *back) { … } EXPORT_SYMBOL_NS_GPL(…); static void iio_backend_unregister(void *arg) { … } /** * devm_iio_backend_register - Device managed backend device register * @dev: Backend device being registered * @ops: Backend ops * @priv: Device private data * * @ops is mandatory. Not providing it results in -EINVAL. * * RETURNS: * 0 on success, negative error number on failure. */ int devm_iio_backend_register(struct device *dev, const struct iio_backend_ops *ops, void *priv) { … } EXPORT_SYMBOL_NS_GPL(…); MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;