xref: /openbmc/linux/drivers/iio/industrialio-backend.c (revision b181f7029bd71238ac2754ce7052dffd69432085)
1*5fc4f0c0SNuno Sa // SPDX-License-Identifier: GPL-2.0-only
2*5fc4f0c0SNuno Sa /*
3*5fc4f0c0SNuno Sa  * Framework to handle complex IIO aggregate devices.
4*5fc4f0c0SNuno Sa  *
5*5fc4f0c0SNuno Sa  * The typical architecture is to have one device as the frontend device which
6*5fc4f0c0SNuno Sa  * can be "linked" against one or multiple backend devices. All the IIO and
7*5fc4f0c0SNuno Sa  * userspace interface is expected to be registers/managed by the frontend
8*5fc4f0c0SNuno Sa  * device which will callback into the backends when needed (to get/set some
9*5fc4f0c0SNuno Sa  * configuration that it does not directly control).
10*5fc4f0c0SNuno Sa  *
11*5fc4f0c0SNuno Sa  *                                           -------------------------------------------------------
12*5fc4f0c0SNuno Sa  * ------------------                        | ------------         ------------      -------  FPGA|
13*5fc4f0c0SNuno Sa  * |     ADC        |------------------------| | ADC CORE |---------| DMA CORE |------| RAM |      |
14*5fc4f0c0SNuno Sa  * | (Frontend/IIO) | Serial Data (eg: LVDS) | |(backend) |---------|          |------|     |      |
15*5fc4f0c0SNuno Sa  * |                |------------------------| ------------         ------------      -------      |
16*5fc4f0c0SNuno Sa  * ------------------                        -------------------------------------------------------
17*5fc4f0c0SNuno Sa  *
18*5fc4f0c0SNuno Sa  * The framework interface is pretty simple:
19*5fc4f0c0SNuno Sa  *   - Backends should register themselves with devm_iio_backend_register()
20*5fc4f0c0SNuno Sa  *   - Frontend devices should get backends with devm_iio_backend_get()
21*5fc4f0c0SNuno Sa  *
22*5fc4f0c0SNuno Sa  * Also to note that the primary target for this framework are converters like
23*5fc4f0c0SNuno Sa  * ADC/DACs so iio_backend_ops will have some operations typical of converter
24*5fc4f0c0SNuno Sa  * devices. On top of that, this is "generic" for all IIO which means any kind
25*5fc4f0c0SNuno Sa  * of device can make use of the framework. That said, If the iio_backend_ops
26*5fc4f0c0SNuno Sa  * struct begins to grow out of control, we can always refactor things so that
27*5fc4f0c0SNuno Sa  * the industrialio-backend.c is only left with the really generic stuff. Then,
28*5fc4f0c0SNuno Sa  * we can build on top of it depending on the needs.
29*5fc4f0c0SNuno Sa  *
30*5fc4f0c0SNuno Sa  * Copyright (C) 2023-2024 Analog Devices Inc.
31*5fc4f0c0SNuno Sa  */
32*5fc4f0c0SNuno Sa #define dev_fmt(fmt) "iio-backend: " fmt
33*5fc4f0c0SNuno Sa 
34*5fc4f0c0SNuno Sa #include <linux/cleanup.h>
35*5fc4f0c0SNuno Sa #include <linux/device.h>
36*5fc4f0c0SNuno Sa #include <linux/err.h>
37*5fc4f0c0SNuno Sa #include <linux/errno.h>
38*5fc4f0c0SNuno Sa #include <linux/list.h>
39*5fc4f0c0SNuno Sa #include <linux/module.h>
40*5fc4f0c0SNuno Sa #include <linux/mutex.h>
41*5fc4f0c0SNuno Sa #include <linux/property.h>
42*5fc4f0c0SNuno Sa #include <linux/slab.h>
43*5fc4f0c0SNuno Sa #include <linux/types.h>
44*5fc4f0c0SNuno Sa 
45*5fc4f0c0SNuno Sa #include <linux/iio/backend.h>
46*5fc4f0c0SNuno Sa 
47*5fc4f0c0SNuno Sa struct iio_backend {
48*5fc4f0c0SNuno Sa 	struct list_head entry;
49*5fc4f0c0SNuno Sa 	const struct iio_backend_ops *ops;
50*5fc4f0c0SNuno Sa 	struct device *dev;
51*5fc4f0c0SNuno Sa 	struct module *owner;
52*5fc4f0c0SNuno Sa 	void *priv;
53*5fc4f0c0SNuno Sa };
54*5fc4f0c0SNuno Sa 
55*5fc4f0c0SNuno Sa /*
56*5fc4f0c0SNuno Sa  * Helper struct for requesting buffers. This ensures that we have all data
57*5fc4f0c0SNuno Sa  * that we need to free the buffer in a device managed action.
58*5fc4f0c0SNuno Sa  */
59*5fc4f0c0SNuno Sa struct iio_backend_buffer_pair {
60*5fc4f0c0SNuno Sa 	struct iio_backend *back;
61*5fc4f0c0SNuno Sa 	struct iio_buffer *buffer;
62*5fc4f0c0SNuno Sa };
63*5fc4f0c0SNuno Sa 
64*5fc4f0c0SNuno Sa static LIST_HEAD(iio_back_list);
65*5fc4f0c0SNuno Sa static DEFINE_MUTEX(iio_back_lock);
66*5fc4f0c0SNuno Sa 
67*5fc4f0c0SNuno Sa /*
68*5fc4f0c0SNuno Sa  * Helper macros to call backend ops. Makes sure the option is supported.
69*5fc4f0c0SNuno Sa  */
70*5fc4f0c0SNuno Sa #define iio_backend_check_op(back, op) ({ \
71*5fc4f0c0SNuno Sa 	struct iio_backend *____back = back;				\
72*5fc4f0c0SNuno Sa 	int ____ret = 0;						\
73*5fc4f0c0SNuno Sa 									\
74*5fc4f0c0SNuno Sa 	if (!____back->ops->op)						\
75*5fc4f0c0SNuno Sa 		____ret = -EOPNOTSUPP;					\
76*5fc4f0c0SNuno Sa 									\
77*5fc4f0c0SNuno Sa 	____ret;							\
78*5fc4f0c0SNuno Sa })
79*5fc4f0c0SNuno Sa 
80*5fc4f0c0SNuno Sa #define iio_backend_op_call(back, op, args...) ({		\
81*5fc4f0c0SNuno Sa 	struct iio_backend *__back = back;			\
82*5fc4f0c0SNuno Sa 	int __ret;						\
83*5fc4f0c0SNuno Sa 								\
84*5fc4f0c0SNuno Sa 	__ret = iio_backend_check_op(__back, op);		\
85*5fc4f0c0SNuno Sa 	if (!__ret)						\
86*5fc4f0c0SNuno Sa 		__ret = __back->ops->op(__back, ##args);	\
87*5fc4f0c0SNuno Sa 								\
88*5fc4f0c0SNuno Sa 	__ret;							\
89*5fc4f0c0SNuno Sa })
90*5fc4f0c0SNuno Sa 
91*5fc4f0c0SNuno Sa #define iio_backend_ptr_op_call(back, op, args...) ({		\
92*5fc4f0c0SNuno Sa 	struct iio_backend *__back = back;			\
93*5fc4f0c0SNuno Sa 	void *ptr_err;						\
94*5fc4f0c0SNuno Sa 	int __ret;						\
95*5fc4f0c0SNuno Sa 								\
96*5fc4f0c0SNuno Sa 	__ret = iio_backend_check_op(__back, op);		\
97*5fc4f0c0SNuno Sa 	if (__ret)						\
98*5fc4f0c0SNuno Sa 		ptr_err = ERR_PTR(__ret);			\
99*5fc4f0c0SNuno Sa 	else							\
100*5fc4f0c0SNuno Sa 		ptr_err = __back->ops->op(__back, ##args);	\
101*5fc4f0c0SNuno Sa 								\
102*5fc4f0c0SNuno Sa 	ptr_err;						\
103*5fc4f0c0SNuno Sa })
104*5fc4f0c0SNuno Sa 
105*5fc4f0c0SNuno Sa #define iio_backend_void_op_call(back, op, args...) {		\
106*5fc4f0c0SNuno Sa 	struct iio_backend *__back = back;			\
107*5fc4f0c0SNuno Sa 	int __ret;						\
108*5fc4f0c0SNuno Sa 								\
109*5fc4f0c0SNuno Sa 	__ret = iio_backend_check_op(__back, op);		\
110*5fc4f0c0SNuno Sa 	if (!__ret)						\
111*5fc4f0c0SNuno Sa 		__back->ops->op(__back, ##args);		\
112*5fc4f0c0SNuno Sa }
113*5fc4f0c0SNuno Sa 
114*5fc4f0c0SNuno Sa /**
115*5fc4f0c0SNuno Sa  * iio_backend_chan_enable - Enable a backend channel
116*5fc4f0c0SNuno Sa  * @back:	Backend device
117*5fc4f0c0SNuno Sa  * @chan:	Channel number
118*5fc4f0c0SNuno Sa  *
119*5fc4f0c0SNuno Sa  * RETURNS:
120*5fc4f0c0SNuno Sa  * 0 on success, negative error number on failure.
121*5fc4f0c0SNuno Sa  */
iio_backend_chan_enable(struct iio_backend * back,unsigned int chan)122*5fc4f0c0SNuno Sa int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan)
123*5fc4f0c0SNuno Sa {
124*5fc4f0c0SNuno Sa 	return iio_backend_op_call(back, chan_enable, chan);
125*5fc4f0c0SNuno Sa }
126*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(iio_backend_chan_enable, IIO_BACKEND);
127*5fc4f0c0SNuno Sa 
128*5fc4f0c0SNuno Sa /**
129*5fc4f0c0SNuno Sa  * iio_backend_chan_disable - Disable a backend channel
130*5fc4f0c0SNuno Sa  * @back:	Backend device
131*5fc4f0c0SNuno Sa  * @chan:	Channel number
132*5fc4f0c0SNuno Sa  *
133*5fc4f0c0SNuno Sa  * RETURNS:
134*5fc4f0c0SNuno Sa  * 0 on success, negative error number on failure.
135*5fc4f0c0SNuno Sa  */
iio_backend_chan_disable(struct iio_backend * back,unsigned int chan)136*5fc4f0c0SNuno Sa int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan)
137*5fc4f0c0SNuno Sa {
138*5fc4f0c0SNuno Sa 	return iio_backend_op_call(back, chan_disable, chan);
139*5fc4f0c0SNuno Sa }
140*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(iio_backend_chan_disable, IIO_BACKEND);
141*5fc4f0c0SNuno Sa 
__iio_backend_disable(void * back)142*5fc4f0c0SNuno Sa static void __iio_backend_disable(void *back)
143*5fc4f0c0SNuno Sa {
144*5fc4f0c0SNuno Sa 	iio_backend_void_op_call(back, disable);
145*5fc4f0c0SNuno Sa }
146*5fc4f0c0SNuno Sa 
147*5fc4f0c0SNuno Sa /**
148*5fc4f0c0SNuno Sa  * devm_iio_backend_enable - Device managed backend enable
149*5fc4f0c0SNuno Sa  * @dev:	Consumer device for the backend
150*5fc4f0c0SNuno Sa  * @back:	Backend device
151*5fc4f0c0SNuno Sa  *
152*5fc4f0c0SNuno Sa  * RETURNS:
153*5fc4f0c0SNuno Sa  * 0 on success, negative error number on failure.
154*5fc4f0c0SNuno Sa  */
devm_iio_backend_enable(struct device * dev,struct iio_backend * back)155*5fc4f0c0SNuno Sa int devm_iio_backend_enable(struct device *dev, struct iio_backend *back)
156*5fc4f0c0SNuno Sa {
157*5fc4f0c0SNuno Sa 	int ret;
158*5fc4f0c0SNuno Sa 
159*5fc4f0c0SNuno Sa 	ret = iio_backend_op_call(back, enable);
160*5fc4f0c0SNuno Sa 	if (ret)
161*5fc4f0c0SNuno Sa 		return ret;
162*5fc4f0c0SNuno Sa 
163*5fc4f0c0SNuno Sa 	return devm_add_action_or_reset(dev, __iio_backend_disable, back);
164*5fc4f0c0SNuno Sa }
165*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(devm_iio_backend_enable, IIO_BACKEND);
166*5fc4f0c0SNuno Sa 
167*5fc4f0c0SNuno Sa /**
168*5fc4f0c0SNuno Sa  * iio_backend_data_format_set - Configure the channel data format
169*5fc4f0c0SNuno Sa  * @back:	Backend device
170*5fc4f0c0SNuno Sa  * @chan:	Channel number
171*5fc4f0c0SNuno Sa  * @data:	Data format
172*5fc4f0c0SNuno Sa  *
173*5fc4f0c0SNuno Sa  * Properly configure a channel with respect to the expected data format. A
174*5fc4f0c0SNuno Sa  * @struct iio_backend_data_fmt must be passed with the settings.
175*5fc4f0c0SNuno Sa  *
176*5fc4f0c0SNuno Sa  * RETURNS:
177*5fc4f0c0SNuno Sa  * 0 on success, negative error number on failure.
178*5fc4f0c0SNuno Sa  */
iio_backend_data_format_set(struct iio_backend * back,unsigned int chan,const struct iio_backend_data_fmt * data)179*5fc4f0c0SNuno Sa int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan,
180*5fc4f0c0SNuno Sa 				const struct iio_backend_data_fmt *data)
181*5fc4f0c0SNuno Sa {
182*5fc4f0c0SNuno Sa 	if (!data || data->type >= IIO_BACKEND_DATA_TYPE_MAX)
183*5fc4f0c0SNuno Sa 		return -EINVAL;
184*5fc4f0c0SNuno Sa 
185*5fc4f0c0SNuno Sa 	return iio_backend_op_call(back, data_format_set, chan, data);
186*5fc4f0c0SNuno Sa }
187*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(iio_backend_data_format_set, IIO_BACKEND);
188*5fc4f0c0SNuno Sa 
iio_backend_free_buffer(void * arg)189*5fc4f0c0SNuno Sa static void iio_backend_free_buffer(void *arg)
190*5fc4f0c0SNuno Sa {
191*5fc4f0c0SNuno Sa 	struct iio_backend_buffer_pair *pair = arg;
192*5fc4f0c0SNuno Sa 
193*5fc4f0c0SNuno Sa 	iio_backend_void_op_call(pair->back, free_buffer, pair->buffer);
194*5fc4f0c0SNuno Sa }
195*5fc4f0c0SNuno Sa 
196*5fc4f0c0SNuno Sa /**
197*5fc4f0c0SNuno Sa  * devm_iio_backend_request_buffer - Device managed buffer request
198*5fc4f0c0SNuno Sa  * @dev:	Consumer device for the backend
199*5fc4f0c0SNuno Sa  * @back:	Backend device
200*5fc4f0c0SNuno Sa  * @indio_dev:	IIO device
201*5fc4f0c0SNuno Sa  *
202*5fc4f0c0SNuno Sa  * Request an IIO buffer from the backend. The type of the buffer (typically
203*5fc4f0c0SNuno Sa  * INDIO_BUFFER_HARDWARE) is up to the backend to decide. This is because,
204*5fc4f0c0SNuno Sa  * normally, the backend dictates what kind of buffering we can get.
205*5fc4f0c0SNuno Sa  *
206*5fc4f0c0SNuno Sa  * The backend .free_buffer() hooks is automatically called on @dev detach.
207*5fc4f0c0SNuno Sa  *
208*5fc4f0c0SNuno Sa  * RETURNS:
209*5fc4f0c0SNuno Sa  * 0 on success, negative error number on failure.
210*5fc4f0c0SNuno Sa  */
devm_iio_backend_request_buffer(struct device * dev,struct iio_backend * back,struct iio_dev * indio_dev)211*5fc4f0c0SNuno Sa int devm_iio_backend_request_buffer(struct device *dev,
212*5fc4f0c0SNuno Sa 				    struct iio_backend *back,
213*5fc4f0c0SNuno Sa 				    struct iio_dev *indio_dev)
214*5fc4f0c0SNuno Sa {
215*5fc4f0c0SNuno Sa 	struct iio_backend_buffer_pair *pair;
216*5fc4f0c0SNuno Sa 	struct iio_buffer *buffer;
217*5fc4f0c0SNuno Sa 
218*5fc4f0c0SNuno Sa 	pair = devm_kzalloc(dev, sizeof(*pair), GFP_KERNEL);
219*5fc4f0c0SNuno Sa 	if (!pair)
220*5fc4f0c0SNuno Sa 		return -ENOMEM;
221*5fc4f0c0SNuno Sa 
222*5fc4f0c0SNuno Sa 	buffer = iio_backend_ptr_op_call(back, request_buffer, indio_dev);
223*5fc4f0c0SNuno Sa 	if (IS_ERR(buffer))
224*5fc4f0c0SNuno Sa 		return PTR_ERR(buffer);
225*5fc4f0c0SNuno Sa 
226*5fc4f0c0SNuno Sa 	/* weak reference should be all what we need */
227*5fc4f0c0SNuno Sa 	pair->back = back;
228*5fc4f0c0SNuno Sa 	pair->buffer = buffer;
229*5fc4f0c0SNuno Sa 
230*5fc4f0c0SNuno Sa 	return devm_add_action_or_reset(dev, iio_backend_free_buffer, pair);
231*5fc4f0c0SNuno Sa }
232*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, IIO_BACKEND);
233*5fc4f0c0SNuno Sa 
iio_backend_release(void * arg)234*5fc4f0c0SNuno Sa static void iio_backend_release(void *arg)
235*5fc4f0c0SNuno Sa {
236*5fc4f0c0SNuno Sa 	struct iio_backend *back = arg;
237*5fc4f0c0SNuno Sa 
238*5fc4f0c0SNuno Sa 	module_put(back->owner);
239*5fc4f0c0SNuno Sa }
240*5fc4f0c0SNuno Sa 
__devm_iio_backend_get(struct device * dev,struct iio_backend * back)241*5fc4f0c0SNuno Sa static int __devm_iio_backend_get(struct device *dev, struct iio_backend *back)
242*5fc4f0c0SNuno Sa {
243*5fc4f0c0SNuno Sa 	struct device_link *link;
244*5fc4f0c0SNuno Sa 	int ret;
245*5fc4f0c0SNuno Sa 
246*5fc4f0c0SNuno Sa 	/*
247*5fc4f0c0SNuno Sa 	 * Make sure the provider cannot be unloaded before the consumer module.
248*5fc4f0c0SNuno Sa 	 * Note that device_links would still guarantee that nothing is
249*5fc4f0c0SNuno Sa 	 * accessible (and breaks) but this makes it explicit that the consumer
250*5fc4f0c0SNuno Sa 	 * module must be also unloaded.
251*5fc4f0c0SNuno Sa 	 */
252*5fc4f0c0SNuno Sa 	if (!try_module_get(back->owner))
253*5fc4f0c0SNuno Sa 		return dev_err_probe(dev, -ENODEV,
254*5fc4f0c0SNuno Sa 				     "Cannot get module reference\n");
255*5fc4f0c0SNuno Sa 
256*5fc4f0c0SNuno Sa 	ret = devm_add_action_or_reset(dev, iio_backend_release, back);
257*5fc4f0c0SNuno Sa 	if (ret)
258*5fc4f0c0SNuno Sa 		return ret;
259*5fc4f0c0SNuno Sa 
260*5fc4f0c0SNuno Sa 	link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
261*5fc4f0c0SNuno Sa 	if (!link)
262*5fc4f0c0SNuno Sa 		return dev_err_probe(dev, -EINVAL,
263*5fc4f0c0SNuno Sa 				     "Could not link to supplier(%s)\n",
264*5fc4f0c0SNuno Sa 				     dev_name(back->dev));
265*5fc4f0c0SNuno Sa 
266*5fc4f0c0SNuno Sa 	dev_dbg(dev, "Found backend(%s) device\n", dev_name(back->dev));
267*5fc4f0c0SNuno Sa 
268*5fc4f0c0SNuno Sa 	return 0;
269*5fc4f0c0SNuno Sa }
270*5fc4f0c0SNuno Sa 
271*5fc4f0c0SNuno Sa /**
272*5fc4f0c0SNuno Sa  * devm_iio_backend_get - Device managed backend device get
273*5fc4f0c0SNuno Sa  * @dev:	Consumer device for the backend
274*5fc4f0c0SNuno Sa  * @name:	Backend name
275*5fc4f0c0SNuno Sa  *
276*5fc4f0c0SNuno Sa  * Get's the backend associated with @dev.
277*5fc4f0c0SNuno Sa  *
278*5fc4f0c0SNuno Sa  * RETURNS:
279*5fc4f0c0SNuno Sa  * A backend pointer, negative error pointer otherwise.
280*5fc4f0c0SNuno Sa  */
devm_iio_backend_get(struct device * dev,const char * name)281*5fc4f0c0SNuno Sa struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name)
282*5fc4f0c0SNuno Sa {
283*5fc4f0c0SNuno Sa 	struct fwnode_handle *fwnode;
284*5fc4f0c0SNuno Sa 	struct iio_backend *back;
285*5fc4f0c0SNuno Sa 	unsigned int index;
286*5fc4f0c0SNuno Sa 	int ret;
287*5fc4f0c0SNuno Sa 
288*5fc4f0c0SNuno Sa 	if (name) {
289*5fc4f0c0SNuno Sa 		ret = device_property_match_string(dev, "io-backend-names",
290*5fc4f0c0SNuno Sa 						   name);
291*5fc4f0c0SNuno Sa 		if (ret < 0)
292*5fc4f0c0SNuno Sa 			return ERR_PTR(ret);
293*5fc4f0c0SNuno Sa 		index = ret;
294*5fc4f0c0SNuno Sa 	} else {
295*5fc4f0c0SNuno Sa 		index = 0;
296*5fc4f0c0SNuno Sa 	}
297*5fc4f0c0SNuno Sa 
298*5fc4f0c0SNuno Sa 	fwnode = fwnode_find_reference(dev_fwnode(dev), "io-backends", index);
299*5fc4f0c0SNuno Sa 	if (IS_ERR(fwnode)) {
300*5fc4f0c0SNuno Sa 		dev_err_probe(dev, PTR_ERR(fwnode),
301*5fc4f0c0SNuno Sa 			      "Cannot get Firmware reference\n");
302*5fc4f0c0SNuno Sa 		return ERR_CAST(fwnode);
303*5fc4f0c0SNuno Sa 	}
304*5fc4f0c0SNuno Sa 
305*5fc4f0c0SNuno Sa 	guard(mutex)(&iio_back_lock);
306*5fc4f0c0SNuno Sa 	list_for_each_entry(back, &iio_back_list, entry) {
307*5fc4f0c0SNuno Sa 		if (!device_match_fwnode(back->dev, fwnode))
308*5fc4f0c0SNuno Sa 			continue;
309*5fc4f0c0SNuno Sa 
310*5fc4f0c0SNuno Sa 		fwnode_handle_put(fwnode);
311*5fc4f0c0SNuno Sa 		ret = __devm_iio_backend_get(dev, back);
312*5fc4f0c0SNuno Sa 		if (ret)
313*5fc4f0c0SNuno Sa 			return ERR_PTR(ret);
314*5fc4f0c0SNuno Sa 
315*5fc4f0c0SNuno Sa 		return back;
316*5fc4f0c0SNuno Sa 	}
317*5fc4f0c0SNuno Sa 
318*5fc4f0c0SNuno Sa 	fwnode_handle_put(fwnode);
319*5fc4f0c0SNuno Sa 	return ERR_PTR(-EPROBE_DEFER);
320*5fc4f0c0SNuno Sa }
321*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, IIO_BACKEND);
322*5fc4f0c0SNuno Sa 
323*5fc4f0c0SNuno Sa /**
324*5fc4f0c0SNuno Sa  * __devm_iio_backend_get_from_fwnode_lookup - Device managed fwnode backend device get
325*5fc4f0c0SNuno Sa  * @dev:	Consumer device for the backend
326*5fc4f0c0SNuno Sa  * @fwnode:	Firmware node of the backend device
327*5fc4f0c0SNuno Sa  *
328*5fc4f0c0SNuno Sa  * Search the backend list for a device matching @fwnode.
329*5fc4f0c0SNuno Sa  * This API should not be used and it's only present for preventing the first
330*5fc4f0c0SNuno Sa  * user of this framework to break it's DT ABI.
331*5fc4f0c0SNuno Sa  *
332*5fc4f0c0SNuno Sa  * RETURNS:
333*5fc4f0c0SNuno Sa  * A backend pointer, negative error pointer otherwise.
334*5fc4f0c0SNuno Sa  */
335*5fc4f0c0SNuno Sa struct iio_backend *
__devm_iio_backend_get_from_fwnode_lookup(struct device * dev,struct fwnode_handle * fwnode)336*5fc4f0c0SNuno Sa __devm_iio_backend_get_from_fwnode_lookup(struct device *dev,
337*5fc4f0c0SNuno Sa 					  struct fwnode_handle *fwnode)
338*5fc4f0c0SNuno Sa {
339*5fc4f0c0SNuno Sa 	struct iio_backend *back;
340*5fc4f0c0SNuno Sa 	int ret;
341*5fc4f0c0SNuno Sa 
342*5fc4f0c0SNuno Sa 	guard(mutex)(&iio_back_lock);
343*5fc4f0c0SNuno Sa 	list_for_each_entry(back, &iio_back_list, entry) {
344*5fc4f0c0SNuno Sa 		if (!device_match_fwnode(back->dev, fwnode))
345*5fc4f0c0SNuno Sa 			continue;
346*5fc4f0c0SNuno Sa 
347*5fc4f0c0SNuno Sa 		ret = __devm_iio_backend_get(dev, back);
348*5fc4f0c0SNuno Sa 		if (ret)
349*5fc4f0c0SNuno Sa 			return ERR_PTR(ret);
350*5fc4f0c0SNuno Sa 
351*5fc4f0c0SNuno Sa 		return back;
352*5fc4f0c0SNuno Sa 	}
353*5fc4f0c0SNuno Sa 
354*5fc4f0c0SNuno Sa 	return ERR_PTR(-EPROBE_DEFER);
355*5fc4f0c0SNuno Sa }
356*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(__devm_iio_backend_get_from_fwnode_lookup, IIO_BACKEND);
357*5fc4f0c0SNuno Sa 
358*5fc4f0c0SNuno Sa /**
359*5fc4f0c0SNuno Sa  * iio_backend_get_priv - Get driver private data
360*5fc4f0c0SNuno Sa  * @back:	Backend device
361*5fc4f0c0SNuno Sa  */
iio_backend_get_priv(const struct iio_backend * back)362*5fc4f0c0SNuno Sa void *iio_backend_get_priv(const struct iio_backend *back)
363*5fc4f0c0SNuno Sa {
364*5fc4f0c0SNuno Sa 	return back->priv;
365*5fc4f0c0SNuno Sa }
366*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(iio_backend_get_priv, IIO_BACKEND);
367*5fc4f0c0SNuno Sa 
iio_backend_unregister(void * arg)368*5fc4f0c0SNuno Sa static void iio_backend_unregister(void *arg)
369*5fc4f0c0SNuno Sa {
370*5fc4f0c0SNuno Sa 	struct iio_backend *back = arg;
371*5fc4f0c0SNuno Sa 
372*5fc4f0c0SNuno Sa 	guard(mutex)(&iio_back_lock);
373*5fc4f0c0SNuno Sa 	list_del(&back->entry);
374*5fc4f0c0SNuno Sa }
375*5fc4f0c0SNuno Sa 
376*5fc4f0c0SNuno Sa /**
377*5fc4f0c0SNuno Sa  * devm_iio_backend_register - Device managed backend device register
378*5fc4f0c0SNuno Sa  * @dev:	Backend device being registered
379*5fc4f0c0SNuno Sa  * @ops:	Backend ops
380*5fc4f0c0SNuno Sa  * @priv:	Device private data
381*5fc4f0c0SNuno Sa  *
382*5fc4f0c0SNuno Sa  * @ops is mandatory. Not providing it results in -EINVAL.
383*5fc4f0c0SNuno Sa  *
384*5fc4f0c0SNuno Sa  * RETURNS:
385*5fc4f0c0SNuno Sa  * 0 on success, negative error number on failure.
386*5fc4f0c0SNuno Sa  */
devm_iio_backend_register(struct device * dev,const struct iio_backend_ops * ops,void * priv)387*5fc4f0c0SNuno Sa int devm_iio_backend_register(struct device *dev,
388*5fc4f0c0SNuno Sa 			      const struct iio_backend_ops *ops, void *priv)
389*5fc4f0c0SNuno Sa {
390*5fc4f0c0SNuno Sa 	struct iio_backend *back;
391*5fc4f0c0SNuno Sa 
392*5fc4f0c0SNuno Sa 	if (!ops)
393*5fc4f0c0SNuno Sa 		return dev_err_probe(dev, -EINVAL, "No backend ops given\n");
394*5fc4f0c0SNuno Sa 
395*5fc4f0c0SNuno Sa 	/*
396*5fc4f0c0SNuno Sa 	 * Through device_links, we guarantee that a frontend device cannot be
397*5fc4f0c0SNuno Sa 	 * bound/exist if the backend driver is not around. Hence, we can bind
398*5fc4f0c0SNuno Sa 	 * the backend object lifetime with the device being passed since
399*5fc4f0c0SNuno Sa 	 * removing it will tear the frontend/consumer down.
400*5fc4f0c0SNuno Sa 	 */
401*5fc4f0c0SNuno Sa 	back = devm_kzalloc(dev, sizeof(*back), GFP_KERNEL);
402*5fc4f0c0SNuno Sa 	if (!back)
403*5fc4f0c0SNuno Sa 		return -ENOMEM;
404*5fc4f0c0SNuno Sa 
405*5fc4f0c0SNuno Sa 	back->ops = ops;
406*5fc4f0c0SNuno Sa 	back->owner = dev->driver->owner;
407*5fc4f0c0SNuno Sa 	back->dev = dev;
408*5fc4f0c0SNuno Sa 	back->priv = priv;
409*5fc4f0c0SNuno Sa 	scoped_guard(mutex, &iio_back_lock)
410*5fc4f0c0SNuno Sa 		list_add(&back->entry, &iio_back_list);
411*5fc4f0c0SNuno Sa 
412*5fc4f0c0SNuno Sa 	return devm_add_action_or_reset(dev, iio_backend_unregister, back);
413*5fc4f0c0SNuno Sa }
414*5fc4f0c0SNuno Sa EXPORT_SYMBOL_NS_GPL(devm_iio_backend_register, IIO_BACKEND);
415*5fc4f0c0SNuno Sa 
416*5fc4f0c0SNuno Sa MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
417*5fc4f0c0SNuno Sa MODULE_DESCRIPTION("Framework to handle complex IIO aggregate devices");
418*5fc4f0c0SNuno Sa MODULE_LICENSE("GPL");
419