1 // SPDX-License-Identifier: GPL-2.0-only
3 * Framework to handle complex IIO aggregate devices.
5 * The typical architecture is to have one device as the frontend device which
6 * can be "linked" against one or multiple backend devices. All the IIO and
7 * userspace interface is expected to be registers/managed by the frontend
8 * device which will callback into the backends when needed (to get/set some
9 * configuration that it does not directly control).
11 * -------------------------------------------------------
12 * ------------------ | ------------ ------------ ------- FPGA|
13 * | ADC |------------------------| | ADC CORE |---------| DMA CORE |------| RAM | |
14 * | (Frontend/IIO) | Serial Data (eg: LVDS) | |(backend) |---------| |------| | |
15 * | |------------------------| ------------ ------------ ------- |
16 * ------------------ -------------------------------------------------------
18 * The framework interface is pretty simple:
19 * - Backends should register themselves with devm_iio_backend_register()
20 * - Frontend devices should get backends with devm_iio_backend_get()
22 * Also to note that the primary target for this framework are converters like
23 * ADC/DACs so iio_backend_ops will have some operations typical of converter
24 * devices. On top of that, this is "generic" for all IIO which means any kind
25 * of device can make use of the framework. That said, If the iio_backend_ops
26 * struct begins to grow out of control, we can always refactor things so that
27 * the industrialio-backend.c is only left with the really generic stuff. Then,
28 * we can build on top of it depending on the needs.
30 * Copyright (C) 2023-2024 Analog Devices Inc.
32 #define dev_fmt(fmt) "iio-backend: " fmt
34 #include <linux/cleanup.h>
35 #include <linux/device.h>
36 #include <linux/err.h>
37 #include <linux/errno.h>
38 #include <linux/list.h>
39 #include <linux/module.h>
40 #include <linux/mutex.h>
41 #include <linux/property.h>
42 #include <linux/slab.h>
43 #include <linux/types.h>
45 #include <linux/iio/backend.h>
48 struct list_head entry;
49 const struct iio_backend_ops *ops;
56 * Helper struct for requesting buffers. This ensures that we have all data
57 * that we need to free the buffer in a device managed action.
59 struct iio_backend_buffer_pair {
60 struct iio_backend *back;
61 struct iio_buffer *buffer;
64 static LIST_HEAD(iio_back_list);
65 static DEFINE_MUTEX(iio_back_lock);
68 * Helper macros to call backend ops. Makes sure the option is supported.
70 #define iio_backend_check_op(back, op) ({ \
71 struct iio_backend *____back = back; \
74 if (!____back->ops->op) \
75 ____ret = -EOPNOTSUPP; \
80 #define iio_backend_op_call(back, op, args...) ({ \
81 struct iio_backend *__back = back; \
84 __ret = iio_backend_check_op(__back, op); \
86 __ret = __back->ops->op(__back, ##args); \
91 #define iio_backend_ptr_op_call(back, op, args...) ({ \
92 struct iio_backend *__back = back; \
96 __ret = iio_backend_check_op(__back, op); \
98 ptr_err = ERR_PTR(__ret); \
100 ptr_err = __back->ops->op(__back, ##args); \
105 #define iio_backend_void_op_call(back, op, args...) { \
106 struct iio_backend *__back = back; \
109 __ret = iio_backend_check_op(__back, op); \
111 __back->ops->op(__back, ##args); \
115 * iio_backend_chan_enable - Enable a backend channel
116 * @back: Backend device
117 * @chan: Channel number
120 * 0 on success, negative error number on failure.
122 int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan)
124 return iio_backend_op_call(back, chan_enable, chan);
126 EXPORT_SYMBOL_NS_GPL(iio_backend_chan_enable, IIO_BACKEND);
129 * iio_backend_chan_disable - Disable a backend channel
130 * @back: Backend device
131 * @chan: Channel number
134 * 0 on success, negative error number on failure.
136 int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan)
138 return iio_backend_op_call(back, chan_disable, chan);
140 EXPORT_SYMBOL_NS_GPL(iio_backend_chan_disable, IIO_BACKEND);
142 static void __iio_backend_disable(void *back)
144 iio_backend_void_op_call(back, disable);
148 * devm_iio_backend_enable - Device managed backend enable
149 * @dev: Consumer device for the backend
150 * @back: Backend device
153 * 0 on success, negative error number on failure.
155 int devm_iio_backend_enable(struct device *dev, struct iio_backend *back)
159 ret = iio_backend_op_call(back, enable);
163 return devm_add_action_or_reset(dev, __iio_backend_disable, back);
165 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_enable, IIO_BACKEND);
168 * iio_backend_data_format_set - Configure the channel data format
169 * @back: Backend device
170 * @chan: Channel number
173 * Properly configure a channel with respect to the expected data format. A
174 * @struct iio_backend_data_fmt must be passed with the settings.
177 * 0 on success, negative error number on failure.
179 int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan,
180 const struct iio_backend_data_fmt *data)
182 if (!data || data->type >= IIO_BACKEND_DATA_TYPE_MAX)
185 return iio_backend_op_call(back, data_format_set, chan, data);
187 EXPORT_SYMBOL_NS_GPL(iio_backend_data_format_set, IIO_BACKEND);
189 static void iio_backend_free_buffer(void *arg)
191 struct iio_backend_buffer_pair *pair = arg;
193 iio_backend_void_op_call(pair->back, free_buffer, pair->buffer);
197 * devm_iio_backend_request_buffer - Device managed buffer request
198 * @dev: Consumer device for the backend
199 * @back: Backend device
200 * @indio_dev: IIO device
202 * Request an IIO buffer from the backend. The type of the buffer (typically
203 * INDIO_BUFFER_HARDWARE) is up to the backend to decide. This is because,
204 * normally, the backend dictates what kind of buffering we can get.
206 * The backend .free_buffer() hooks is automatically called on @dev detach.
209 * 0 on success, negative error number on failure.
211 int devm_iio_backend_request_buffer(struct device *dev,
212 struct iio_backend *back,
213 struct iio_dev *indio_dev)
215 struct iio_backend_buffer_pair *pair;
216 struct iio_buffer *buffer;
218 pair = devm_kzalloc(dev, sizeof(*pair), GFP_KERNEL);
222 buffer = iio_backend_ptr_op_call(back, request_buffer, indio_dev);
224 return PTR_ERR(buffer);
226 /* weak reference should be all what we need */
228 pair->buffer = buffer;
230 return devm_add_action_or_reset(dev, iio_backend_free_buffer, pair);
232 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, IIO_BACKEND);
234 static void iio_backend_release(void *arg)
236 struct iio_backend *back = arg;
238 module_put(back->owner);
241 static int __devm_iio_backend_get(struct device *dev, struct iio_backend *back)
243 struct device_link *link;
247 * Make sure the provider cannot be unloaded before the consumer module.
248 * Note that device_links would still guarantee that nothing is
249 * accessible (and breaks) but this makes it explicit that the consumer
250 * module must be also unloaded.
252 if (!try_module_get(back->owner))
253 return dev_err_probe(dev, -ENODEV,
254 "Cannot get module reference\n");
256 ret = devm_add_action_or_reset(dev, iio_backend_release, back);
260 link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
262 return dev_err_probe(dev, -EINVAL,
263 "Could not link to supplier(%s)\n",
264 dev_name(back->dev));
266 dev_dbg(dev, "Found backend(%s) device\n", dev_name(back->dev));
272 * devm_iio_backend_get - Device managed backend device get
273 * @dev: Consumer device for the backend
274 * @name: Backend name
276 * Get's the backend associated with @dev.
279 * A backend pointer, negative error pointer otherwise.
281 struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name)
283 struct fwnode_handle *fwnode;
284 struct iio_backend *back;
289 ret = device_property_match_string(dev, "io-backend-names",
298 fwnode = fwnode_find_reference(dev_fwnode(dev), "io-backends", index);
299 if (IS_ERR(fwnode)) {
300 dev_err_probe(dev, PTR_ERR(fwnode),
301 "Cannot get Firmware reference\n");
302 return ERR_CAST(fwnode);
305 guard(mutex)(&iio_back_lock);
306 list_for_each_entry(back, &iio_back_list, entry) {
307 if (!device_match_fwnode(back->dev, fwnode))
310 fwnode_handle_put(fwnode);
311 ret = __devm_iio_backend_get(dev, back);
318 fwnode_handle_put(fwnode);
319 return ERR_PTR(-EPROBE_DEFER);
321 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, IIO_BACKEND);
324 * __devm_iio_backend_get_from_fwnode_lookup - Device managed fwnode backend device get
325 * @dev: Consumer device for the backend
326 * @fwnode: Firmware node of the backend device
328 * Search the backend list for a device matching @fwnode.
329 * This API should not be used and it's only present for preventing the first
330 * user of this framework to break it's DT ABI.
333 * A backend pointer, negative error pointer otherwise.
336 __devm_iio_backend_get_from_fwnode_lookup(struct device *dev,
337 struct fwnode_handle *fwnode)
339 struct iio_backend *back;
342 guard(mutex)(&iio_back_lock);
343 list_for_each_entry(back, &iio_back_list, entry) {
344 if (!device_match_fwnode(back->dev, fwnode))
347 ret = __devm_iio_backend_get(dev, back);
354 return ERR_PTR(-EPROBE_DEFER);
356 EXPORT_SYMBOL_NS_GPL(__devm_iio_backend_get_from_fwnode_lookup, IIO_BACKEND);
359 * iio_backend_get_priv - Get driver private data
360 * @back: Backend device
362 void *iio_backend_get_priv(const struct iio_backend *back)
366 EXPORT_SYMBOL_NS_GPL(iio_backend_get_priv, IIO_BACKEND);
368 static void iio_backend_unregister(void *arg)
370 struct iio_backend *back = arg;
372 guard(mutex)(&iio_back_lock);
373 list_del(&back->entry);
377 * devm_iio_backend_register - Device managed backend device register
378 * @dev: Backend device being registered
380 * @priv: Device private data
382 * @ops is mandatory. Not providing it results in -EINVAL.
385 * 0 on success, negative error number on failure.
387 int devm_iio_backend_register(struct device *dev,
388 const struct iio_backend_ops *ops, void *priv)
390 struct iio_backend *back;
393 return dev_err_probe(dev, -EINVAL, "No backend ops given\n");
396 * Through device_links, we guarantee that a frontend device cannot be
397 * bound/exist if the backend driver is not around. Hence, we can bind
398 * the backend object lifetime with the device being passed since
399 * removing it will tear the frontend/consumer down.
401 back = devm_kzalloc(dev, sizeof(*back), GFP_KERNEL);
406 back->owner = dev->driver->owner;
409 scoped_guard(mutex, &iio_back_lock)
410 list_add(&back->entry, &iio_back_list);
412 return devm_add_action_or_reset(dev, iio_backend_unregister, back);
414 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_register, IIO_BACKEND);
416 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
417 MODULE_DESCRIPTION("Framework to handle complex IIO aggregate devices");
418 MODULE_LICENSE("GPL");