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>
46 #include <linux/iio/iio.h>
49 struct list_head entry;
50 const struct iio_backend_ops *ops;
51 struct device *frontend_dev;
58 * Helper struct for requesting buffers. This ensures that we have all data
59 * that we need to free the buffer in a device managed action.
61 struct iio_backend_buffer_pair {
62 struct iio_backend *back;
63 struct iio_buffer *buffer;
66 static LIST_HEAD(iio_back_list);
67 static DEFINE_MUTEX(iio_back_lock);
70 * Helper macros to call backend ops. Makes sure the option is supported.
72 #define iio_backend_check_op(back, op) ({ \
73 struct iio_backend *____back = back; \
76 if (!____back->ops->op) \
77 ____ret = -EOPNOTSUPP; \
82 #define iio_backend_op_call(back, op, args...) ({ \
83 struct iio_backend *__back = back; \
86 __ret = iio_backend_check_op(__back, op); \
88 __ret = __back->ops->op(__back, ##args); \
93 #define iio_backend_ptr_op_call(back, op, args...) ({ \
94 struct iio_backend *__back = back; \
98 __ret = iio_backend_check_op(__back, op); \
100 ptr_err = ERR_PTR(__ret); \
102 ptr_err = __back->ops->op(__back, ##args); \
107 #define iio_backend_void_op_call(back, op, args...) { \
108 struct iio_backend *__back = back; \
111 __ret = iio_backend_check_op(__back, op); \
113 __back->ops->op(__back, ##args); \
117 * iio_backend_chan_enable - Enable a backend channel
118 * @back: Backend device
119 * @chan: Channel number
122 * 0 on success, negative error number on failure.
124 int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan)
126 return iio_backend_op_call(back, chan_enable, chan);
128 EXPORT_SYMBOL_NS_GPL(iio_backend_chan_enable, IIO_BACKEND);
131 * iio_backend_chan_disable - Disable a backend channel
132 * @back: Backend device
133 * @chan: Channel number
136 * 0 on success, negative error number on failure.
138 int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan)
140 return iio_backend_op_call(back, chan_disable, chan);
142 EXPORT_SYMBOL_NS_GPL(iio_backend_chan_disable, IIO_BACKEND);
144 static void __iio_backend_disable(void *back)
146 iio_backend_void_op_call(back, disable);
150 * devm_iio_backend_enable - Device managed backend enable
151 * @dev: Consumer device for the backend
152 * @back: Backend device
155 * 0 on success, negative error number on failure.
157 int devm_iio_backend_enable(struct device *dev, struct iio_backend *back)
161 ret = iio_backend_op_call(back, enable);
165 return devm_add_action_or_reset(dev, __iio_backend_disable, back);
167 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_enable, IIO_BACKEND);
170 * iio_backend_data_format_set - Configure the channel data format
171 * @back: Backend device
172 * @chan: Channel number
175 * Properly configure a channel with respect to the expected data format. A
176 * @struct iio_backend_data_fmt must be passed with the settings.
179 * 0 on success, negative error number on failure.
181 int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan,
182 const struct iio_backend_data_fmt *data)
184 if (!data || data->type >= IIO_BACKEND_DATA_TYPE_MAX)
187 return iio_backend_op_call(back, data_format_set, chan, data);
189 EXPORT_SYMBOL_NS_GPL(iio_backend_data_format_set, IIO_BACKEND);
192 * iio_backend_data_source_set - Select data source
193 * @back: Backend device
194 * @chan: Channel number
197 * A given backend may have different sources to stream/sync data. This allows
198 * to choose that source.
201 * 0 on success, negative error number on failure.
203 int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan,
204 enum iio_backend_data_source data)
206 if (data >= IIO_BACKEND_DATA_SOURCE_MAX)
209 return iio_backend_op_call(back, data_source_set, chan, data);
211 EXPORT_SYMBOL_NS_GPL(iio_backend_data_source_set, IIO_BACKEND);
214 * iio_backend_set_sampling_freq - Set channel sampling rate
215 * @back: Backend device
216 * @chan: Channel number
217 * @sample_rate_hz: Sample rate
220 * 0 on success, negative error number on failure.
222 int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan,
225 return iio_backend_op_call(back, set_sample_rate, chan, sample_rate_hz);
227 EXPORT_SYMBOL_NS_GPL(iio_backend_set_sampling_freq, IIO_BACKEND);
230 * iio_backend_test_pattern_set - Configure a test pattern
231 * @back: Backend device
232 * @chan: Channel number
233 * @pattern: Test pattern
235 * Configure a test pattern on the backend. This is typically used for
236 * calibrating the timings on the data digital interface.
239 * 0 on success, negative error number on failure.
241 int iio_backend_test_pattern_set(struct iio_backend *back,
243 enum iio_backend_test_pattern pattern)
245 if (pattern >= IIO_BACKEND_TEST_PATTERN_MAX)
248 return iio_backend_op_call(back, test_pattern_set, chan, pattern);
250 EXPORT_SYMBOL_NS_GPL(iio_backend_test_pattern_set, IIO_BACKEND);
253 * iio_backend_chan_status - Get the channel status
254 * @back: Backend device
255 * @chan: Channel number
256 * @error: Error indication
258 * Get the current state of the backend channel. Typically used to check if
259 * there were any errors sending/receiving data.
262 * 0 on success, negative error number on failure.
264 int iio_backend_chan_status(struct iio_backend *back, unsigned int chan,
267 return iio_backend_op_call(back, chan_status, chan, error);
269 EXPORT_SYMBOL_NS_GPL(iio_backend_chan_status, IIO_BACKEND);
272 * iio_backend_iodelay_set - Set digital I/O delay
273 * @back: Backend device
275 * @taps: Number of taps
277 * Controls delays on sending/receiving data. One usecase for this is to
278 * calibrate the data digital interface so we get the best results when
279 * transferring data. Note that @taps has no unit since the actual delay per tap
280 * is very backend specific. Hence, frontend devices typically should go through
281 * an array of @taps (the size of that array should typically match the size of
282 * calibration points on the frontend device) and call this API.
285 * 0 on success, negative error number on failure.
287 int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane,
290 return iio_backend_op_call(back, iodelay_set, lane, taps);
292 EXPORT_SYMBOL_NS_GPL(iio_backend_iodelay_set, IIO_BACKEND);
295 * iio_backend_data_sample_trigger - Control when to sample data
296 * @back: Backend device
297 * @trigger: Data trigger
299 * Mostly useful for input backends. Configures the backend for when to sample
300 * data (eg: rising vs falling edge).
303 * 0 on success, negative error number on failure.
305 int iio_backend_data_sample_trigger(struct iio_backend *back,
306 enum iio_backend_sample_trigger trigger)
308 if (trigger >= IIO_BACKEND_SAMPLE_TRIGGER_MAX)
311 return iio_backend_op_call(back, data_sample_trigger, trigger);
313 EXPORT_SYMBOL_NS_GPL(iio_backend_data_sample_trigger, IIO_BACKEND);
315 static void iio_backend_free_buffer(void *arg)
317 struct iio_backend_buffer_pair *pair = arg;
319 iio_backend_void_op_call(pair->back, free_buffer, pair->buffer);
323 * devm_iio_backend_request_buffer - Device managed buffer request
324 * @dev: Consumer device for the backend
325 * @back: Backend device
326 * @indio_dev: IIO device
328 * Request an IIO buffer from the backend. The type of the buffer (typically
329 * INDIO_BUFFER_HARDWARE) is up to the backend to decide. This is because,
330 * normally, the backend dictates what kind of buffering we can get.
332 * The backend .free_buffer() hooks is automatically called on @dev detach.
335 * 0 on success, negative error number on failure.
337 int devm_iio_backend_request_buffer(struct device *dev,
338 struct iio_backend *back,
339 struct iio_dev *indio_dev)
341 struct iio_backend_buffer_pair *pair;
342 struct iio_buffer *buffer;
344 pair = devm_kzalloc(dev, sizeof(*pair), GFP_KERNEL);
348 buffer = iio_backend_ptr_op_call(back, request_buffer, indio_dev);
350 return PTR_ERR(buffer);
352 /* weak reference should be all what we need */
354 pair->buffer = buffer;
356 return devm_add_action_or_reset(dev, iio_backend_free_buffer, pair);
358 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, IIO_BACKEND);
360 static struct iio_backend *iio_backend_from_indio_dev_parent(const struct device *dev)
362 struct iio_backend *back = ERR_PTR(-ENODEV), *iter;
365 * We deliberately go through all backends even after finding a match.
366 * The reason is that we want to catch frontend devices which have more
367 * than one backend in which case returning the first we find is bogus.
368 * For those cases, frontends need to explicitly define
369 * get_iio_backend() in struct iio_info.
371 guard(mutex)(&iio_back_lock);
372 list_for_each_entry(iter, &iio_back_list, entry) {
373 if (dev == iter->frontend_dev) {
376 "Multiple backends! get_iio_backend() needs to be implemented");
377 return ERR_PTR(-ENODEV);
388 * iio_backend_ext_info_get - IIO ext_info read callback
389 * @indio_dev: IIO device
390 * @private: Data private to the driver
392 * @buf: Buffer where to place the attribute data
394 * This helper is intended to be used by backends that extend an IIO channel
395 * (through iio_backend_extend_chan_spec()) with extended info. In that case,
396 * backends are not supposed to give their own callbacks (as they would not have
397 * a way to get the backend from indio_dev). This is the getter.
400 * Number of bytes written to buf, negative error number on failure.
402 ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private,
403 const struct iio_chan_spec *chan, char *buf)
405 struct iio_backend *back;
408 * The below should work for the majority of the cases. It will not work
409 * when one frontend has multiple backends in which case we'll need a
410 * new callback in struct iio_info so we can directly request the proper
411 * backend from the frontend. Anyways, let's only introduce new options
412 * when really needed...
414 back = iio_backend_from_indio_dev_parent(indio_dev->dev.parent);
416 return PTR_ERR(back);
418 return iio_backend_op_call(back, ext_info_get, private, chan, buf);
420 EXPORT_SYMBOL_NS_GPL(iio_backend_ext_info_get, IIO_BACKEND);
423 * iio_backend_ext_info_set - IIO ext_info write callback
424 * @indio_dev: IIO device
425 * @private: Data private to the driver
427 * @buf: Buffer holding the sysfs attribute
428 * @len: Buffer length
430 * This helper is intended to be used by backends that extend an IIO channel
431 * (trough iio_backend_extend_chan_spec()) with extended info. In that case,
432 * backends are not supposed to give their own callbacks (as they would not have
433 * a way to get the backend from indio_dev). This is the setter.
436 * Buffer length on success, negative error number on failure.
438 ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private,
439 const struct iio_chan_spec *chan,
440 const char *buf, size_t len)
442 struct iio_backend *back;
444 back = iio_backend_from_indio_dev_parent(indio_dev->dev.parent);
446 return PTR_ERR(back);
448 return iio_backend_op_call(back, ext_info_set, private, chan, buf, len);
450 EXPORT_SYMBOL_NS_GPL(iio_backend_ext_info_set, IIO_BACKEND);
453 * iio_backend_extend_chan_spec - Extend an IIO channel
454 * @indio_dev: IIO device
455 * @back: Backend device
458 * Some backends may have their own functionalities and hence capable of
459 * extending a frontend's channel.
462 * 0 on success, negative error number on failure.
464 int iio_backend_extend_chan_spec(struct iio_dev *indio_dev,
465 struct iio_backend *back,
466 struct iio_chan_spec *chan)
468 const struct iio_chan_spec_ext_info *frontend_ext_info = chan->ext_info;
469 const struct iio_chan_spec_ext_info *back_ext_info;
472 ret = iio_backend_op_call(back, extend_chan_spec, chan);
476 * Let's keep things simple for now. Don't allow to overwrite the
477 * frontend's extended info. If ever needed, we can support appending
480 if (frontend_ext_info && chan->ext_info != frontend_ext_info)
485 /* Don't allow backends to get creative and force their own handlers */
486 for (back_ext_info = chan->ext_info; back_ext_info->name; back_ext_info++) {
487 if (back_ext_info->read != iio_backend_ext_info_get)
489 if (back_ext_info->write != iio_backend_ext_info_set)
495 EXPORT_SYMBOL_NS_GPL(iio_backend_extend_chan_spec, IIO_BACKEND);
497 static void iio_backend_release(void *arg)
499 struct iio_backend *back = arg;
501 module_put(back->owner);
504 static int __devm_iio_backend_get(struct device *dev, struct iio_backend *back)
506 struct device_link *link;
510 * Make sure the provider cannot be unloaded before the consumer module.
511 * Note that device_links would still guarantee that nothing is
512 * accessible (and breaks) but this makes it explicit that the consumer
513 * module must be also unloaded.
515 if (!try_module_get(back->owner))
516 return dev_err_probe(dev, -ENODEV,
517 "Cannot get module reference\n");
519 ret = devm_add_action_or_reset(dev, iio_backend_release, back);
523 link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
525 return dev_err_probe(dev, -EINVAL,
526 "Could not link to supplier(%s)\n",
527 dev_name(back->dev));
529 back->frontend_dev = dev;
531 dev_dbg(dev, "Found backend(%s) device\n", dev_name(back->dev));
537 * devm_iio_backend_get - Device managed backend device get
538 * @dev: Consumer device for the backend
539 * @name: Backend name
541 * Get's the backend associated with @dev.
544 * A backend pointer, negative error pointer otherwise.
546 struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name)
548 struct fwnode_handle *fwnode;
549 struct iio_backend *back;
554 ret = device_property_match_string(dev, "io-backend-names",
563 fwnode = fwnode_find_reference(dev_fwnode(dev), "io-backends", index);
565 return dev_err_cast_probe(dev, fwnode,
566 "Cannot get Firmware reference\n");
568 guard(mutex)(&iio_back_lock);
569 list_for_each_entry(back, &iio_back_list, entry) {
570 if (!device_match_fwnode(back->dev, fwnode))
573 fwnode_handle_put(fwnode);
574 ret = __devm_iio_backend_get(dev, back);
581 fwnode_handle_put(fwnode);
582 return ERR_PTR(-EPROBE_DEFER);
584 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, IIO_BACKEND);
587 * __devm_iio_backend_get_from_fwnode_lookup - Device managed fwnode backend device get
588 * @dev: Consumer device for the backend
589 * @fwnode: Firmware node of the backend device
591 * Search the backend list for a device matching @fwnode.
592 * This API should not be used and it's only present for preventing the first
593 * user of this framework to break it's DT ABI.
596 * A backend pointer, negative error pointer otherwise.
599 __devm_iio_backend_get_from_fwnode_lookup(struct device *dev,
600 struct fwnode_handle *fwnode)
602 struct iio_backend *back;
605 guard(mutex)(&iio_back_lock);
606 list_for_each_entry(back, &iio_back_list, entry) {
607 if (!device_match_fwnode(back->dev, fwnode))
610 ret = __devm_iio_backend_get(dev, back);
617 return ERR_PTR(-EPROBE_DEFER);
619 EXPORT_SYMBOL_NS_GPL(__devm_iio_backend_get_from_fwnode_lookup, IIO_BACKEND);
622 * iio_backend_get_priv - Get driver private data
623 * @back: Backend device
625 void *iio_backend_get_priv(const struct iio_backend *back)
629 EXPORT_SYMBOL_NS_GPL(iio_backend_get_priv, IIO_BACKEND);
631 static void iio_backend_unregister(void *arg)
633 struct iio_backend *back = arg;
635 guard(mutex)(&iio_back_lock);
636 list_del(&back->entry);
640 * devm_iio_backend_register - Device managed backend device register
641 * @dev: Backend device being registered
643 * @priv: Device private data
645 * @ops is mandatory. Not providing it results in -EINVAL.
648 * 0 on success, negative error number on failure.
650 int devm_iio_backend_register(struct device *dev,
651 const struct iio_backend_ops *ops, void *priv)
653 struct iio_backend *back;
656 return dev_err_probe(dev, -EINVAL, "No backend ops given\n");
659 * Through device_links, we guarantee that a frontend device cannot be
660 * bound/exist if the backend driver is not around. Hence, we can bind
661 * the backend object lifetime with the device being passed since
662 * removing it will tear the frontend/consumer down.
664 back = devm_kzalloc(dev, sizeof(*back), GFP_KERNEL);
669 back->owner = dev->driver->owner;
672 scoped_guard(mutex, &iio_back_lock)
673 list_add(&back->entry, &iio_back_list);
675 return devm_add_action_or_reset(dev, iio_backend_unregister, back);
677 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_register, IIO_BACKEND);
680 MODULE_DESCRIPTION("Framework to handle complex IIO aggregate devices");
681 MODULE_LICENSE("GPL");