xref: /OK3568_Linux_fs/kernel/drivers/of/property.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * drivers/of/property.c - Procedures for accessing and interpreting
4*4882a593Smuzhiyun  *			   Devicetree properties and graphs.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Initially created by copying procedures from drivers/of/base.c. This
7*4882a593Smuzhiyun  * file contains the OF property as well as the OF graph interface
8*4882a593Smuzhiyun  * functions.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Paul Mackerras	August 1996.
11*4882a593Smuzhiyun  * Copyright (C) 1996-2005 Paul Mackerras.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
14*4882a593Smuzhiyun  *    {engebret|bergner}@us.ibm.com
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
19*4882a593Smuzhiyun  *  Grant Likely.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define pr_fmt(fmt)	"OF: " fmt
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include <linux/of.h>
25*4882a593Smuzhiyun #include <linux/of_device.h>
26*4882a593Smuzhiyun #include <linux/of_graph.h>
27*4882a593Smuzhiyun #include <linux/of_irq.h>
28*4882a593Smuzhiyun #include <linux/string.h>
29*4882a593Smuzhiyun #include <linux/moduleparam.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include "of_private.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun  * of_graph_is_present() - check graph's presence
35*4882a593Smuzhiyun  * @node: pointer to device_node containing graph port
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * Return: True if @node has a port or ports (with a port) sub-node,
38*4882a593Smuzhiyun  * false otherwise.
39*4882a593Smuzhiyun  */
of_graph_is_present(const struct device_node * node)40*4882a593Smuzhiyun bool of_graph_is_present(const struct device_node *node)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	struct device_node *ports, *port;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	ports = of_get_child_by_name(node, "ports");
45*4882a593Smuzhiyun 	if (ports)
46*4882a593Smuzhiyun 		node = ports;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	port = of_get_child_by_name(node, "port");
49*4882a593Smuzhiyun 	of_node_put(ports);
50*4882a593Smuzhiyun 	of_node_put(port);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	return !!port;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_is_present);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /**
57*4882a593Smuzhiyun  * of_property_count_elems_of_size - Count the number of elements in a property
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
60*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
61*4882a593Smuzhiyun  * @elem_size:	size of the individual element
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * Search for a property in a device node and count the number of elements of
64*4882a593Smuzhiyun  * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
65*4882a593Smuzhiyun  * property does not exist or its length does not match a multiple of elem_size
66*4882a593Smuzhiyun  * and -ENODATA if the property does not have a value.
67*4882a593Smuzhiyun  */
of_property_count_elems_of_size(const struct device_node * np,const char * propname,int elem_size)68*4882a593Smuzhiyun int of_property_count_elems_of_size(const struct device_node *np,
69*4882a593Smuzhiyun 				const char *propname, int elem_size)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	struct property *prop = of_find_property(np, propname, NULL);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	if (!prop)
74*4882a593Smuzhiyun 		return -EINVAL;
75*4882a593Smuzhiyun 	if (!prop->value)
76*4882a593Smuzhiyun 		return -ENODATA;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	if (prop->length % elem_size != 0) {
79*4882a593Smuzhiyun 		pr_err("size of %s in node %pOF is not a multiple of %d\n",
80*4882a593Smuzhiyun 		       propname, np, elem_size);
81*4882a593Smuzhiyun 		return -EINVAL;
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	return prop->length / elem_size;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /**
89*4882a593Smuzhiyun  * of_find_property_value_of_size
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
92*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
93*4882a593Smuzhiyun  * @min:	minimum allowed length of property value
94*4882a593Smuzhiyun  * @max:	maximum allowed length of property value (0 means unlimited)
95*4882a593Smuzhiyun  * @len:	if !=NULL, actual length is written to here
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  * Search for a property in a device node and valid the requested size.
98*4882a593Smuzhiyun  * Returns the property value on success, -EINVAL if the property does not
99*4882a593Smuzhiyun  *  exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
100*4882a593Smuzhiyun  * property data is too small or too large.
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  */
of_find_property_value_of_size(const struct device_node * np,const char * propname,u32 min,u32 max,size_t * len)103*4882a593Smuzhiyun static void *of_find_property_value_of_size(const struct device_node *np,
104*4882a593Smuzhiyun 			const char *propname, u32 min, u32 max, size_t *len)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	struct property *prop = of_find_property(np, propname, NULL);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	if (!prop)
109*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
110*4882a593Smuzhiyun 	if (!prop->value)
111*4882a593Smuzhiyun 		return ERR_PTR(-ENODATA);
112*4882a593Smuzhiyun 	if (prop->length < min)
113*4882a593Smuzhiyun 		return ERR_PTR(-EOVERFLOW);
114*4882a593Smuzhiyun 	if (max && prop->length > max)
115*4882a593Smuzhiyun 		return ERR_PTR(-EOVERFLOW);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	if (len)
118*4882a593Smuzhiyun 		*len = prop->length;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return prop->value;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
127*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
128*4882a593Smuzhiyun  * @index:	index of the u32 in the list of values
129*4882a593Smuzhiyun  * @out_value:	pointer to return value, modified only if no error.
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * Search for a property in a device node and read nth 32-bit value from
132*4882a593Smuzhiyun  * it. Returns 0 on success, -EINVAL if the property does not exist,
133*4882a593Smuzhiyun  * -ENODATA if property does not have a value, and -EOVERFLOW if the
134*4882a593Smuzhiyun  * property data isn't large enough.
135*4882a593Smuzhiyun  *
136*4882a593Smuzhiyun  * The out_value is modified only if a valid u32 value can be decoded.
137*4882a593Smuzhiyun  */
of_property_read_u32_index(const struct device_node * np,const char * propname,u32 index,u32 * out_value)138*4882a593Smuzhiyun int of_property_read_u32_index(const struct device_node *np,
139*4882a593Smuzhiyun 				       const char *propname,
140*4882a593Smuzhiyun 				       u32 index, u32 *out_value)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	const u32 *val = of_find_property_value_of_size(np, propname,
143*4882a593Smuzhiyun 					((index + 1) * sizeof(*out_value)),
144*4882a593Smuzhiyun 					0,
145*4882a593Smuzhiyun 					NULL);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	if (IS_ERR(val))
148*4882a593Smuzhiyun 		return PTR_ERR(val);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	*out_value = be32_to_cpup(((__be32 *)val) + index);
151*4882a593Smuzhiyun 	return 0;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_u32_index);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /**
156*4882a593Smuzhiyun  * of_property_read_u64_index - Find and read a u64 from a multi-value property.
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
159*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
160*4882a593Smuzhiyun  * @index:	index of the u64 in the list of values
161*4882a593Smuzhiyun  * @out_value:	pointer to return value, modified only if no error.
162*4882a593Smuzhiyun  *
163*4882a593Smuzhiyun  * Search for a property in a device node and read nth 64-bit value from
164*4882a593Smuzhiyun  * it. Returns 0 on success, -EINVAL if the property does not exist,
165*4882a593Smuzhiyun  * -ENODATA if property does not have a value, and -EOVERFLOW if the
166*4882a593Smuzhiyun  * property data isn't large enough.
167*4882a593Smuzhiyun  *
168*4882a593Smuzhiyun  * The out_value is modified only if a valid u64 value can be decoded.
169*4882a593Smuzhiyun  */
of_property_read_u64_index(const struct device_node * np,const char * propname,u32 index,u64 * out_value)170*4882a593Smuzhiyun int of_property_read_u64_index(const struct device_node *np,
171*4882a593Smuzhiyun 				       const char *propname,
172*4882a593Smuzhiyun 				       u32 index, u64 *out_value)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	const u64 *val = of_find_property_value_of_size(np, propname,
175*4882a593Smuzhiyun 					((index + 1) * sizeof(*out_value)),
176*4882a593Smuzhiyun 					0, NULL);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	if (IS_ERR(val))
179*4882a593Smuzhiyun 		return PTR_ERR(val);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	*out_value = be64_to_cpup(((__be64 *)val) + index);
182*4882a593Smuzhiyun 	return 0;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_u64_index);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /**
187*4882a593Smuzhiyun  * of_property_read_variable_u8_array - Find and read an array of u8 from a
188*4882a593Smuzhiyun  * property, with bounds on the minimum and maximum array size.
189*4882a593Smuzhiyun  *
190*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
191*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
192*4882a593Smuzhiyun  * @out_values:	pointer to found values.
193*4882a593Smuzhiyun  * @sz_min:	minimum number of array elements to read
194*4882a593Smuzhiyun  * @sz_max:	maximum number of array elements to read, if zero there is no
195*4882a593Smuzhiyun  *		upper limit on the number of elements in the dts entry but only
196*4882a593Smuzhiyun  *		sz_min will be read.
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * Search for a property in a device node and read 8-bit value(s) from
199*4882a593Smuzhiyun  * it. Returns number of elements read on success, -EINVAL if the property
200*4882a593Smuzhiyun  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
201*4882a593Smuzhiyun  * if the property data is smaller than sz_min or longer than sz_max.
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * dts entry of array should be like:
204*4882a593Smuzhiyun  *	property = /bits/ 8 <0x50 0x60 0x70>;
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * The out_values is modified only if a valid u8 value can be decoded.
207*4882a593Smuzhiyun  */
of_property_read_variable_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz_min,size_t sz_max)208*4882a593Smuzhiyun int of_property_read_variable_u8_array(const struct device_node *np,
209*4882a593Smuzhiyun 					const char *propname, u8 *out_values,
210*4882a593Smuzhiyun 					size_t sz_min, size_t sz_max)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	size_t sz, count;
213*4882a593Smuzhiyun 	const u8 *val = of_find_property_value_of_size(np, propname,
214*4882a593Smuzhiyun 						(sz_min * sizeof(*out_values)),
215*4882a593Smuzhiyun 						(sz_max * sizeof(*out_values)),
216*4882a593Smuzhiyun 						&sz);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	if (IS_ERR(val))
219*4882a593Smuzhiyun 		return PTR_ERR(val);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (!sz_max)
222*4882a593Smuzhiyun 		sz = sz_min;
223*4882a593Smuzhiyun 	else
224*4882a593Smuzhiyun 		sz /= sizeof(*out_values);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	count = sz;
227*4882a593Smuzhiyun 	while (count--)
228*4882a593Smuzhiyun 		*out_values++ = *val++;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	return sz;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun  * of_property_read_variable_u16_array - Find and read an array of u16 from a
236*4882a593Smuzhiyun  * property, with bounds on the minimum and maximum array size.
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
239*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
240*4882a593Smuzhiyun  * @out_values:	pointer to found values.
241*4882a593Smuzhiyun  * @sz_min:	minimum number of array elements to read
242*4882a593Smuzhiyun  * @sz_max:	maximum number of array elements to read, if zero there is no
243*4882a593Smuzhiyun  *		upper limit on the number of elements in the dts entry but only
244*4882a593Smuzhiyun  *		sz_min will be read.
245*4882a593Smuzhiyun  *
246*4882a593Smuzhiyun  * Search for a property in a device node and read 16-bit value(s) from
247*4882a593Smuzhiyun  * it. Returns number of elements read on success, -EINVAL if the property
248*4882a593Smuzhiyun  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
249*4882a593Smuzhiyun  * if the property data is smaller than sz_min or longer than sz_max.
250*4882a593Smuzhiyun  *
251*4882a593Smuzhiyun  * dts entry of array should be like:
252*4882a593Smuzhiyun  *	property = /bits/ 16 <0x5000 0x6000 0x7000>;
253*4882a593Smuzhiyun  *
254*4882a593Smuzhiyun  * The out_values is modified only if a valid u16 value can be decoded.
255*4882a593Smuzhiyun  */
of_property_read_variable_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz_min,size_t sz_max)256*4882a593Smuzhiyun int of_property_read_variable_u16_array(const struct device_node *np,
257*4882a593Smuzhiyun 					const char *propname, u16 *out_values,
258*4882a593Smuzhiyun 					size_t sz_min, size_t sz_max)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	size_t sz, count;
261*4882a593Smuzhiyun 	const __be16 *val = of_find_property_value_of_size(np, propname,
262*4882a593Smuzhiyun 						(sz_min * sizeof(*out_values)),
263*4882a593Smuzhiyun 						(sz_max * sizeof(*out_values)),
264*4882a593Smuzhiyun 						&sz);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if (IS_ERR(val))
267*4882a593Smuzhiyun 		return PTR_ERR(val);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if (!sz_max)
270*4882a593Smuzhiyun 		sz = sz_min;
271*4882a593Smuzhiyun 	else
272*4882a593Smuzhiyun 		sz /= sizeof(*out_values);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	count = sz;
275*4882a593Smuzhiyun 	while (count--)
276*4882a593Smuzhiyun 		*out_values++ = be16_to_cpup(val++);
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	return sz;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun /**
283*4882a593Smuzhiyun  * of_property_read_variable_u32_array - Find and read an array of 32 bit
284*4882a593Smuzhiyun  * integers from a property, with bounds on the minimum and maximum array size.
285*4882a593Smuzhiyun  *
286*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
287*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
288*4882a593Smuzhiyun  * @out_values:	pointer to return found values.
289*4882a593Smuzhiyun  * @sz_min:	minimum number of array elements to read
290*4882a593Smuzhiyun  * @sz_max:	maximum number of array elements to read, if zero there is no
291*4882a593Smuzhiyun  *		upper limit on the number of elements in the dts entry but only
292*4882a593Smuzhiyun  *		sz_min will be read.
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  * Search for a property in a device node and read 32-bit value(s) from
295*4882a593Smuzhiyun  * it. Returns number of elements read on success, -EINVAL if the property
296*4882a593Smuzhiyun  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
297*4882a593Smuzhiyun  * if the property data is smaller than sz_min or longer than sz_max.
298*4882a593Smuzhiyun  *
299*4882a593Smuzhiyun  * The out_values is modified only if a valid u32 value can be decoded.
300*4882a593Smuzhiyun  */
of_property_read_variable_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz_min,size_t sz_max)301*4882a593Smuzhiyun int of_property_read_variable_u32_array(const struct device_node *np,
302*4882a593Smuzhiyun 			       const char *propname, u32 *out_values,
303*4882a593Smuzhiyun 			       size_t sz_min, size_t sz_max)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	size_t sz, count;
306*4882a593Smuzhiyun 	const __be32 *val = of_find_property_value_of_size(np, propname,
307*4882a593Smuzhiyun 						(sz_min * sizeof(*out_values)),
308*4882a593Smuzhiyun 						(sz_max * sizeof(*out_values)),
309*4882a593Smuzhiyun 						&sz);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (IS_ERR(val))
312*4882a593Smuzhiyun 		return PTR_ERR(val);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	if (!sz_max)
315*4882a593Smuzhiyun 		sz = sz_min;
316*4882a593Smuzhiyun 	else
317*4882a593Smuzhiyun 		sz /= sizeof(*out_values);
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	count = sz;
320*4882a593Smuzhiyun 	while (count--)
321*4882a593Smuzhiyun 		*out_values++ = be32_to_cpup(val++);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	return sz;
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun /**
328*4882a593Smuzhiyun  * of_property_read_u64 - Find and read a 64 bit integer from a property
329*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
330*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
331*4882a593Smuzhiyun  * @out_value:	pointer to return value, modified only if return value is 0.
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  * Search for a property in a device node and read a 64-bit value from
334*4882a593Smuzhiyun  * it. Returns 0 on success, -EINVAL if the property does not exist,
335*4882a593Smuzhiyun  * -ENODATA if property does not have a value, and -EOVERFLOW if the
336*4882a593Smuzhiyun  * property data isn't large enough.
337*4882a593Smuzhiyun  *
338*4882a593Smuzhiyun  * The out_value is modified only if a valid u64 value can be decoded.
339*4882a593Smuzhiyun  */
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)340*4882a593Smuzhiyun int of_property_read_u64(const struct device_node *np, const char *propname,
341*4882a593Smuzhiyun 			 u64 *out_value)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun 	const __be32 *val = of_find_property_value_of_size(np, propname,
344*4882a593Smuzhiyun 						sizeof(*out_value),
345*4882a593Smuzhiyun 						0,
346*4882a593Smuzhiyun 						NULL);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	if (IS_ERR(val))
349*4882a593Smuzhiyun 		return PTR_ERR(val);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	*out_value = of_read_number(val, 2);
352*4882a593Smuzhiyun 	return 0;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_u64);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun /**
357*4882a593Smuzhiyun  * of_property_read_variable_u64_array - Find and read an array of 64 bit
358*4882a593Smuzhiyun  * integers from a property, with bounds on the minimum and maximum array size.
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
361*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
362*4882a593Smuzhiyun  * @out_values:	pointer to found values.
363*4882a593Smuzhiyun  * @sz_min:	minimum number of array elements to read
364*4882a593Smuzhiyun  * @sz_max:	maximum number of array elements to read, if zero there is no
365*4882a593Smuzhiyun  *		upper limit on the number of elements in the dts entry but only
366*4882a593Smuzhiyun  *		sz_min will be read.
367*4882a593Smuzhiyun  *
368*4882a593Smuzhiyun  * Search for a property in a device node and read 64-bit value(s) from
369*4882a593Smuzhiyun  * it. Returns number of elements read on success, -EINVAL if the property
370*4882a593Smuzhiyun  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
371*4882a593Smuzhiyun  * if the property data is smaller than sz_min or longer than sz_max.
372*4882a593Smuzhiyun  *
373*4882a593Smuzhiyun  * The out_values is modified only if a valid u64 value can be decoded.
374*4882a593Smuzhiyun  */
of_property_read_variable_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz_min,size_t sz_max)375*4882a593Smuzhiyun int of_property_read_variable_u64_array(const struct device_node *np,
376*4882a593Smuzhiyun 			       const char *propname, u64 *out_values,
377*4882a593Smuzhiyun 			       size_t sz_min, size_t sz_max)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	size_t sz, count;
380*4882a593Smuzhiyun 	const __be32 *val = of_find_property_value_of_size(np, propname,
381*4882a593Smuzhiyun 						(sz_min * sizeof(*out_values)),
382*4882a593Smuzhiyun 						(sz_max * sizeof(*out_values)),
383*4882a593Smuzhiyun 						&sz);
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	if (IS_ERR(val))
386*4882a593Smuzhiyun 		return PTR_ERR(val);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	if (!sz_max)
389*4882a593Smuzhiyun 		sz = sz_min;
390*4882a593Smuzhiyun 	else
391*4882a593Smuzhiyun 		sz /= sizeof(*out_values);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	count = sz;
394*4882a593Smuzhiyun 	while (count--) {
395*4882a593Smuzhiyun 		*out_values++ = of_read_number(val, 2);
396*4882a593Smuzhiyun 		val += 2;
397*4882a593Smuzhiyun 	}
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	return sz;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun /**
404*4882a593Smuzhiyun  * of_property_read_string - Find and read a string from a property
405*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
406*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
407*4882a593Smuzhiyun  * @out_string:	pointer to null terminated return string, modified only if
408*4882a593Smuzhiyun  *		return value is 0.
409*4882a593Smuzhiyun  *
410*4882a593Smuzhiyun  * Search for a property in a device tree node and retrieve a null
411*4882a593Smuzhiyun  * terminated string value (pointer to data, not a copy). Returns 0 on
412*4882a593Smuzhiyun  * success, -EINVAL if the property does not exist, -ENODATA if property
413*4882a593Smuzhiyun  * does not have a value, and -EILSEQ if the string is not null-terminated
414*4882a593Smuzhiyun  * within the length of the property data.
415*4882a593Smuzhiyun  *
416*4882a593Smuzhiyun  * The out_string pointer is modified only if a valid string can be decoded.
417*4882a593Smuzhiyun  */
of_property_read_string(const struct device_node * np,const char * propname,const char ** out_string)418*4882a593Smuzhiyun int of_property_read_string(const struct device_node *np, const char *propname,
419*4882a593Smuzhiyun 				const char **out_string)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	const struct property *prop = of_find_property(np, propname, NULL);
422*4882a593Smuzhiyun 	if (!prop)
423*4882a593Smuzhiyun 		return -EINVAL;
424*4882a593Smuzhiyun 	if (!prop->value)
425*4882a593Smuzhiyun 		return -ENODATA;
426*4882a593Smuzhiyun 	if (strnlen(prop->value, prop->length) >= prop->length)
427*4882a593Smuzhiyun 		return -EILSEQ;
428*4882a593Smuzhiyun 	*out_string = prop->value;
429*4882a593Smuzhiyun 	return 0;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_string);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun /**
434*4882a593Smuzhiyun  * of_property_match_string() - Find string in a list and return index
435*4882a593Smuzhiyun  * @np: pointer to node containing string list property
436*4882a593Smuzhiyun  * @propname: string list property name
437*4882a593Smuzhiyun  * @string: pointer to string to search for in string list
438*4882a593Smuzhiyun  *
439*4882a593Smuzhiyun  * This function searches a string list property and returns the index
440*4882a593Smuzhiyun  * of a specific string value.
441*4882a593Smuzhiyun  */
of_property_match_string(const struct device_node * np,const char * propname,const char * string)442*4882a593Smuzhiyun int of_property_match_string(const struct device_node *np, const char *propname,
443*4882a593Smuzhiyun 			     const char *string)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	const struct property *prop = of_find_property(np, propname, NULL);
446*4882a593Smuzhiyun 	size_t l;
447*4882a593Smuzhiyun 	int i;
448*4882a593Smuzhiyun 	const char *p, *end;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	if (!prop)
451*4882a593Smuzhiyun 		return -EINVAL;
452*4882a593Smuzhiyun 	if (!prop->value)
453*4882a593Smuzhiyun 		return -ENODATA;
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	p = prop->value;
456*4882a593Smuzhiyun 	end = p + prop->length;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	for (i = 0; p < end; i++, p += l) {
459*4882a593Smuzhiyun 		l = strnlen(p, end - p) + 1;
460*4882a593Smuzhiyun 		if (p + l > end)
461*4882a593Smuzhiyun 			return -EILSEQ;
462*4882a593Smuzhiyun 		pr_debug("comparing %s with %s\n", string, p);
463*4882a593Smuzhiyun 		if (strcmp(string, p) == 0)
464*4882a593Smuzhiyun 			return i; /* Found it; return index */
465*4882a593Smuzhiyun 	}
466*4882a593Smuzhiyun 	return -ENODATA;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_match_string);
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun /**
471*4882a593Smuzhiyun  * of_property_read_string_helper() - Utility helper for parsing string properties
472*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
473*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
474*4882a593Smuzhiyun  * @out_strs:	output array of string pointers.
475*4882a593Smuzhiyun  * @sz:		number of array elements to read.
476*4882a593Smuzhiyun  * @skip:	Number of strings to skip over at beginning of list.
477*4882a593Smuzhiyun  *
478*4882a593Smuzhiyun  * Don't call this function directly. It is a utility helper for the
479*4882a593Smuzhiyun  * of_property_read_string*() family of functions.
480*4882a593Smuzhiyun  */
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int skip)481*4882a593Smuzhiyun int of_property_read_string_helper(const struct device_node *np,
482*4882a593Smuzhiyun 				   const char *propname, const char **out_strs,
483*4882a593Smuzhiyun 				   size_t sz, int skip)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun 	const struct property *prop = of_find_property(np, propname, NULL);
486*4882a593Smuzhiyun 	int l = 0, i = 0;
487*4882a593Smuzhiyun 	const char *p, *end;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	if (!prop)
490*4882a593Smuzhiyun 		return -EINVAL;
491*4882a593Smuzhiyun 	if (!prop->value)
492*4882a593Smuzhiyun 		return -ENODATA;
493*4882a593Smuzhiyun 	p = prop->value;
494*4882a593Smuzhiyun 	end = p + prop->length;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
497*4882a593Smuzhiyun 		l = strnlen(p, end - p) + 1;
498*4882a593Smuzhiyun 		if (p + l > end)
499*4882a593Smuzhiyun 			return -EILSEQ;
500*4882a593Smuzhiyun 		if (out_strs && i >= skip)
501*4882a593Smuzhiyun 			*out_strs++ = p;
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun 	i -= skip;
504*4882a593Smuzhiyun 	return i <= 0 ? -ENODATA : i;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_property_read_string_helper);
507*4882a593Smuzhiyun 
of_prop_next_u32(struct property * prop,const __be32 * cur,u32 * pu)508*4882a593Smuzhiyun const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
509*4882a593Smuzhiyun 			       u32 *pu)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	const void *curv = cur;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	if (!prop)
514*4882a593Smuzhiyun 		return NULL;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	if (!cur) {
517*4882a593Smuzhiyun 		curv = prop->value;
518*4882a593Smuzhiyun 		goto out_val;
519*4882a593Smuzhiyun 	}
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	curv += sizeof(*cur);
522*4882a593Smuzhiyun 	if (curv >= prop->value + prop->length)
523*4882a593Smuzhiyun 		return NULL;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun out_val:
526*4882a593Smuzhiyun 	*pu = be32_to_cpup(curv);
527*4882a593Smuzhiyun 	return curv;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_prop_next_u32);
530*4882a593Smuzhiyun 
of_prop_next_string(struct property * prop,const char * cur)531*4882a593Smuzhiyun const char *of_prop_next_string(struct property *prop, const char *cur)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun 	const void *curv = cur;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	if (!prop)
536*4882a593Smuzhiyun 		return NULL;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	if (!cur)
539*4882a593Smuzhiyun 		return prop->value;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	curv += strlen(cur) + 1;
542*4882a593Smuzhiyun 	if (curv >= prop->value + prop->length)
543*4882a593Smuzhiyun 		return NULL;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	return curv;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_prop_next_string);
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun /**
550*4882a593Smuzhiyun  * of_graph_parse_endpoint() - parse common endpoint node properties
551*4882a593Smuzhiyun  * @node: pointer to endpoint device_node
552*4882a593Smuzhiyun  * @endpoint: pointer to the OF endpoint data structure
553*4882a593Smuzhiyun  *
554*4882a593Smuzhiyun  * The caller should hold a reference to @node.
555*4882a593Smuzhiyun  */
of_graph_parse_endpoint(const struct device_node * node,struct of_endpoint * endpoint)556*4882a593Smuzhiyun int of_graph_parse_endpoint(const struct device_node *node,
557*4882a593Smuzhiyun 			    struct of_endpoint *endpoint)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun 	struct device_node *port_node = of_get_parent(node);
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
562*4882a593Smuzhiyun 		  __func__, node);
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	memset(endpoint, 0, sizeof(*endpoint));
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	endpoint->local_node = node;
567*4882a593Smuzhiyun 	/*
568*4882a593Smuzhiyun 	 * It doesn't matter whether the two calls below succeed.
569*4882a593Smuzhiyun 	 * If they don't then the default value 0 is used.
570*4882a593Smuzhiyun 	 */
571*4882a593Smuzhiyun 	of_property_read_u32(port_node, "reg", &endpoint->port);
572*4882a593Smuzhiyun 	of_property_read_u32(node, "reg", &endpoint->id);
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	of_node_put(port_node);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	return 0;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_parse_endpoint);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun /**
581*4882a593Smuzhiyun  * of_graph_get_port_by_id() - get the port matching a given id
582*4882a593Smuzhiyun  * @parent: pointer to the parent device node
583*4882a593Smuzhiyun  * @id: id of the port
584*4882a593Smuzhiyun  *
585*4882a593Smuzhiyun  * Return: A 'port' node pointer with refcount incremented. The caller
586*4882a593Smuzhiyun  * has to use of_node_put() on it when done.
587*4882a593Smuzhiyun  */
of_graph_get_port_by_id(struct device_node * parent,u32 id)588*4882a593Smuzhiyun struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun 	struct device_node *node, *port;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	node = of_get_child_by_name(parent, "ports");
593*4882a593Smuzhiyun 	if (node)
594*4882a593Smuzhiyun 		parent = node;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	for_each_child_of_node(parent, port) {
597*4882a593Smuzhiyun 		u32 port_id = 0;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		if (!of_node_name_eq(port, "port"))
600*4882a593Smuzhiyun 			continue;
601*4882a593Smuzhiyun 		of_property_read_u32(port, "reg", &port_id);
602*4882a593Smuzhiyun 		if (id == port_id)
603*4882a593Smuzhiyun 			break;
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	of_node_put(node);
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	return port;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_port_by_id);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun /**
613*4882a593Smuzhiyun  * of_graph_get_next_endpoint() - get next endpoint node
614*4882a593Smuzhiyun  * @parent: pointer to the parent device node
615*4882a593Smuzhiyun  * @prev: previous endpoint node, or NULL to get first
616*4882a593Smuzhiyun  *
617*4882a593Smuzhiyun  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
618*4882a593Smuzhiyun  * of the passed @prev node is decremented.
619*4882a593Smuzhiyun  */
of_graph_get_next_endpoint(const struct device_node * parent,struct device_node * prev)620*4882a593Smuzhiyun struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
621*4882a593Smuzhiyun 					struct device_node *prev)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun 	struct device_node *endpoint;
624*4882a593Smuzhiyun 	struct device_node *port;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	if (!parent)
627*4882a593Smuzhiyun 		return NULL;
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	/*
630*4882a593Smuzhiyun 	 * Start by locating the port node. If no previous endpoint is specified
631*4882a593Smuzhiyun 	 * search for the first port node, otherwise get the previous endpoint
632*4882a593Smuzhiyun 	 * parent port node.
633*4882a593Smuzhiyun 	 */
634*4882a593Smuzhiyun 	if (!prev) {
635*4882a593Smuzhiyun 		struct device_node *node;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 		node = of_get_child_by_name(parent, "ports");
638*4882a593Smuzhiyun 		if (node)
639*4882a593Smuzhiyun 			parent = node;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 		port = of_get_child_by_name(parent, "port");
642*4882a593Smuzhiyun 		of_node_put(node);
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 		if (!port) {
645*4882a593Smuzhiyun 			pr_err("graph: no port node found in %pOF\n", parent);
646*4882a593Smuzhiyun 			return NULL;
647*4882a593Smuzhiyun 		}
648*4882a593Smuzhiyun 	} else {
649*4882a593Smuzhiyun 		port = of_get_parent(prev);
650*4882a593Smuzhiyun 		if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
651*4882a593Smuzhiyun 			      __func__, prev))
652*4882a593Smuzhiyun 			return NULL;
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	while (1) {
656*4882a593Smuzhiyun 		/*
657*4882a593Smuzhiyun 		 * Now that we have a port node, get the next endpoint by
658*4882a593Smuzhiyun 		 * getting the next child. If the previous endpoint is NULL this
659*4882a593Smuzhiyun 		 * will return the first child.
660*4882a593Smuzhiyun 		 */
661*4882a593Smuzhiyun 		endpoint = of_get_next_child(port, prev);
662*4882a593Smuzhiyun 		if (endpoint) {
663*4882a593Smuzhiyun 			of_node_put(port);
664*4882a593Smuzhiyun 			return endpoint;
665*4882a593Smuzhiyun 		}
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		/* No more endpoints under this port, try the next one. */
668*4882a593Smuzhiyun 		prev = NULL;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 		do {
671*4882a593Smuzhiyun 			port = of_get_next_child(parent, port);
672*4882a593Smuzhiyun 			if (!port)
673*4882a593Smuzhiyun 				return NULL;
674*4882a593Smuzhiyun 		} while (!of_node_name_eq(port, "port"));
675*4882a593Smuzhiyun 	}
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_next_endpoint);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun /**
680*4882a593Smuzhiyun  * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
681*4882a593Smuzhiyun  * @parent: pointer to the parent device node
682*4882a593Smuzhiyun  * @port_reg: identifier (value of reg property) of the parent port node
683*4882a593Smuzhiyun  * @reg: identifier (value of reg property) of the endpoint node
684*4882a593Smuzhiyun  *
685*4882a593Smuzhiyun  * Return: An 'endpoint' node pointer which is identified by reg and at the same
686*4882a593Smuzhiyun  * is the child of a port node identified by port_reg. reg and port_reg are
687*4882a593Smuzhiyun  * ignored when they are -1. Use of_node_put() on the pointer when done.
688*4882a593Smuzhiyun  */
of_graph_get_endpoint_by_regs(const struct device_node * parent,int port_reg,int reg)689*4882a593Smuzhiyun struct device_node *of_graph_get_endpoint_by_regs(
690*4882a593Smuzhiyun 	const struct device_node *parent, int port_reg, int reg)
691*4882a593Smuzhiyun {
692*4882a593Smuzhiyun 	struct of_endpoint endpoint;
693*4882a593Smuzhiyun 	struct device_node *node = NULL;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	for_each_endpoint_of_node(parent, node) {
696*4882a593Smuzhiyun 		of_graph_parse_endpoint(node, &endpoint);
697*4882a593Smuzhiyun 		if (((port_reg == -1) || (endpoint.port == port_reg)) &&
698*4882a593Smuzhiyun 			((reg == -1) || (endpoint.id == reg)))
699*4882a593Smuzhiyun 			return node;
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	return NULL;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun /**
707*4882a593Smuzhiyun  * of_graph_get_remote_endpoint() - get remote endpoint node
708*4882a593Smuzhiyun  * @node: pointer to a local endpoint device_node
709*4882a593Smuzhiyun  *
710*4882a593Smuzhiyun  * Return: Remote endpoint node associated with remote endpoint node linked
711*4882a593Smuzhiyun  *	   to @node. Use of_node_put() on it when done.
712*4882a593Smuzhiyun  */
of_graph_get_remote_endpoint(const struct device_node * node)713*4882a593Smuzhiyun struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
714*4882a593Smuzhiyun {
715*4882a593Smuzhiyun 	/* Get remote endpoint node. */
716*4882a593Smuzhiyun 	return of_parse_phandle(node, "remote-endpoint", 0);
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_remote_endpoint);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun /**
721*4882a593Smuzhiyun  * of_graph_get_port_parent() - get port's parent node
722*4882a593Smuzhiyun  * @node: pointer to a local endpoint device_node
723*4882a593Smuzhiyun  *
724*4882a593Smuzhiyun  * Return: device node associated with endpoint node linked
725*4882a593Smuzhiyun  *	   to @node. Use of_node_put() on it when done.
726*4882a593Smuzhiyun  */
of_graph_get_port_parent(struct device_node * node)727*4882a593Smuzhiyun struct device_node *of_graph_get_port_parent(struct device_node *node)
728*4882a593Smuzhiyun {
729*4882a593Smuzhiyun 	unsigned int depth;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if (!node)
732*4882a593Smuzhiyun 		return NULL;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	/*
735*4882a593Smuzhiyun 	 * Preserve usecount for passed in node as of_get_next_parent()
736*4882a593Smuzhiyun 	 * will do of_node_put() on it.
737*4882a593Smuzhiyun 	 */
738*4882a593Smuzhiyun 	of_node_get(node);
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	/* Walk 3 levels up only if there is 'ports' node. */
741*4882a593Smuzhiyun 	for (depth = 3; depth && node; depth--) {
742*4882a593Smuzhiyun 		node = of_get_next_parent(node);
743*4882a593Smuzhiyun 		if (depth == 2 && !of_node_name_eq(node, "ports"))
744*4882a593Smuzhiyun 			break;
745*4882a593Smuzhiyun 	}
746*4882a593Smuzhiyun 	return node;
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_port_parent);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun /**
751*4882a593Smuzhiyun  * of_graph_get_remote_port_parent() - get remote port's parent node
752*4882a593Smuzhiyun  * @node: pointer to a local endpoint device_node
753*4882a593Smuzhiyun  *
754*4882a593Smuzhiyun  * Return: Remote device node associated with remote endpoint node linked
755*4882a593Smuzhiyun  *	   to @node. Use of_node_put() on it when done.
756*4882a593Smuzhiyun  */
of_graph_get_remote_port_parent(const struct device_node * node)757*4882a593Smuzhiyun struct device_node *of_graph_get_remote_port_parent(
758*4882a593Smuzhiyun 			       const struct device_node *node)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	struct device_node *np, *pp;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	/* Get remote endpoint node. */
763*4882a593Smuzhiyun 	np = of_graph_get_remote_endpoint(node);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	pp = of_graph_get_port_parent(np);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	of_node_put(np);
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	return pp;
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_remote_port_parent);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun /**
774*4882a593Smuzhiyun  * of_graph_get_remote_port() - get remote port node
775*4882a593Smuzhiyun  * @node: pointer to a local endpoint device_node
776*4882a593Smuzhiyun  *
777*4882a593Smuzhiyun  * Return: Remote port node associated with remote endpoint node linked
778*4882a593Smuzhiyun  *	   to @node. Use of_node_put() on it when done.
779*4882a593Smuzhiyun  */
of_graph_get_remote_port(const struct device_node * node)780*4882a593Smuzhiyun struct device_node *of_graph_get_remote_port(const struct device_node *node)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun 	struct device_node *np;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	/* Get remote endpoint node. */
785*4882a593Smuzhiyun 	np = of_graph_get_remote_endpoint(node);
786*4882a593Smuzhiyun 	if (!np)
787*4882a593Smuzhiyun 		return NULL;
788*4882a593Smuzhiyun 	return of_get_next_parent(np);
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_remote_port);
791*4882a593Smuzhiyun 
of_graph_get_endpoint_count(const struct device_node * np)792*4882a593Smuzhiyun int of_graph_get_endpoint_count(const struct device_node *np)
793*4882a593Smuzhiyun {
794*4882a593Smuzhiyun 	struct device_node *endpoint;
795*4882a593Smuzhiyun 	int num = 0;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	for_each_endpoint_of_node(np, endpoint)
798*4882a593Smuzhiyun 		num++;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	return num;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_endpoint_count);
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun /**
805*4882a593Smuzhiyun  * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
806*4882a593Smuzhiyun  * @node: pointer to parent device_node containing graph port/endpoint
807*4882a593Smuzhiyun  * @port: identifier (value of reg property) of the parent port node
808*4882a593Smuzhiyun  * @endpoint: identifier (value of reg property) of the endpoint node
809*4882a593Smuzhiyun  *
810*4882a593Smuzhiyun  * Return: Remote device node associated with remote endpoint node linked
811*4882a593Smuzhiyun  *	   to @node. Use of_node_put() on it when done.
812*4882a593Smuzhiyun  */
of_graph_get_remote_node(const struct device_node * node,u32 port,u32 endpoint)813*4882a593Smuzhiyun struct device_node *of_graph_get_remote_node(const struct device_node *node,
814*4882a593Smuzhiyun 					     u32 port, u32 endpoint)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun 	struct device_node *endpoint_node, *remote;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
819*4882a593Smuzhiyun 	if (!endpoint_node) {
820*4882a593Smuzhiyun 		pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
821*4882a593Smuzhiyun 			 port, endpoint, node);
822*4882a593Smuzhiyun 		return NULL;
823*4882a593Smuzhiyun 	}
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	remote = of_graph_get_remote_port_parent(endpoint_node);
826*4882a593Smuzhiyun 	of_node_put(endpoint_node);
827*4882a593Smuzhiyun 	if (!remote) {
828*4882a593Smuzhiyun 		pr_debug("no valid remote node\n");
829*4882a593Smuzhiyun 		return NULL;
830*4882a593Smuzhiyun 	}
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	if (!of_device_is_available(remote)) {
833*4882a593Smuzhiyun 		pr_debug("not available for remote node\n");
834*4882a593Smuzhiyun 		of_node_put(remote);
835*4882a593Smuzhiyun 		return NULL;
836*4882a593Smuzhiyun 	}
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	return remote;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun EXPORT_SYMBOL(of_graph_get_remote_node);
841*4882a593Smuzhiyun 
of_fwnode_get(struct fwnode_handle * fwnode)842*4882a593Smuzhiyun static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun 	return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun 
of_fwnode_put(struct fwnode_handle * fwnode)847*4882a593Smuzhiyun static void of_fwnode_put(struct fwnode_handle *fwnode)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun 	of_node_put(to_of_node(fwnode));
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun 
of_fwnode_device_is_available(const struct fwnode_handle * fwnode)852*4882a593Smuzhiyun static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	return of_device_is_available(to_of_node(fwnode));
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
of_fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)857*4882a593Smuzhiyun static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
858*4882a593Smuzhiyun 				       const char *propname)
859*4882a593Smuzhiyun {
860*4882a593Smuzhiyun 	return of_property_read_bool(to_of_node(fwnode), propname);
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun 
of_fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)863*4882a593Smuzhiyun static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
864*4882a593Smuzhiyun 					     const char *propname,
865*4882a593Smuzhiyun 					     unsigned int elem_size, void *val,
866*4882a593Smuzhiyun 					     size_t nval)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun 	const struct device_node *node = to_of_node(fwnode);
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	if (!val)
871*4882a593Smuzhiyun 		return of_property_count_elems_of_size(node, propname,
872*4882a593Smuzhiyun 						       elem_size);
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	switch (elem_size) {
875*4882a593Smuzhiyun 	case sizeof(u8):
876*4882a593Smuzhiyun 		return of_property_read_u8_array(node, propname, val, nval);
877*4882a593Smuzhiyun 	case sizeof(u16):
878*4882a593Smuzhiyun 		return of_property_read_u16_array(node, propname, val, nval);
879*4882a593Smuzhiyun 	case sizeof(u32):
880*4882a593Smuzhiyun 		return of_property_read_u32_array(node, propname, val, nval);
881*4882a593Smuzhiyun 	case sizeof(u64):
882*4882a593Smuzhiyun 		return of_property_read_u64_array(node, propname, val, nval);
883*4882a593Smuzhiyun 	}
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	return -ENXIO;
886*4882a593Smuzhiyun }
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun static int
of_fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)889*4882a593Smuzhiyun of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
890*4882a593Smuzhiyun 				     const char *propname, const char **val,
891*4882a593Smuzhiyun 				     size_t nval)
892*4882a593Smuzhiyun {
893*4882a593Smuzhiyun 	const struct device_node *node = to_of_node(fwnode);
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	return val ?
896*4882a593Smuzhiyun 		of_property_read_string_array(node, propname, val, nval) :
897*4882a593Smuzhiyun 		of_property_count_strings(node, propname);
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
of_fwnode_get_name(const struct fwnode_handle * fwnode)900*4882a593Smuzhiyun static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun 	return kbasename(to_of_node(fwnode)->full_name);
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun 
of_fwnode_get_name_prefix(const struct fwnode_handle * fwnode)905*4882a593Smuzhiyun static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	/* Root needs no prefix here (its name is "/"). */
908*4882a593Smuzhiyun 	if (!to_of_node(fwnode)->parent)
909*4882a593Smuzhiyun 		return "";
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	return "/";
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun static struct fwnode_handle *
of_fwnode_get_parent(const struct fwnode_handle * fwnode)915*4882a593Smuzhiyun of_fwnode_get_parent(const struct fwnode_handle *fwnode)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun 	return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun static struct fwnode_handle *
of_fwnode_get_next_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)921*4882a593Smuzhiyun of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
922*4882a593Smuzhiyun 			      struct fwnode_handle *child)
923*4882a593Smuzhiyun {
924*4882a593Smuzhiyun 	return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
925*4882a593Smuzhiyun 							    to_of_node(child)));
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun static struct fwnode_handle *
of_fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)929*4882a593Smuzhiyun of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
930*4882a593Smuzhiyun 			       const char *childname)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	const struct device_node *node = to_of_node(fwnode);
933*4882a593Smuzhiyun 	struct device_node *child;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	for_each_available_child_of_node(node, child)
936*4882a593Smuzhiyun 		if (of_node_name_eq(child, childname))
937*4882a593Smuzhiyun 			return of_fwnode_handle(child);
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	return NULL;
940*4882a593Smuzhiyun }
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun static int
of_fwnode_get_reference_args(const struct fwnode_handle * fwnode,const char * prop,const char * nargs_prop,unsigned int nargs,unsigned int index,struct fwnode_reference_args * args)943*4882a593Smuzhiyun of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
944*4882a593Smuzhiyun 			     const char *prop, const char *nargs_prop,
945*4882a593Smuzhiyun 			     unsigned int nargs, unsigned int index,
946*4882a593Smuzhiyun 			     struct fwnode_reference_args *args)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun 	struct of_phandle_args of_args;
949*4882a593Smuzhiyun 	unsigned int i;
950*4882a593Smuzhiyun 	int ret;
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	if (nargs_prop)
953*4882a593Smuzhiyun 		ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
954*4882a593Smuzhiyun 						 nargs_prop, index, &of_args);
955*4882a593Smuzhiyun 	else
956*4882a593Smuzhiyun 		ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
957*4882a593Smuzhiyun 						       nargs, index, &of_args);
958*4882a593Smuzhiyun 	if (ret < 0)
959*4882a593Smuzhiyun 		return ret;
960*4882a593Smuzhiyun 	if (!args) {
961*4882a593Smuzhiyun 		of_node_put(of_args.np);
962*4882a593Smuzhiyun 		return 0;
963*4882a593Smuzhiyun 	}
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	args->nargs = of_args.args_count;
966*4882a593Smuzhiyun 	args->fwnode = of_fwnode_handle(of_args.np);
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
969*4882a593Smuzhiyun 		args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	return 0;
972*4882a593Smuzhiyun }
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun static struct fwnode_handle *
of_fwnode_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)975*4882a593Smuzhiyun of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
976*4882a593Smuzhiyun 				  struct fwnode_handle *prev)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun 	return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
979*4882a593Smuzhiyun 							   to_of_node(prev)));
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun static struct fwnode_handle *
of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle * fwnode)983*4882a593Smuzhiyun of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
984*4882a593Smuzhiyun {
985*4882a593Smuzhiyun 	return of_fwnode_handle(
986*4882a593Smuzhiyun 		of_graph_get_remote_endpoint(to_of_node(fwnode)));
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun static struct fwnode_handle *
of_fwnode_graph_get_port_parent(struct fwnode_handle * fwnode)990*4882a593Smuzhiyun of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun 	struct device_node *np;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	/* Get the parent of the port */
995*4882a593Smuzhiyun 	np = of_get_parent(to_of_node(fwnode));
996*4882a593Smuzhiyun 	if (!np)
997*4882a593Smuzhiyun 		return NULL;
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	/* Is this the "ports" node? If not, it's the port parent. */
1000*4882a593Smuzhiyun 	if (!of_node_name_eq(np, "ports"))
1001*4882a593Smuzhiyun 		return of_fwnode_handle(np);
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	return of_fwnode_handle(of_get_next_parent(np));
1004*4882a593Smuzhiyun }
1005*4882a593Smuzhiyun 
of_fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)1006*4882a593Smuzhiyun static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1007*4882a593Smuzhiyun 					  struct fwnode_endpoint *endpoint)
1008*4882a593Smuzhiyun {
1009*4882a593Smuzhiyun 	const struct device_node *node = to_of_node(fwnode);
1010*4882a593Smuzhiyun 	struct device_node *port_node = of_get_parent(node);
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	endpoint->local_fwnode = fwnode;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	of_property_read_u32(port_node, "reg", &endpoint->port);
1015*4882a593Smuzhiyun 	of_property_read_u32(node, "reg", &endpoint->id);
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	of_node_put(port_node);
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	return 0;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun static const void *
of_fwnode_device_get_match_data(const struct fwnode_handle * fwnode,const struct device * dev)1023*4882a593Smuzhiyun of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1024*4882a593Smuzhiyun 				const struct device *dev)
1025*4882a593Smuzhiyun {
1026*4882a593Smuzhiyun 	return of_device_get_match_data(dev);
1027*4882a593Smuzhiyun }
1028*4882a593Smuzhiyun 
of_is_ancestor_of(struct device_node * test_ancestor,struct device_node * child)1029*4882a593Smuzhiyun static bool of_is_ancestor_of(struct device_node *test_ancestor,
1030*4882a593Smuzhiyun 			      struct device_node *child)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun 	of_node_get(child);
1033*4882a593Smuzhiyun 	while (child) {
1034*4882a593Smuzhiyun 		if (child == test_ancestor) {
1035*4882a593Smuzhiyun 			of_node_put(child);
1036*4882a593Smuzhiyun 			return true;
1037*4882a593Smuzhiyun 		}
1038*4882a593Smuzhiyun 		child = of_get_next_parent(child);
1039*4882a593Smuzhiyun 	}
1040*4882a593Smuzhiyun 	return false;
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun /**
1044*4882a593Smuzhiyun  * of_link_to_phandle - Add fwnode link to supplier from supplier phandle
1045*4882a593Smuzhiyun  * @con_np: consumer device tree node
1046*4882a593Smuzhiyun  * @sup_np: supplier device tree node
1047*4882a593Smuzhiyun  *
1048*4882a593Smuzhiyun  * Given a phandle to a supplier device tree node (@sup_np), this function
1049*4882a593Smuzhiyun  * finds the device that owns the supplier device tree node and creates a
1050*4882a593Smuzhiyun  * device link from @dev consumer device to the supplier device. This function
1051*4882a593Smuzhiyun  * doesn't create device links for invalid scenarios such as trying to create a
1052*4882a593Smuzhiyun  * link with a parent device as the consumer of its child device. In such
1053*4882a593Smuzhiyun  * cases, it returns an error.
1054*4882a593Smuzhiyun  *
1055*4882a593Smuzhiyun  * Returns:
1056*4882a593Smuzhiyun  * - 0 if fwnode link successfully created to supplier
1057*4882a593Smuzhiyun  * - -EINVAL if the supplier link is invalid and should not be created
1058*4882a593Smuzhiyun  * - -ENODEV if struct device will never be create for supplier
1059*4882a593Smuzhiyun  */
of_link_to_phandle(struct device_node * con_np,struct device_node * sup_np)1060*4882a593Smuzhiyun static int of_link_to_phandle(struct device_node *con_np,
1061*4882a593Smuzhiyun 			      struct device_node *sup_np)
1062*4882a593Smuzhiyun {
1063*4882a593Smuzhiyun 	struct device *sup_dev;
1064*4882a593Smuzhiyun 	struct device_node *tmp_np = sup_np;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	of_node_get(sup_np);
1067*4882a593Smuzhiyun 	/*
1068*4882a593Smuzhiyun 	 * Find the device node that contains the supplier phandle.  It may be
1069*4882a593Smuzhiyun 	 * @sup_np or it may be an ancestor of @sup_np.
1070*4882a593Smuzhiyun 	 */
1071*4882a593Smuzhiyun 	while (sup_np) {
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 		/* Don't allow linking to a disabled supplier */
1074*4882a593Smuzhiyun 		if (!of_device_is_available(sup_np)) {
1075*4882a593Smuzhiyun 			of_node_put(sup_np);
1076*4882a593Smuzhiyun 			sup_np = NULL;
1077*4882a593Smuzhiyun 		}
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 		if (of_find_property(sup_np, "compatible", NULL))
1080*4882a593Smuzhiyun 			break;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 		sup_np = of_get_next_parent(sup_np);
1083*4882a593Smuzhiyun 	}
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	if (!sup_np) {
1086*4882a593Smuzhiyun 		pr_debug("Not linking %pOFP to %pOFP - No device\n",
1087*4882a593Smuzhiyun 			 con_np, tmp_np);
1088*4882a593Smuzhiyun 		return -ENODEV;
1089*4882a593Smuzhiyun 	}
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	/*
1092*4882a593Smuzhiyun 	 * Don't allow linking a device node as a consumer of one of its
1093*4882a593Smuzhiyun 	 * descendant nodes. By definition, a child node can't be a functional
1094*4882a593Smuzhiyun 	 * dependency for the parent node.
1095*4882a593Smuzhiyun 	 */
1096*4882a593Smuzhiyun 	if (of_is_ancestor_of(con_np, sup_np)) {
1097*4882a593Smuzhiyun 		pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
1098*4882a593Smuzhiyun 			 con_np, sup_np);
1099*4882a593Smuzhiyun 		of_node_put(sup_np);
1100*4882a593Smuzhiyun 		return -EINVAL;
1101*4882a593Smuzhiyun 	}
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	/*
1104*4882a593Smuzhiyun 	 * Don't create links to "early devices" that won't have struct devices
1105*4882a593Smuzhiyun 	 * created for them.
1106*4882a593Smuzhiyun 	 */
1107*4882a593Smuzhiyun 	sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1108*4882a593Smuzhiyun 	if (!sup_dev &&
1109*4882a593Smuzhiyun 	    (of_node_check_flag(sup_np, OF_POPULATED) ||
1110*4882a593Smuzhiyun 	     sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) {
1111*4882a593Smuzhiyun 		pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1112*4882a593Smuzhiyun 			 con_np, sup_np);
1113*4882a593Smuzhiyun 		of_node_put(sup_np);
1114*4882a593Smuzhiyun 		return -ENODEV;
1115*4882a593Smuzhiyun 	}
1116*4882a593Smuzhiyun 	put_device(sup_dev);
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1119*4882a593Smuzhiyun 	of_node_put(sup_np);
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	return 0;
1122*4882a593Smuzhiyun }
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun /**
1125*4882a593Smuzhiyun  * parse_prop_cells - Property parsing function for suppliers
1126*4882a593Smuzhiyun  *
1127*4882a593Smuzhiyun  * @np:		Pointer to device tree node containing a list
1128*4882a593Smuzhiyun  * @prop_name:	Name of property to be parsed. Expected to hold phandle values
1129*4882a593Smuzhiyun  * @index:	For properties holding a list of phandles, this is the index
1130*4882a593Smuzhiyun  *		into the list.
1131*4882a593Smuzhiyun  * @list_name:	Property name that is known to contain list of phandle(s) to
1132*4882a593Smuzhiyun  *		supplier(s)
1133*4882a593Smuzhiyun  * @cells_name:	property name that specifies phandles' arguments count
1134*4882a593Smuzhiyun  *
1135*4882a593Smuzhiyun  * This is a helper function to parse properties that have a known fixed name
1136*4882a593Smuzhiyun  * and are a list of phandles and phandle arguments.
1137*4882a593Smuzhiyun  *
1138*4882a593Smuzhiyun  * Returns:
1139*4882a593Smuzhiyun  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1140*4882a593Smuzhiyun  *   on it when done.
1141*4882a593Smuzhiyun  * - NULL if no phandle found at index
1142*4882a593Smuzhiyun  */
parse_prop_cells(struct device_node * np,const char * prop_name,int index,const char * list_name,const char * cells_name)1143*4882a593Smuzhiyun static struct device_node *parse_prop_cells(struct device_node *np,
1144*4882a593Smuzhiyun 					    const char *prop_name, int index,
1145*4882a593Smuzhiyun 					    const char *list_name,
1146*4882a593Smuzhiyun 					    const char *cells_name)
1147*4882a593Smuzhiyun {
1148*4882a593Smuzhiyun 	struct of_phandle_args sup_args;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	if (strcmp(prop_name, list_name))
1151*4882a593Smuzhiyun 		return NULL;
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1154*4882a593Smuzhiyun 				       &sup_args))
1155*4882a593Smuzhiyun 		return NULL;
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	return sup_args.np;
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun #define DEFINE_SIMPLE_PROP(fname, name, cells)				  \
1161*4882a593Smuzhiyun static struct device_node *parse_##fname(struct device_node *np,	  \
1162*4882a593Smuzhiyun 					const char *prop_name, int index) \
1163*4882a593Smuzhiyun {									  \
1164*4882a593Smuzhiyun 	return parse_prop_cells(np, prop_name, index, name, cells);	  \
1165*4882a593Smuzhiyun }
1166*4882a593Smuzhiyun 
strcmp_suffix(const char * str,const char * suffix)1167*4882a593Smuzhiyun static int strcmp_suffix(const char *str, const char *suffix)
1168*4882a593Smuzhiyun {
1169*4882a593Smuzhiyun 	unsigned int len, suffix_len;
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	len = strlen(str);
1172*4882a593Smuzhiyun 	suffix_len = strlen(suffix);
1173*4882a593Smuzhiyun 	if (len <= suffix_len)
1174*4882a593Smuzhiyun 		return -1;
1175*4882a593Smuzhiyun 	return strcmp(str + len - suffix_len, suffix);
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun /**
1179*4882a593Smuzhiyun  * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1180*4882a593Smuzhiyun  *
1181*4882a593Smuzhiyun  * @np:		Pointer to device tree node containing a list
1182*4882a593Smuzhiyun  * @prop_name:	Name of property to be parsed. Expected to hold phandle values
1183*4882a593Smuzhiyun  * @index:	For properties holding a list of phandles, this is the index
1184*4882a593Smuzhiyun  *		into the list.
1185*4882a593Smuzhiyun  * @suffix:	Property suffix that is known to contain list of phandle(s) to
1186*4882a593Smuzhiyun  *		supplier(s)
1187*4882a593Smuzhiyun  * @cells_name:	property name that specifies phandles' arguments count
1188*4882a593Smuzhiyun  *
1189*4882a593Smuzhiyun  * This is a helper function to parse properties that have a known fixed suffix
1190*4882a593Smuzhiyun  * and are a list of phandles and phandle arguments.
1191*4882a593Smuzhiyun  *
1192*4882a593Smuzhiyun  * Returns:
1193*4882a593Smuzhiyun  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1194*4882a593Smuzhiyun  *   on it when done.
1195*4882a593Smuzhiyun  * - NULL if no phandle found at index
1196*4882a593Smuzhiyun  */
parse_suffix_prop_cells(struct device_node * np,const char * prop_name,int index,const char * suffix,const char * cells_name)1197*4882a593Smuzhiyun static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1198*4882a593Smuzhiyun 					    const char *prop_name, int index,
1199*4882a593Smuzhiyun 					    const char *suffix,
1200*4882a593Smuzhiyun 					    const char *cells_name)
1201*4882a593Smuzhiyun {
1202*4882a593Smuzhiyun 	struct of_phandle_args sup_args;
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun 	if (strcmp_suffix(prop_name, suffix))
1205*4882a593Smuzhiyun 		return NULL;
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1208*4882a593Smuzhiyun 				       &sup_args))
1209*4882a593Smuzhiyun 		return NULL;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	return sup_args.np;
1212*4882a593Smuzhiyun }
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun #define DEFINE_SUFFIX_PROP(fname, suffix, cells)			     \
1215*4882a593Smuzhiyun static struct device_node *parse_##fname(struct device_node *np,	     \
1216*4882a593Smuzhiyun 					const char *prop_name, int index)    \
1217*4882a593Smuzhiyun {									     \
1218*4882a593Smuzhiyun 	return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun /**
1222*4882a593Smuzhiyun  * struct supplier_bindings - Property parsing functions for suppliers
1223*4882a593Smuzhiyun  *
1224*4882a593Smuzhiyun  * @parse_prop: function name
1225*4882a593Smuzhiyun  *	parse_prop() finds the node corresponding to a supplier phandle
1226*4882a593Smuzhiyun  * @parse_prop.np: Pointer to device node holding supplier phandle property
1227*4882a593Smuzhiyun  * @parse_prop.prop_name: Name of property holding a phandle value
1228*4882a593Smuzhiyun  * @parse_prop.index: For properties holding a list of phandles, this is the
1229*4882a593Smuzhiyun  *		      index into the list
1230*4882a593Smuzhiyun  *
1231*4882a593Smuzhiyun  * Returns:
1232*4882a593Smuzhiyun  * parse_prop() return values are
1233*4882a593Smuzhiyun  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1234*4882a593Smuzhiyun  *   on it when done.
1235*4882a593Smuzhiyun  * - NULL if no phandle found at index
1236*4882a593Smuzhiyun  */
1237*4882a593Smuzhiyun struct supplier_bindings {
1238*4882a593Smuzhiyun 	struct device_node *(*parse_prop)(struct device_node *np,
1239*4882a593Smuzhiyun 					  const char *prop_name, int index);
1240*4882a593Smuzhiyun 	bool optional;
1241*4882a593Smuzhiyun };
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1244*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1245*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1246*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1247*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1248*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1249*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1250*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1251*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1252*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1253*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1254*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1255*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1256*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1257*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1258*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1259*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1260*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1261*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1262*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1263*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1264*4882a593Smuzhiyun DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1265*4882a593Smuzhiyun DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1266*4882a593Smuzhiyun DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1267*4882a593Smuzhiyun 
parse_gpios(struct device_node * np,const char * prop_name,int index)1268*4882a593Smuzhiyun static struct device_node *parse_gpios(struct device_node *np,
1269*4882a593Smuzhiyun 				       const char *prop_name, int index)
1270*4882a593Smuzhiyun {
1271*4882a593Smuzhiyun 	if (!strcmp_suffix(prop_name, ",nr-gpios"))
1272*4882a593Smuzhiyun 		return NULL;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1275*4882a593Smuzhiyun 				       "#gpio-cells");
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun 
parse_iommu_maps(struct device_node * np,const char * prop_name,int index)1278*4882a593Smuzhiyun static struct device_node *parse_iommu_maps(struct device_node *np,
1279*4882a593Smuzhiyun 					    const char *prop_name, int index)
1280*4882a593Smuzhiyun {
1281*4882a593Smuzhiyun 	if (strcmp(prop_name, "iommu-map"))
1282*4882a593Smuzhiyun 		return NULL;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	return of_parse_phandle(np, prop_name, (index * 4) + 1);
1285*4882a593Smuzhiyun }
1286*4882a593Smuzhiyun 
parse_gpio_compat(struct device_node * np,const char * prop_name,int index)1287*4882a593Smuzhiyun static struct device_node *parse_gpio_compat(struct device_node *np,
1288*4882a593Smuzhiyun 					     const char *prop_name, int index)
1289*4882a593Smuzhiyun {
1290*4882a593Smuzhiyun 	struct of_phandle_args sup_args;
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1293*4882a593Smuzhiyun 		return NULL;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	/*
1296*4882a593Smuzhiyun 	 * Ignore node with gpio-hog property since its gpios are all provided
1297*4882a593Smuzhiyun 	 * by its parent.
1298*4882a593Smuzhiyun 	 */
1299*4882a593Smuzhiyun 	if (of_find_property(np, "gpio-hog", NULL))
1300*4882a593Smuzhiyun 		return NULL;
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1303*4882a593Smuzhiyun 				       &sup_args))
1304*4882a593Smuzhiyun 		return NULL;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	return sup_args.np;
1307*4882a593Smuzhiyun }
1308*4882a593Smuzhiyun 
parse_interrupts(struct device_node * np,const char * prop_name,int index)1309*4882a593Smuzhiyun static struct device_node *parse_interrupts(struct device_node *np,
1310*4882a593Smuzhiyun 					    const char *prop_name, int index)
1311*4882a593Smuzhiyun {
1312*4882a593Smuzhiyun 	struct of_phandle_args sup_args;
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1315*4882a593Smuzhiyun 		return NULL;
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	if (strcmp(prop_name, "interrupts") &&
1318*4882a593Smuzhiyun 	    strcmp(prop_name, "interrupts-extended"))
1319*4882a593Smuzhiyun 		return NULL;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1322*4882a593Smuzhiyun }
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun static const struct supplier_bindings of_supplier_bindings[] = {
1325*4882a593Smuzhiyun 	{ .parse_prop = parse_clocks, },
1326*4882a593Smuzhiyun 	{ .parse_prop = parse_interconnects, },
1327*4882a593Smuzhiyun 	{ .parse_prop = parse_iommus, .optional = true, },
1328*4882a593Smuzhiyun 	{ .parse_prop = parse_iommu_maps, .optional = true, },
1329*4882a593Smuzhiyun 	{ .parse_prop = parse_mboxes, },
1330*4882a593Smuzhiyun 	{ .parse_prop = parse_io_channels, },
1331*4882a593Smuzhiyun 	{ .parse_prop = parse_interrupt_parent, },
1332*4882a593Smuzhiyun 	{ .parse_prop = parse_dmas, .optional = true, },
1333*4882a593Smuzhiyun 	{ .parse_prop = parse_power_domains, },
1334*4882a593Smuzhiyun 	{ .parse_prop = parse_hwlocks, },
1335*4882a593Smuzhiyun 	{ .parse_prop = parse_extcon, },
1336*4882a593Smuzhiyun 	{ .parse_prop = parse_nvmem_cells, },
1337*4882a593Smuzhiyun 	{ .parse_prop = parse_phys, },
1338*4882a593Smuzhiyun 	{ .parse_prop = parse_wakeup_parent, },
1339*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl0, },
1340*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl1, },
1341*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl2, },
1342*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl3, },
1343*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl4, },
1344*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl5, },
1345*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl6, },
1346*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl7, },
1347*4882a593Smuzhiyun 	{ .parse_prop = parse_pinctrl8, },
1348*4882a593Smuzhiyun 	{ .parse_prop = parse_gpio_compat, },
1349*4882a593Smuzhiyun 	{ .parse_prop = parse_interrupts, },
1350*4882a593Smuzhiyun 	{ .parse_prop = parse_regulators, },
1351*4882a593Smuzhiyun 	{ .parse_prop = parse_gpio, },
1352*4882a593Smuzhiyun 	{ .parse_prop = parse_gpios, },
1353*4882a593Smuzhiyun 	{}
1354*4882a593Smuzhiyun };
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun /**
1357*4882a593Smuzhiyun  * of_link_property - Create device links to suppliers listed in a property
1358*4882a593Smuzhiyun  * @dev: Consumer device
1359*4882a593Smuzhiyun  * @con_np: The consumer device tree node which contains the property
1360*4882a593Smuzhiyun  * @prop_name: Name of property to be parsed
1361*4882a593Smuzhiyun  *
1362*4882a593Smuzhiyun  * This function checks if the property @prop_name that is present in the
1363*4882a593Smuzhiyun  * @con_np device tree node is one of the known common device tree bindings
1364*4882a593Smuzhiyun  * that list phandles to suppliers. If @prop_name isn't one, this function
1365*4882a593Smuzhiyun  * doesn't do anything.
1366*4882a593Smuzhiyun  *
1367*4882a593Smuzhiyun  * If @prop_name is one, this function attempts to create fwnode links from the
1368*4882a593Smuzhiyun  * consumer device tree node @con_np to all the suppliers device tree nodes
1369*4882a593Smuzhiyun  * listed in @prop_name.
1370*4882a593Smuzhiyun  *
1371*4882a593Smuzhiyun  * Any failed attempt to create a fwnode link will NOT result in an immediate
1372*4882a593Smuzhiyun  * return.  of_link_property() must create links to all the available supplier
1373*4882a593Smuzhiyun  * device tree nodes even when attempts to create a link to one or more
1374*4882a593Smuzhiyun  * suppliers fail.
1375*4882a593Smuzhiyun  */
of_link_property(struct device_node * con_np,const char * prop_name)1376*4882a593Smuzhiyun static int of_link_property(struct device_node *con_np, const char *prop_name)
1377*4882a593Smuzhiyun {
1378*4882a593Smuzhiyun 	struct device_node *phandle;
1379*4882a593Smuzhiyun 	const struct supplier_bindings *s = of_supplier_bindings;
1380*4882a593Smuzhiyun 	unsigned int i = 0;
1381*4882a593Smuzhiyun 	bool matched = false;
1382*4882a593Smuzhiyun 	int ret = 0;
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 	/* Do not stop at first failed link, link all available suppliers. */
1385*4882a593Smuzhiyun 	while (!matched && s->parse_prop) {
1386*4882a593Smuzhiyun 		if (s->optional && !fw_devlink_is_strict()) {
1387*4882a593Smuzhiyun 			s++;
1388*4882a593Smuzhiyun 			continue;
1389*4882a593Smuzhiyun 		}
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 		while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1392*4882a593Smuzhiyun 			matched = true;
1393*4882a593Smuzhiyun 			i++;
1394*4882a593Smuzhiyun 			of_link_to_phandle(con_np, phandle);
1395*4882a593Smuzhiyun 			of_node_put(phandle);
1396*4882a593Smuzhiyun 		}
1397*4882a593Smuzhiyun 		s++;
1398*4882a593Smuzhiyun 	}
1399*4882a593Smuzhiyun 	return ret;
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun 
of_fwnode_add_links(struct fwnode_handle * fwnode)1402*4882a593Smuzhiyun static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1403*4882a593Smuzhiyun {
1404*4882a593Smuzhiyun 	struct property *p;
1405*4882a593Smuzhiyun 	struct device_node *con_np = to_of_node(fwnode);
1406*4882a593Smuzhiyun 
1407*4882a593Smuzhiyun 	if (!con_np)
1408*4882a593Smuzhiyun 		return -EINVAL;
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun 	for_each_property_of_node(con_np, p)
1411*4882a593Smuzhiyun 		of_link_property(con_np, p->name);
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 	return 0;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun const struct fwnode_operations of_fwnode_ops = {
1417*4882a593Smuzhiyun 	.get = of_fwnode_get,
1418*4882a593Smuzhiyun 	.put = of_fwnode_put,
1419*4882a593Smuzhiyun 	.device_is_available = of_fwnode_device_is_available,
1420*4882a593Smuzhiyun 	.device_get_match_data = of_fwnode_device_get_match_data,
1421*4882a593Smuzhiyun 	.property_present = of_fwnode_property_present,
1422*4882a593Smuzhiyun 	.property_read_int_array = of_fwnode_property_read_int_array,
1423*4882a593Smuzhiyun 	.property_read_string_array = of_fwnode_property_read_string_array,
1424*4882a593Smuzhiyun 	.get_name = of_fwnode_get_name,
1425*4882a593Smuzhiyun 	.get_name_prefix = of_fwnode_get_name_prefix,
1426*4882a593Smuzhiyun 	.get_parent = of_fwnode_get_parent,
1427*4882a593Smuzhiyun 	.get_next_child_node = of_fwnode_get_next_child_node,
1428*4882a593Smuzhiyun 	.get_named_child_node = of_fwnode_get_named_child_node,
1429*4882a593Smuzhiyun 	.get_reference_args = of_fwnode_get_reference_args,
1430*4882a593Smuzhiyun 	.graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1431*4882a593Smuzhiyun 	.graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1432*4882a593Smuzhiyun 	.graph_get_port_parent = of_fwnode_graph_get_port_parent,
1433*4882a593Smuzhiyun 	.graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1434*4882a593Smuzhiyun 	.add_links = of_fwnode_add_links,
1435*4882a593Smuzhiyun };
1436*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_fwnode_ops);
1437