1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * property.c - Unified device property interface.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2014, Intel Corporation
6*4882a593Smuzhiyun * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7*4882a593Smuzhiyun * Mika Westerberg <mika.westerberg@linux.intel.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/acpi.h>
11*4882a593Smuzhiyun #include <linux/export.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun #include <linux/of_address.h>
15*4882a593Smuzhiyun #include <linux/of_graph.h>
16*4882a593Smuzhiyun #include <linux/of_irq.h>
17*4882a593Smuzhiyun #include <linux/property.h>
18*4882a593Smuzhiyun #include <linux/etherdevice.h>
19*4882a593Smuzhiyun #include <linux/phy.h>
20*4882a593Smuzhiyun
dev_fwnode(struct device * dev)21*4882a593Smuzhiyun struct fwnode_handle *dev_fwnode(struct device *dev)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun return IS_ENABLED(CONFIG_OF) && dev->of_node ?
24*4882a593Smuzhiyun &dev->of_node->fwnode : dev->fwnode;
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dev_fwnode);
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun * device_property_present - check if a property of a device is present
30*4882a593Smuzhiyun * @dev: Device whose property is being checked
31*4882a593Smuzhiyun * @propname: Name of the property
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * Check if property @propname is present in the device firmware description.
34*4882a593Smuzhiyun */
device_property_present(struct device * dev,const char * propname)35*4882a593Smuzhiyun bool device_property_present(struct device *dev, const char *propname)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun return fwnode_property_present(dev_fwnode(dev), propname);
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_property_present);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun * fwnode_property_present - check if a property of a firmware node is present
43*4882a593Smuzhiyun * @fwnode: Firmware node whose property to check
44*4882a593Smuzhiyun * @propname: Name of the property
45*4882a593Smuzhiyun */
fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)46*4882a593Smuzhiyun bool fwnode_property_present(const struct fwnode_handle *fwnode,
47*4882a593Smuzhiyun const char *propname)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun bool ret;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun ret = fwnode_call_bool_op(fwnode, property_present, propname);
52*4882a593Smuzhiyun if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
53*4882a593Smuzhiyun !IS_ERR_OR_NULL(fwnode->secondary))
54*4882a593Smuzhiyun ret = fwnode_call_bool_op(fwnode->secondary, property_present,
55*4882a593Smuzhiyun propname);
56*4882a593Smuzhiyun return ret;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_present);
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun * device_property_read_u8_array - return a u8 array property of a device
62*4882a593Smuzhiyun * @dev: Device to get the property of
63*4882a593Smuzhiyun * @propname: Name of the property
64*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
65*4882a593Smuzhiyun * @nval: Size of the @val array
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * Function reads an array of u8 properties with @propname from the device
68*4882a593Smuzhiyun * firmware description and stores them to @val if found.
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun * Return: number of values if @val was %NULL,
71*4882a593Smuzhiyun * %0 if the property was found (success),
72*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
73*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
74*4882a593Smuzhiyun * %-EPROTO if the property is not an array of numbers,
75*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected.
76*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
77*4882a593Smuzhiyun */
device_property_read_u8_array(struct device * dev,const char * propname,u8 * val,size_t nval)78*4882a593Smuzhiyun int device_property_read_u8_array(struct device *dev, const char *propname,
79*4882a593Smuzhiyun u8 *val, size_t nval)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_property_read_u8_array);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /**
86*4882a593Smuzhiyun * device_property_read_u16_array - return a u16 array property of a device
87*4882a593Smuzhiyun * @dev: Device to get the property of
88*4882a593Smuzhiyun * @propname: Name of the property
89*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
90*4882a593Smuzhiyun * @nval: Size of the @val array
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * Function reads an array of u16 properties with @propname from the device
93*4882a593Smuzhiyun * firmware description and stores them to @val if found.
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * Return: number of values if @val was %NULL,
96*4882a593Smuzhiyun * %0 if the property was found (success),
97*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
98*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
99*4882a593Smuzhiyun * %-EPROTO if the property is not an array of numbers,
100*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected.
101*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
102*4882a593Smuzhiyun */
device_property_read_u16_array(struct device * dev,const char * propname,u16 * val,size_t nval)103*4882a593Smuzhiyun int device_property_read_u16_array(struct device *dev, const char *propname,
104*4882a593Smuzhiyun u16 *val, size_t nval)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_property_read_u16_array);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun * device_property_read_u32_array - return a u32 array property of a device
112*4882a593Smuzhiyun * @dev: Device to get the property of
113*4882a593Smuzhiyun * @propname: Name of the property
114*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
115*4882a593Smuzhiyun * @nval: Size of the @val array
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * Function reads an array of u32 properties with @propname from the device
118*4882a593Smuzhiyun * firmware description and stores them to @val if found.
119*4882a593Smuzhiyun *
120*4882a593Smuzhiyun * Return: number of values if @val was %NULL,
121*4882a593Smuzhiyun * %0 if the property was found (success),
122*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
123*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
124*4882a593Smuzhiyun * %-EPROTO if the property is not an array of numbers,
125*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected.
126*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
127*4882a593Smuzhiyun */
device_property_read_u32_array(struct device * dev,const char * propname,u32 * val,size_t nval)128*4882a593Smuzhiyun int device_property_read_u32_array(struct device *dev, const char *propname,
129*4882a593Smuzhiyun u32 *val, size_t nval)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_property_read_u32_array);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /**
136*4882a593Smuzhiyun * device_property_read_u64_array - return a u64 array property of a device
137*4882a593Smuzhiyun * @dev: Device to get the property of
138*4882a593Smuzhiyun * @propname: Name of the property
139*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
140*4882a593Smuzhiyun * @nval: Size of the @val array
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * Function reads an array of u64 properties with @propname from the device
143*4882a593Smuzhiyun * firmware description and stores them to @val if found.
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * Return: number of values if @val was %NULL,
146*4882a593Smuzhiyun * %0 if the property was found (success),
147*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
148*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
149*4882a593Smuzhiyun * %-EPROTO if the property is not an array of numbers,
150*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected.
151*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
152*4882a593Smuzhiyun */
device_property_read_u64_array(struct device * dev,const char * propname,u64 * val,size_t nval)153*4882a593Smuzhiyun int device_property_read_u64_array(struct device *dev, const char *propname,
154*4882a593Smuzhiyun u64 *val, size_t nval)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_property_read_u64_array);
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /**
161*4882a593Smuzhiyun * device_property_read_string_array - return a string array property of device
162*4882a593Smuzhiyun * @dev: Device to get the property of
163*4882a593Smuzhiyun * @propname: Name of the property
164*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
165*4882a593Smuzhiyun * @nval: Size of the @val array
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * Function reads an array of string properties with @propname from the device
168*4882a593Smuzhiyun * firmware description and stores them to @val if found.
169*4882a593Smuzhiyun *
170*4882a593Smuzhiyun * Return: number of values read on success if @val is non-NULL,
171*4882a593Smuzhiyun * number of values available on success if @val is NULL,
172*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
173*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
174*4882a593Smuzhiyun * %-EPROTO or %-EILSEQ if the property is not an array of strings,
175*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected.
176*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
177*4882a593Smuzhiyun */
device_property_read_string_array(struct device * dev,const char * propname,const char ** val,size_t nval)178*4882a593Smuzhiyun int device_property_read_string_array(struct device *dev, const char *propname,
179*4882a593Smuzhiyun const char **val, size_t nval)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_property_read_string_array);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun * device_property_read_string - return a string property of a device
187*4882a593Smuzhiyun * @dev: Device to get the property of
188*4882a593Smuzhiyun * @propname: Name of the property
189*4882a593Smuzhiyun * @val: The value is stored here
190*4882a593Smuzhiyun *
191*4882a593Smuzhiyun * Function reads property @propname from the device firmware description and
192*4882a593Smuzhiyun * stores the value into @val if found. The value is checked to be a string.
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * Return: %0 if the property was found (success),
195*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
196*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
197*4882a593Smuzhiyun * %-EPROTO or %-EILSEQ if the property type is not a string.
198*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
199*4882a593Smuzhiyun */
device_property_read_string(struct device * dev,const char * propname,const char ** val)200*4882a593Smuzhiyun int device_property_read_string(struct device *dev, const char *propname,
201*4882a593Smuzhiyun const char **val)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun return fwnode_property_read_string(dev_fwnode(dev), propname, val);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_property_read_string);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /**
208*4882a593Smuzhiyun * device_property_match_string - find a string in an array and return index
209*4882a593Smuzhiyun * @dev: Device to get the property of
210*4882a593Smuzhiyun * @propname: Name of the property holding the array
211*4882a593Smuzhiyun * @string: String to look for
212*4882a593Smuzhiyun *
213*4882a593Smuzhiyun * Find a given string in a string array and if it is found return the
214*4882a593Smuzhiyun * index back.
215*4882a593Smuzhiyun *
216*4882a593Smuzhiyun * Return: %0 if the property was found (success),
217*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
218*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
219*4882a593Smuzhiyun * %-EPROTO if the property is not an array of strings,
220*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
221*4882a593Smuzhiyun */
device_property_match_string(struct device * dev,const char * propname,const char * string)222*4882a593Smuzhiyun int device_property_match_string(struct device *dev, const char *propname,
223*4882a593Smuzhiyun const char *string)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun return fwnode_property_match_string(dev_fwnode(dev), propname, string);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_property_match_string);
228*4882a593Smuzhiyun
fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)229*4882a593Smuzhiyun static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
230*4882a593Smuzhiyun const char *propname,
231*4882a593Smuzhiyun unsigned int elem_size, void *val,
232*4882a593Smuzhiyun size_t nval)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun int ret;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
237*4882a593Smuzhiyun elem_size, val, nval);
238*4882a593Smuzhiyun if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
239*4882a593Smuzhiyun !IS_ERR_OR_NULL(fwnode->secondary))
240*4882a593Smuzhiyun ret = fwnode_call_int_op(
241*4882a593Smuzhiyun fwnode->secondary, property_read_int_array, propname,
242*4882a593Smuzhiyun elem_size, val, nval);
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun return ret;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /**
248*4882a593Smuzhiyun * fwnode_property_read_u8_array - return a u8 array property of firmware node
249*4882a593Smuzhiyun * @fwnode: Firmware node to get the property of
250*4882a593Smuzhiyun * @propname: Name of the property
251*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
252*4882a593Smuzhiyun * @nval: Size of the @val array
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * Read an array of u8 properties with @propname from @fwnode and stores them to
255*4882a593Smuzhiyun * @val if found.
256*4882a593Smuzhiyun *
257*4882a593Smuzhiyun * Return: number of values if @val was %NULL,
258*4882a593Smuzhiyun * %0 if the property was found (success),
259*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
260*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
261*4882a593Smuzhiyun * %-EPROTO if the property is not an array of numbers,
262*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected,
263*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
264*4882a593Smuzhiyun */
fwnode_property_read_u8_array(const struct fwnode_handle * fwnode,const char * propname,u8 * val,size_t nval)265*4882a593Smuzhiyun int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
266*4882a593Smuzhiyun const char *propname, u8 *val, size_t nval)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
269*4882a593Smuzhiyun val, nval);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /**
274*4882a593Smuzhiyun * fwnode_property_read_u16_array - return a u16 array property of firmware node
275*4882a593Smuzhiyun * @fwnode: Firmware node to get the property of
276*4882a593Smuzhiyun * @propname: Name of the property
277*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
278*4882a593Smuzhiyun * @nval: Size of the @val array
279*4882a593Smuzhiyun *
280*4882a593Smuzhiyun * Read an array of u16 properties with @propname from @fwnode and store them to
281*4882a593Smuzhiyun * @val if found.
282*4882a593Smuzhiyun *
283*4882a593Smuzhiyun * Return: number of values if @val was %NULL,
284*4882a593Smuzhiyun * %0 if the property was found (success),
285*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
286*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
287*4882a593Smuzhiyun * %-EPROTO if the property is not an array of numbers,
288*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected,
289*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
290*4882a593Smuzhiyun */
fwnode_property_read_u16_array(const struct fwnode_handle * fwnode,const char * propname,u16 * val,size_t nval)291*4882a593Smuzhiyun int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
292*4882a593Smuzhiyun const char *propname, u16 *val, size_t nval)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
295*4882a593Smuzhiyun val, nval);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /**
300*4882a593Smuzhiyun * fwnode_property_read_u32_array - return a u32 array property of firmware node
301*4882a593Smuzhiyun * @fwnode: Firmware node to get the property of
302*4882a593Smuzhiyun * @propname: Name of the property
303*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
304*4882a593Smuzhiyun * @nval: Size of the @val array
305*4882a593Smuzhiyun *
306*4882a593Smuzhiyun * Read an array of u32 properties with @propname from @fwnode store them to
307*4882a593Smuzhiyun * @val if found.
308*4882a593Smuzhiyun *
309*4882a593Smuzhiyun * Return: number of values if @val was %NULL,
310*4882a593Smuzhiyun * %0 if the property was found (success),
311*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
312*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
313*4882a593Smuzhiyun * %-EPROTO if the property is not an array of numbers,
314*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected,
315*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
316*4882a593Smuzhiyun */
fwnode_property_read_u32_array(const struct fwnode_handle * fwnode,const char * propname,u32 * val,size_t nval)317*4882a593Smuzhiyun int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
318*4882a593Smuzhiyun const char *propname, u32 *val, size_t nval)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
321*4882a593Smuzhiyun val, nval);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /**
326*4882a593Smuzhiyun * fwnode_property_read_u64_array - return a u64 array property firmware node
327*4882a593Smuzhiyun * @fwnode: Firmware node to get the property of
328*4882a593Smuzhiyun * @propname: Name of the property
329*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
330*4882a593Smuzhiyun * @nval: Size of the @val array
331*4882a593Smuzhiyun *
332*4882a593Smuzhiyun * Read an array of u64 properties with @propname from @fwnode and store them to
333*4882a593Smuzhiyun * @val if found.
334*4882a593Smuzhiyun *
335*4882a593Smuzhiyun * Return: number of values if @val was %NULL,
336*4882a593Smuzhiyun * %0 if the property was found (success),
337*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
338*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
339*4882a593Smuzhiyun * %-EPROTO if the property is not an array of numbers,
340*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected,
341*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
342*4882a593Smuzhiyun */
fwnode_property_read_u64_array(const struct fwnode_handle * fwnode,const char * propname,u64 * val,size_t nval)343*4882a593Smuzhiyun int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
344*4882a593Smuzhiyun const char *propname, u64 *val, size_t nval)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
347*4882a593Smuzhiyun val, nval);
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /**
352*4882a593Smuzhiyun * fwnode_property_read_string_array - return string array property of a node
353*4882a593Smuzhiyun * @fwnode: Firmware node to get the property of
354*4882a593Smuzhiyun * @propname: Name of the property
355*4882a593Smuzhiyun * @val: The values are stored here or %NULL to return the number of values
356*4882a593Smuzhiyun * @nval: Size of the @val array
357*4882a593Smuzhiyun *
358*4882a593Smuzhiyun * Read an string list property @propname from the given firmware node and store
359*4882a593Smuzhiyun * them to @val if found.
360*4882a593Smuzhiyun *
361*4882a593Smuzhiyun * Return: number of values read on success if @val is non-NULL,
362*4882a593Smuzhiyun * number of values available on success if @val is NULL,
363*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
364*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
365*4882a593Smuzhiyun * %-EPROTO or %-EILSEQ if the property is not an array of strings,
366*4882a593Smuzhiyun * %-EOVERFLOW if the size of the property is not as expected,
367*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
368*4882a593Smuzhiyun */
fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)369*4882a593Smuzhiyun int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
370*4882a593Smuzhiyun const char *propname, const char **val,
371*4882a593Smuzhiyun size_t nval)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun int ret;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
376*4882a593Smuzhiyun val, nval);
377*4882a593Smuzhiyun if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
378*4882a593Smuzhiyun !IS_ERR_OR_NULL(fwnode->secondary))
379*4882a593Smuzhiyun ret = fwnode_call_int_op(fwnode->secondary,
380*4882a593Smuzhiyun property_read_string_array, propname,
381*4882a593Smuzhiyun val, nval);
382*4882a593Smuzhiyun return ret;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /**
387*4882a593Smuzhiyun * fwnode_property_read_string - return a string property of a firmware node
388*4882a593Smuzhiyun * @fwnode: Firmware node to get the property of
389*4882a593Smuzhiyun * @propname: Name of the property
390*4882a593Smuzhiyun * @val: The value is stored here
391*4882a593Smuzhiyun *
392*4882a593Smuzhiyun * Read property @propname from the given firmware node and store the value into
393*4882a593Smuzhiyun * @val if found. The value is checked to be a string.
394*4882a593Smuzhiyun *
395*4882a593Smuzhiyun * Return: %0 if the property was found (success),
396*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
397*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
398*4882a593Smuzhiyun * %-EPROTO or %-EILSEQ if the property is not a string,
399*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
400*4882a593Smuzhiyun */
fwnode_property_read_string(const struct fwnode_handle * fwnode,const char * propname,const char ** val)401*4882a593Smuzhiyun int fwnode_property_read_string(const struct fwnode_handle *fwnode,
402*4882a593Smuzhiyun const char *propname, const char **val)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun return ret < 0 ? ret : 0;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_read_string);
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /**
411*4882a593Smuzhiyun * fwnode_property_match_string - find a string in an array and return index
412*4882a593Smuzhiyun * @fwnode: Firmware node to get the property of
413*4882a593Smuzhiyun * @propname: Name of the property holding the array
414*4882a593Smuzhiyun * @string: String to look for
415*4882a593Smuzhiyun *
416*4882a593Smuzhiyun * Find a given string in a string array and if it is found return the
417*4882a593Smuzhiyun * index back.
418*4882a593Smuzhiyun *
419*4882a593Smuzhiyun * Return: %0 if the property was found (success),
420*4882a593Smuzhiyun * %-EINVAL if given arguments are not valid,
421*4882a593Smuzhiyun * %-ENODATA if the property does not have a value,
422*4882a593Smuzhiyun * %-EPROTO if the property is not an array of strings,
423*4882a593Smuzhiyun * %-ENXIO if no suitable firmware interface is present.
424*4882a593Smuzhiyun */
fwnode_property_match_string(const struct fwnode_handle * fwnode,const char * propname,const char * string)425*4882a593Smuzhiyun int fwnode_property_match_string(const struct fwnode_handle *fwnode,
426*4882a593Smuzhiyun const char *propname, const char *string)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun const char **values;
429*4882a593Smuzhiyun int nval, ret;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
432*4882a593Smuzhiyun if (nval < 0)
433*4882a593Smuzhiyun return nval;
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun if (nval == 0)
436*4882a593Smuzhiyun return -ENODATA;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
439*4882a593Smuzhiyun if (!values)
440*4882a593Smuzhiyun return -ENOMEM;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
443*4882a593Smuzhiyun if (ret < 0)
444*4882a593Smuzhiyun goto out;
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun ret = match_string(values, nval, string);
447*4882a593Smuzhiyun if (ret < 0)
448*4882a593Smuzhiyun ret = -ENODATA;
449*4882a593Smuzhiyun out:
450*4882a593Smuzhiyun kfree(values);
451*4882a593Smuzhiyun return ret;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_match_string);
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun /**
456*4882a593Smuzhiyun * fwnode_property_get_reference_args() - Find a reference with arguments
457*4882a593Smuzhiyun * @fwnode: Firmware node where to look for the reference
458*4882a593Smuzhiyun * @prop: The name of the property
459*4882a593Smuzhiyun * @nargs_prop: The name of the property telling the number of
460*4882a593Smuzhiyun * arguments in the referred node. NULL if @nargs is known,
461*4882a593Smuzhiyun * otherwise @nargs is ignored. Only relevant on OF.
462*4882a593Smuzhiyun * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.
463*4882a593Smuzhiyun * @index: Index of the reference, from zero onwards.
464*4882a593Smuzhiyun * @args: Result structure with reference and integer arguments.
465*4882a593Smuzhiyun *
466*4882a593Smuzhiyun * Obtain a reference based on a named property in an fwnode, with
467*4882a593Smuzhiyun * integer arguments.
468*4882a593Smuzhiyun *
469*4882a593Smuzhiyun * Caller is responsible to call fwnode_handle_put() on the returned
470*4882a593Smuzhiyun * args->fwnode pointer.
471*4882a593Smuzhiyun *
472*4882a593Smuzhiyun * Returns: %0 on success
473*4882a593Smuzhiyun * %-ENOENT when the index is out of bounds, the index has an empty
474*4882a593Smuzhiyun * reference or the property was not found
475*4882a593Smuzhiyun * %-EINVAL on parse error
476*4882a593Smuzhiyun */
fwnode_property_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)477*4882a593Smuzhiyun int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
478*4882a593Smuzhiyun const char *prop, const char *nargs_prop,
479*4882a593Smuzhiyun unsigned int nargs, unsigned int index,
480*4882a593Smuzhiyun struct fwnode_reference_args *args)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun return fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
483*4882a593Smuzhiyun nargs, index, args);
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /**
488*4882a593Smuzhiyun * fwnode_find_reference - Find named reference to a fwnode_handle
489*4882a593Smuzhiyun * @fwnode: Firmware node where to look for the reference
490*4882a593Smuzhiyun * @name: The name of the reference
491*4882a593Smuzhiyun * @index: Index of the reference
492*4882a593Smuzhiyun *
493*4882a593Smuzhiyun * @index can be used when the named reference holds a table of references.
494*4882a593Smuzhiyun *
495*4882a593Smuzhiyun * Returns pointer to the reference fwnode, or ERR_PTR. Caller is responsible to
496*4882a593Smuzhiyun * call fwnode_handle_put() on the returned fwnode pointer.
497*4882a593Smuzhiyun */
fwnode_find_reference(const struct fwnode_handle * fwnode,const char * name,unsigned int index)498*4882a593Smuzhiyun struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
499*4882a593Smuzhiyun const char *name,
500*4882a593Smuzhiyun unsigned int index)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun struct fwnode_reference_args args;
503*4882a593Smuzhiyun int ret;
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
506*4882a593Smuzhiyun &args);
507*4882a593Smuzhiyun return ret ? ERR_PTR(ret) : args.fwnode;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_find_reference);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun * device_remove_properties - Remove properties from a device object.
513*4882a593Smuzhiyun * @dev: Device whose properties to remove.
514*4882a593Smuzhiyun *
515*4882a593Smuzhiyun * The function removes properties previously associated to the device
516*4882a593Smuzhiyun * firmware node with device_add_properties(). Memory allocated to the
517*4882a593Smuzhiyun * properties will also be released.
518*4882a593Smuzhiyun */
device_remove_properties(struct device * dev)519*4882a593Smuzhiyun void device_remove_properties(struct device *dev)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun struct fwnode_handle *fwnode = dev_fwnode(dev);
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun if (!fwnode)
524*4882a593Smuzhiyun return;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun if (is_software_node(fwnode->secondary)) {
527*4882a593Smuzhiyun fwnode_remove_software_node(fwnode->secondary);
528*4882a593Smuzhiyun set_secondary_fwnode(dev, NULL);
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_remove_properties);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /**
534*4882a593Smuzhiyun * device_add_properties - Add a collection of properties to a device object.
535*4882a593Smuzhiyun * @dev: Device to add properties to.
536*4882a593Smuzhiyun * @properties: Collection of properties to add.
537*4882a593Smuzhiyun *
538*4882a593Smuzhiyun * Associate a collection of device properties represented by @properties with
539*4882a593Smuzhiyun * @dev. The function takes a copy of @properties.
540*4882a593Smuzhiyun *
541*4882a593Smuzhiyun * WARNING: The callers should not use this function if it is known that there
542*4882a593Smuzhiyun * is no real firmware node associated with @dev! In that case the callers
543*4882a593Smuzhiyun * should create a software node and assign it to @dev directly.
544*4882a593Smuzhiyun */
device_add_properties(struct device * dev,const struct property_entry * properties)545*4882a593Smuzhiyun int device_add_properties(struct device *dev,
546*4882a593Smuzhiyun const struct property_entry *properties)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun struct fwnode_handle *fwnode;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun fwnode = fwnode_create_software_node(properties, NULL);
551*4882a593Smuzhiyun if (IS_ERR(fwnode))
552*4882a593Smuzhiyun return PTR_ERR(fwnode);
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun set_secondary_fwnode(dev, fwnode);
555*4882a593Smuzhiyun return 0;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_add_properties);
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun /**
560*4882a593Smuzhiyun * fwnode_get_name - Return the name of a node
561*4882a593Smuzhiyun * @fwnode: The firmware node
562*4882a593Smuzhiyun *
563*4882a593Smuzhiyun * Returns a pointer to the node name.
564*4882a593Smuzhiyun */
fwnode_get_name(const struct fwnode_handle * fwnode)565*4882a593Smuzhiyun const char *fwnode_get_name(const struct fwnode_handle *fwnode)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun return fwnode_call_ptr_op(fwnode, get_name);
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_get_name);
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /**
572*4882a593Smuzhiyun * fwnode_get_name_prefix - Return the prefix of node for printing purposes
573*4882a593Smuzhiyun * @fwnode: The firmware node
574*4882a593Smuzhiyun *
575*4882a593Smuzhiyun * Returns the prefix of a node, intended to be printed right before the node.
576*4882a593Smuzhiyun * The prefix works also as a separator between the nodes.
577*4882a593Smuzhiyun */
fwnode_get_name_prefix(const struct fwnode_handle * fwnode)578*4882a593Smuzhiyun const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun return fwnode_call_ptr_op(fwnode, get_name_prefix);
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun /**
584*4882a593Smuzhiyun * fwnode_get_parent - Return parent firwmare node
585*4882a593Smuzhiyun * @fwnode: Firmware whose parent is retrieved
586*4882a593Smuzhiyun *
587*4882a593Smuzhiyun * Return parent firmware node of the given node if possible or %NULL if no
588*4882a593Smuzhiyun * parent was available.
589*4882a593Smuzhiyun */
fwnode_get_parent(const struct fwnode_handle * fwnode)590*4882a593Smuzhiyun struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun return fwnode_call_ptr_op(fwnode, get_parent);
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_get_parent);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun /**
597*4882a593Smuzhiyun * fwnode_get_next_parent - Iterate to the node's parent
598*4882a593Smuzhiyun * @fwnode: Firmware whose parent is retrieved
599*4882a593Smuzhiyun *
600*4882a593Smuzhiyun * This is like fwnode_get_parent() except that it drops the refcount
601*4882a593Smuzhiyun * on the passed node, making it suitable for iterating through a
602*4882a593Smuzhiyun * node's parents.
603*4882a593Smuzhiyun *
604*4882a593Smuzhiyun * Returns a node pointer with refcount incremented, use
605*4882a593Smuzhiyun * fwnode_handle_node() on it when done.
606*4882a593Smuzhiyun */
fwnode_get_next_parent(struct fwnode_handle * fwnode)607*4882a593Smuzhiyun struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
608*4882a593Smuzhiyun {
609*4882a593Smuzhiyun struct fwnode_handle *parent = fwnode_get_parent(fwnode);
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun fwnode_handle_put(fwnode);
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun return parent;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun /**
618*4882a593Smuzhiyun * fwnode_get_next_parent_dev - Find device of closest ancestor fwnode
619*4882a593Smuzhiyun * @fwnode: firmware node
620*4882a593Smuzhiyun *
621*4882a593Smuzhiyun * Given a firmware node (@fwnode), this function finds its closest ancestor
622*4882a593Smuzhiyun * firmware node that has a corresponding struct device and returns that struct
623*4882a593Smuzhiyun * device.
624*4882a593Smuzhiyun *
625*4882a593Smuzhiyun * The caller of this function is expected to call put_device() on the returned
626*4882a593Smuzhiyun * device when they are done.
627*4882a593Smuzhiyun */
fwnode_get_next_parent_dev(struct fwnode_handle * fwnode)628*4882a593Smuzhiyun struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun struct device *dev = NULL;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun fwnode_handle_get(fwnode);
633*4882a593Smuzhiyun do {
634*4882a593Smuzhiyun fwnode = fwnode_get_next_parent(fwnode);
635*4882a593Smuzhiyun if (fwnode)
636*4882a593Smuzhiyun dev = get_dev_from_fwnode(fwnode);
637*4882a593Smuzhiyun } while (fwnode && !dev);
638*4882a593Smuzhiyun fwnode_handle_put(fwnode);
639*4882a593Smuzhiyun return dev;
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun /**
643*4882a593Smuzhiyun * fwnode_count_parents - Return the number of parents a node has
644*4882a593Smuzhiyun * @fwnode: The node the parents of which are to be counted
645*4882a593Smuzhiyun *
646*4882a593Smuzhiyun * Returns the number of parents a node has.
647*4882a593Smuzhiyun */
fwnode_count_parents(const struct fwnode_handle * fwnode)648*4882a593Smuzhiyun unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun struct fwnode_handle *__fwnode;
651*4882a593Smuzhiyun unsigned int count;
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun __fwnode = fwnode_get_parent(fwnode);
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun for (count = 0; __fwnode; count++)
656*4882a593Smuzhiyun __fwnode = fwnode_get_next_parent(__fwnode);
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun return count;
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_count_parents);
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun /**
663*4882a593Smuzhiyun * fwnode_get_nth_parent - Return an nth parent of a node
664*4882a593Smuzhiyun * @fwnode: The node the parent of which is requested
665*4882a593Smuzhiyun * @depth: Distance of the parent from the node
666*4882a593Smuzhiyun *
667*4882a593Smuzhiyun * Returns the nth parent of a node. If there is no parent at the requested
668*4882a593Smuzhiyun * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
669*4882a593Smuzhiyun * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
670*4882a593Smuzhiyun *
671*4882a593Smuzhiyun * The caller is responsible for calling fwnode_handle_put() for the returned
672*4882a593Smuzhiyun * node.
673*4882a593Smuzhiyun */
fwnode_get_nth_parent(struct fwnode_handle * fwnode,unsigned int depth)674*4882a593Smuzhiyun struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
675*4882a593Smuzhiyun unsigned int depth)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun unsigned int i;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun fwnode_handle_get(fwnode);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun for (i = 0; i < depth && fwnode; i++)
682*4882a593Smuzhiyun fwnode = fwnode_get_next_parent(fwnode);
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun return fwnode;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun /**
689*4882a593Smuzhiyun * fwnode_is_ancestor_of - Test if @test_ancestor is ancestor of @test_child
690*4882a593Smuzhiyun * @test_ancestor: Firmware which is tested for being an ancestor
691*4882a593Smuzhiyun * @test_child: Firmware which is tested for being the child
692*4882a593Smuzhiyun *
693*4882a593Smuzhiyun * A node is considered an ancestor of itself too.
694*4882a593Smuzhiyun *
695*4882a593Smuzhiyun * Returns true if @test_ancestor is an ancestor of @test_child.
696*4882a593Smuzhiyun * Otherwise, returns false.
697*4882a593Smuzhiyun */
fwnode_is_ancestor_of(struct fwnode_handle * test_ancestor,struct fwnode_handle * test_child)698*4882a593Smuzhiyun bool fwnode_is_ancestor_of(struct fwnode_handle *test_ancestor,
699*4882a593Smuzhiyun struct fwnode_handle *test_child)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun if (!test_ancestor)
702*4882a593Smuzhiyun return false;
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun fwnode_handle_get(test_child);
705*4882a593Smuzhiyun while (test_child) {
706*4882a593Smuzhiyun if (test_child == test_ancestor) {
707*4882a593Smuzhiyun fwnode_handle_put(test_child);
708*4882a593Smuzhiyun return true;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun test_child = fwnode_get_next_parent(test_child);
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun return false;
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun /**
716*4882a593Smuzhiyun * fwnode_get_next_child_node - Return the next child node handle for a node
717*4882a593Smuzhiyun * @fwnode: Firmware node to find the next child node for.
718*4882a593Smuzhiyun * @child: Handle to one of the node's child nodes or a %NULL handle.
719*4882a593Smuzhiyun */
720*4882a593Smuzhiyun struct fwnode_handle *
fwnode_get_next_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)721*4882a593Smuzhiyun fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
722*4882a593Smuzhiyun struct fwnode_handle *child)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun /**
729*4882a593Smuzhiyun * fwnode_get_next_available_child_node - Return the next
730*4882a593Smuzhiyun * available child node handle for a node
731*4882a593Smuzhiyun * @fwnode: Firmware node to find the next child node for.
732*4882a593Smuzhiyun * @child: Handle to one of the node's child nodes or a %NULL handle.
733*4882a593Smuzhiyun */
734*4882a593Smuzhiyun struct fwnode_handle *
fwnode_get_next_available_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)735*4882a593Smuzhiyun fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
736*4882a593Smuzhiyun struct fwnode_handle *child)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun struct fwnode_handle *next_child = child;
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun if (!fwnode)
741*4882a593Smuzhiyun return NULL;
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun do {
744*4882a593Smuzhiyun next_child = fwnode_get_next_child_node(fwnode, next_child);
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun if (!next_child || fwnode_device_is_available(next_child))
747*4882a593Smuzhiyun break;
748*4882a593Smuzhiyun } while (next_child);
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun return next_child;
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun /**
755*4882a593Smuzhiyun * device_get_next_child_node - Return the next child node handle for a device
756*4882a593Smuzhiyun * @dev: Device to find the next child node for.
757*4882a593Smuzhiyun * @child: Handle to one of the device's child nodes or a null handle.
758*4882a593Smuzhiyun */
device_get_next_child_node(struct device * dev,struct fwnode_handle * child)759*4882a593Smuzhiyun struct fwnode_handle *device_get_next_child_node(struct device *dev,
760*4882a593Smuzhiyun struct fwnode_handle *child)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun struct acpi_device *adev = ACPI_COMPANION(dev);
763*4882a593Smuzhiyun struct fwnode_handle *fwnode = NULL, *next;
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun if (dev->of_node)
766*4882a593Smuzhiyun fwnode = &dev->of_node->fwnode;
767*4882a593Smuzhiyun else if (adev)
768*4882a593Smuzhiyun fwnode = acpi_fwnode_handle(adev);
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun /* Try to find a child in primary fwnode */
771*4882a593Smuzhiyun next = fwnode_get_next_child_node(fwnode, child);
772*4882a593Smuzhiyun if (next)
773*4882a593Smuzhiyun return next;
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun /* When no more children in primary, continue with secondary */
776*4882a593Smuzhiyun if (fwnode && !IS_ERR_OR_NULL(fwnode->secondary))
777*4882a593Smuzhiyun next = fwnode_get_next_child_node(fwnode->secondary, child);
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun return next;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_get_next_child_node);
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun /**
784*4882a593Smuzhiyun * fwnode_get_named_child_node - Return first matching named child node handle
785*4882a593Smuzhiyun * @fwnode: Firmware node to find the named child node for.
786*4882a593Smuzhiyun * @childname: String to match child node name against.
787*4882a593Smuzhiyun */
788*4882a593Smuzhiyun struct fwnode_handle *
fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)789*4882a593Smuzhiyun fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
790*4882a593Smuzhiyun const char *childname)
791*4882a593Smuzhiyun {
792*4882a593Smuzhiyun return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /**
797*4882a593Smuzhiyun * device_get_named_child_node - Return first matching named child node handle
798*4882a593Smuzhiyun * @dev: Device to find the named child node for.
799*4882a593Smuzhiyun * @childname: String to match child node name against.
800*4882a593Smuzhiyun */
device_get_named_child_node(struct device * dev,const char * childname)801*4882a593Smuzhiyun struct fwnode_handle *device_get_named_child_node(struct device *dev,
802*4882a593Smuzhiyun const char *childname)
803*4882a593Smuzhiyun {
804*4882a593Smuzhiyun return fwnode_get_named_child_node(dev_fwnode(dev), childname);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_get_named_child_node);
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun /**
809*4882a593Smuzhiyun * fwnode_handle_get - Obtain a reference to a device node
810*4882a593Smuzhiyun * @fwnode: Pointer to the device node to obtain the reference to.
811*4882a593Smuzhiyun *
812*4882a593Smuzhiyun * Returns the fwnode handle.
813*4882a593Smuzhiyun */
fwnode_handle_get(struct fwnode_handle * fwnode)814*4882a593Smuzhiyun struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun if (!fwnode_has_op(fwnode, get))
817*4882a593Smuzhiyun return fwnode;
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun return fwnode_call_ptr_op(fwnode, get);
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_handle_get);
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun /**
824*4882a593Smuzhiyun * fwnode_handle_put - Drop reference to a device node
825*4882a593Smuzhiyun * @fwnode: Pointer to the device node to drop the reference to.
826*4882a593Smuzhiyun *
827*4882a593Smuzhiyun * This has to be used when terminating device_for_each_child_node() iteration
828*4882a593Smuzhiyun * with break or return to prevent stale device node references from being left
829*4882a593Smuzhiyun * behind.
830*4882a593Smuzhiyun */
fwnode_handle_put(struct fwnode_handle * fwnode)831*4882a593Smuzhiyun void fwnode_handle_put(struct fwnode_handle *fwnode)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun fwnode_call_void_op(fwnode, put);
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_handle_put);
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun /**
838*4882a593Smuzhiyun * fwnode_device_is_available - check if a device is available for use
839*4882a593Smuzhiyun * @fwnode: Pointer to the fwnode of the device.
840*4882a593Smuzhiyun */
fwnode_device_is_available(const struct fwnode_handle * fwnode)841*4882a593Smuzhiyun bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
842*4882a593Smuzhiyun {
843*4882a593Smuzhiyun return fwnode_call_bool_op(fwnode, device_is_available);
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_device_is_available);
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun /**
848*4882a593Smuzhiyun * device_get_child_node_count - return the number of child nodes for device
849*4882a593Smuzhiyun * @dev: Device to cound the child nodes for
850*4882a593Smuzhiyun */
device_get_child_node_count(struct device * dev)851*4882a593Smuzhiyun unsigned int device_get_child_node_count(struct device *dev)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun struct fwnode_handle *child;
854*4882a593Smuzhiyun unsigned int count = 0;
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun device_for_each_child_node(dev, child)
857*4882a593Smuzhiyun count++;
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun return count;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_get_child_node_count);
862*4882a593Smuzhiyun
device_dma_supported(struct device * dev)863*4882a593Smuzhiyun bool device_dma_supported(struct device *dev)
864*4882a593Smuzhiyun {
865*4882a593Smuzhiyun /* For DT, this is always supported.
866*4882a593Smuzhiyun * For ACPI, this depends on CCA, which
867*4882a593Smuzhiyun * is determined by the acpi_dma_supported().
868*4882a593Smuzhiyun */
869*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_OF) && dev->of_node)
870*4882a593Smuzhiyun return true;
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun return acpi_dma_supported(ACPI_COMPANION(dev));
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_dma_supported);
875*4882a593Smuzhiyun
device_get_dma_attr(struct device * dev)876*4882a593Smuzhiyun enum dev_dma_attr device_get_dma_attr(struct device *dev)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun enum dev_dma_attr attr = DEV_DMA_NOT_SUPPORTED;
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
881*4882a593Smuzhiyun if (of_dma_is_coherent(dev->of_node))
882*4882a593Smuzhiyun attr = DEV_DMA_COHERENT;
883*4882a593Smuzhiyun else
884*4882a593Smuzhiyun attr = DEV_DMA_NON_COHERENT;
885*4882a593Smuzhiyun } else
886*4882a593Smuzhiyun attr = acpi_get_dma_attr(ACPI_COMPANION(dev));
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun return attr;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_get_dma_attr);
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun /**
893*4882a593Smuzhiyun * fwnode_get_phy_mode - Get phy mode for given firmware node
894*4882a593Smuzhiyun * @fwnode: Pointer to the given node
895*4882a593Smuzhiyun *
896*4882a593Smuzhiyun * The function gets phy interface string from property 'phy-mode' or
897*4882a593Smuzhiyun * 'phy-connection-type', and return its index in phy_modes table, or errno in
898*4882a593Smuzhiyun * error case.
899*4882a593Smuzhiyun */
fwnode_get_phy_mode(struct fwnode_handle * fwnode)900*4882a593Smuzhiyun int fwnode_get_phy_mode(struct fwnode_handle *fwnode)
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun const char *pm;
903*4882a593Smuzhiyun int err, i;
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
906*4882a593Smuzhiyun if (err < 0)
907*4882a593Smuzhiyun err = fwnode_property_read_string(fwnode,
908*4882a593Smuzhiyun "phy-connection-type", &pm);
909*4882a593Smuzhiyun if (err < 0)
910*4882a593Smuzhiyun return err;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
913*4882a593Smuzhiyun if (!strcasecmp(pm, phy_modes(i)))
914*4882a593Smuzhiyun return i;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun return -ENODEV;
917*4882a593Smuzhiyun }
918*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun /**
921*4882a593Smuzhiyun * device_get_phy_mode - Get phy mode for given device
922*4882a593Smuzhiyun * @dev: Pointer to the given device
923*4882a593Smuzhiyun *
924*4882a593Smuzhiyun * The function gets phy interface string from property 'phy-mode' or
925*4882a593Smuzhiyun * 'phy-connection-type', and return its index in phy_modes table, or errno in
926*4882a593Smuzhiyun * error case.
927*4882a593Smuzhiyun */
device_get_phy_mode(struct device * dev)928*4882a593Smuzhiyun int device_get_phy_mode(struct device *dev)
929*4882a593Smuzhiyun {
930*4882a593Smuzhiyun return fwnode_get_phy_mode(dev_fwnode(dev));
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_get_phy_mode);
933*4882a593Smuzhiyun
fwnode_get_mac_addr(struct fwnode_handle * fwnode,const char * name,char * addr,int alen)934*4882a593Smuzhiyun static void *fwnode_get_mac_addr(struct fwnode_handle *fwnode,
935*4882a593Smuzhiyun const char *name, char *addr,
936*4882a593Smuzhiyun int alen)
937*4882a593Smuzhiyun {
938*4882a593Smuzhiyun int ret = fwnode_property_read_u8_array(fwnode, name, addr, alen);
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
941*4882a593Smuzhiyun return addr;
942*4882a593Smuzhiyun return NULL;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun
945*4882a593Smuzhiyun /**
946*4882a593Smuzhiyun * fwnode_get_mac_address - Get the MAC from the firmware node
947*4882a593Smuzhiyun * @fwnode: Pointer to the firmware node
948*4882a593Smuzhiyun * @addr: Address of buffer to store the MAC in
949*4882a593Smuzhiyun * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
950*4882a593Smuzhiyun *
951*4882a593Smuzhiyun * Search the firmware node for the best MAC address to use. 'mac-address' is
952*4882a593Smuzhiyun * checked first, because that is supposed to contain to "most recent" MAC
953*4882a593Smuzhiyun * address. If that isn't set, then 'local-mac-address' is checked next,
954*4882a593Smuzhiyun * because that is the default address. If that isn't set, then the obsolete
955*4882a593Smuzhiyun * 'address' is checked, just in case we're using an old device tree.
956*4882a593Smuzhiyun *
957*4882a593Smuzhiyun * Note that the 'address' property is supposed to contain a virtual address of
958*4882a593Smuzhiyun * the register set, but some DTS files have redefined that property to be the
959*4882a593Smuzhiyun * MAC address.
960*4882a593Smuzhiyun *
961*4882a593Smuzhiyun * All-zero MAC addresses are rejected, because those could be properties that
962*4882a593Smuzhiyun * exist in the firmware tables, but were not updated by the firmware. For
963*4882a593Smuzhiyun * example, the DTS could define 'mac-address' and 'local-mac-address', with
964*4882a593Smuzhiyun * zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.
965*4882a593Smuzhiyun * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
966*4882a593Smuzhiyun * exists but is all zeros.
967*4882a593Smuzhiyun */
fwnode_get_mac_address(struct fwnode_handle * fwnode,char * addr,int alen)968*4882a593Smuzhiyun void *fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr, int alen)
969*4882a593Smuzhiyun {
970*4882a593Smuzhiyun char *res;
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun res = fwnode_get_mac_addr(fwnode, "mac-address", addr, alen);
973*4882a593Smuzhiyun if (res)
974*4882a593Smuzhiyun return res;
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun res = fwnode_get_mac_addr(fwnode, "local-mac-address", addr, alen);
977*4882a593Smuzhiyun if (res)
978*4882a593Smuzhiyun return res;
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun return fwnode_get_mac_addr(fwnode, "address", addr, alen);
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun EXPORT_SYMBOL(fwnode_get_mac_address);
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun /**
985*4882a593Smuzhiyun * device_get_mac_address - Get the MAC for a given device
986*4882a593Smuzhiyun * @dev: Pointer to the device
987*4882a593Smuzhiyun * @addr: Address of buffer to store the MAC in
988*4882a593Smuzhiyun * @alen: Length of the buffer pointed to by addr, should be ETH_ALEN
989*4882a593Smuzhiyun */
device_get_mac_address(struct device * dev,char * addr,int alen)990*4882a593Smuzhiyun void *device_get_mac_address(struct device *dev, char *addr, int alen)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun return fwnode_get_mac_address(dev_fwnode(dev), addr, alen);
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun EXPORT_SYMBOL(device_get_mac_address);
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun /**
997*4882a593Smuzhiyun * fwnode_irq_get - Get IRQ directly from a fwnode
998*4882a593Smuzhiyun * @fwnode: Pointer to the firmware node
999*4882a593Smuzhiyun * @index: Zero-based index of the IRQ
1000*4882a593Smuzhiyun *
1001*4882a593Smuzhiyun * Returns Linux IRQ number on success. Other values are determined
1002*4882a593Smuzhiyun * accordingly to acpi_/of_ irq_get() operation.
1003*4882a593Smuzhiyun */
fwnode_irq_get(struct fwnode_handle * fwnode,unsigned int index)1004*4882a593Smuzhiyun int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun struct device_node *of_node = to_of_node(fwnode);
1007*4882a593Smuzhiyun struct resource res;
1008*4882a593Smuzhiyun int ret;
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_OF) && of_node)
1011*4882a593Smuzhiyun return of_irq_get(of_node, index);
1012*4882a593Smuzhiyun
1013*4882a593Smuzhiyun ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res);
1014*4882a593Smuzhiyun if (ret)
1015*4882a593Smuzhiyun return ret;
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun return res.start;
1018*4882a593Smuzhiyun }
1019*4882a593Smuzhiyun EXPORT_SYMBOL(fwnode_irq_get);
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun /**
1022*4882a593Smuzhiyun * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
1023*4882a593Smuzhiyun * @fwnode: Pointer to the parent firmware node
1024*4882a593Smuzhiyun * @prev: Previous endpoint node or %NULL to get the first
1025*4882a593Smuzhiyun *
1026*4882a593Smuzhiyun * Returns an endpoint firmware node pointer or %NULL if no more endpoints
1027*4882a593Smuzhiyun * are available.
1028*4882a593Smuzhiyun */
1029*4882a593Smuzhiyun struct fwnode_handle *
fwnode_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)1030*4882a593Smuzhiyun fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1031*4882a593Smuzhiyun struct fwnode_handle *prev)
1032*4882a593Smuzhiyun {
1033*4882a593Smuzhiyun return fwnode_call_ptr_op(fwnode, graph_get_next_endpoint, prev);
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun /**
1038*4882a593Smuzhiyun * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1039*4882a593Smuzhiyun * @endpoint: Endpoint firmware node of the port
1040*4882a593Smuzhiyun *
1041*4882a593Smuzhiyun * Return: the firmware node of the device the @endpoint belongs to.
1042*4882a593Smuzhiyun */
1043*4882a593Smuzhiyun struct fwnode_handle *
fwnode_graph_get_port_parent(const struct fwnode_handle * endpoint)1044*4882a593Smuzhiyun fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1045*4882a593Smuzhiyun {
1046*4882a593Smuzhiyun struct fwnode_handle *port, *parent;
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun port = fwnode_get_parent(endpoint);
1049*4882a593Smuzhiyun parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun fwnode_handle_put(port);
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun return parent;
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun /**
1058*4882a593Smuzhiyun * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1059*4882a593Smuzhiyun * @fwnode: Endpoint firmware node pointing to the remote endpoint
1060*4882a593Smuzhiyun *
1061*4882a593Smuzhiyun * Extracts firmware node of a remote device the @fwnode points to.
1062*4882a593Smuzhiyun */
1063*4882a593Smuzhiyun struct fwnode_handle *
fwnode_graph_get_remote_port_parent(const struct fwnode_handle * fwnode)1064*4882a593Smuzhiyun fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1065*4882a593Smuzhiyun {
1066*4882a593Smuzhiyun struct fwnode_handle *endpoint, *parent;
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1069*4882a593Smuzhiyun parent = fwnode_graph_get_port_parent(endpoint);
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun fwnode_handle_put(endpoint);
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun return parent;
1074*4882a593Smuzhiyun }
1075*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun /**
1078*4882a593Smuzhiyun * fwnode_graph_get_remote_port - Return fwnode of a remote port
1079*4882a593Smuzhiyun * @fwnode: Endpoint firmware node pointing to the remote endpoint
1080*4882a593Smuzhiyun *
1081*4882a593Smuzhiyun * Extracts firmware node of a remote port the @fwnode points to.
1082*4882a593Smuzhiyun */
1083*4882a593Smuzhiyun struct fwnode_handle *
fwnode_graph_get_remote_port(const struct fwnode_handle * fwnode)1084*4882a593Smuzhiyun fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1085*4882a593Smuzhiyun {
1086*4882a593Smuzhiyun return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun /**
1091*4882a593Smuzhiyun * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1092*4882a593Smuzhiyun * @fwnode: Endpoint firmware node pointing to the remote endpoint
1093*4882a593Smuzhiyun *
1094*4882a593Smuzhiyun * Extracts firmware node of a remote endpoint the @fwnode points to.
1095*4882a593Smuzhiyun */
1096*4882a593Smuzhiyun struct fwnode_handle *
fwnode_graph_get_remote_endpoint(const struct fwnode_handle * fwnode)1097*4882a593Smuzhiyun fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1098*4882a593Smuzhiyun {
1099*4882a593Smuzhiyun return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun /**
1104*4882a593Smuzhiyun * fwnode_graph_get_remote_node - get remote parent node for given port/endpoint
1105*4882a593Smuzhiyun * @fwnode: pointer to parent fwnode_handle containing graph port/endpoint
1106*4882a593Smuzhiyun * @port_id: identifier of the parent port node
1107*4882a593Smuzhiyun * @endpoint_id: identifier of the endpoint node
1108*4882a593Smuzhiyun *
1109*4882a593Smuzhiyun * Return: Remote fwnode handle associated with remote endpoint node linked
1110*4882a593Smuzhiyun * to @node. Use fwnode_node_put() on it when done.
1111*4882a593Smuzhiyun */
1112*4882a593Smuzhiyun struct fwnode_handle *
fwnode_graph_get_remote_node(const struct fwnode_handle * fwnode,u32 port_id,u32 endpoint_id)1113*4882a593Smuzhiyun fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
1114*4882a593Smuzhiyun u32 endpoint_id)
1115*4882a593Smuzhiyun {
1116*4882a593Smuzhiyun struct fwnode_handle *endpoint = NULL;
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun while ((endpoint = fwnode_graph_get_next_endpoint(fwnode, endpoint))) {
1119*4882a593Smuzhiyun struct fwnode_endpoint fwnode_ep;
1120*4882a593Smuzhiyun struct fwnode_handle *remote;
1121*4882a593Smuzhiyun int ret;
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun ret = fwnode_graph_parse_endpoint(endpoint, &fwnode_ep);
1124*4882a593Smuzhiyun if (ret < 0)
1125*4882a593Smuzhiyun continue;
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun if (fwnode_ep.port != port_id || fwnode_ep.id != endpoint_id)
1128*4882a593Smuzhiyun continue;
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun remote = fwnode_graph_get_remote_port_parent(endpoint);
1131*4882a593Smuzhiyun if (!remote)
1132*4882a593Smuzhiyun return NULL;
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun return fwnode_device_is_available(remote) ? remote : NULL;
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun return NULL;
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun /**
1142*4882a593Smuzhiyun * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1143*4882a593Smuzhiyun * @fwnode: parent fwnode_handle containing the graph
1144*4882a593Smuzhiyun * @port: identifier of the port node
1145*4882a593Smuzhiyun * @endpoint: identifier of the endpoint node under the port node
1146*4882a593Smuzhiyun * @flags: fwnode lookup flags
1147*4882a593Smuzhiyun *
1148*4882a593Smuzhiyun * Return the fwnode handle of the local endpoint corresponding the port and
1149*4882a593Smuzhiyun * endpoint IDs or NULL if not found.
1150*4882a593Smuzhiyun *
1151*4882a593Smuzhiyun * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1152*4882a593Smuzhiyun * has not been found, look for the closest endpoint ID greater than the
1153*4882a593Smuzhiyun * specified one and return the endpoint that corresponds to it, if present.
1154*4882a593Smuzhiyun *
1155*4882a593Smuzhiyun * Do not return endpoints that belong to disabled devices, unless
1156*4882a593Smuzhiyun * FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1157*4882a593Smuzhiyun *
1158*4882a593Smuzhiyun * The returned endpoint needs to be released by calling fwnode_handle_put() on
1159*4882a593Smuzhiyun * it when it is not needed any more.
1160*4882a593Smuzhiyun */
1161*4882a593Smuzhiyun struct fwnode_handle *
fwnode_graph_get_endpoint_by_id(const struct fwnode_handle * fwnode,u32 port,u32 endpoint,unsigned long flags)1162*4882a593Smuzhiyun fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1163*4882a593Smuzhiyun u32 port, u32 endpoint, unsigned long flags)
1164*4882a593Smuzhiyun {
1165*4882a593Smuzhiyun struct fwnode_handle *ep = NULL, *best_ep = NULL;
1166*4882a593Smuzhiyun unsigned int best_ep_id = 0;
1167*4882a593Smuzhiyun bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1168*4882a593Smuzhiyun bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun while ((ep = fwnode_graph_get_next_endpoint(fwnode, ep))) {
1171*4882a593Smuzhiyun struct fwnode_endpoint fwnode_ep = { 0 };
1172*4882a593Smuzhiyun int ret;
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun if (enabled_only) {
1175*4882a593Smuzhiyun struct fwnode_handle *dev_node;
1176*4882a593Smuzhiyun bool available;
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun dev_node = fwnode_graph_get_remote_port_parent(ep);
1179*4882a593Smuzhiyun available = fwnode_device_is_available(dev_node);
1180*4882a593Smuzhiyun fwnode_handle_put(dev_node);
1181*4882a593Smuzhiyun if (!available)
1182*4882a593Smuzhiyun continue;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1186*4882a593Smuzhiyun if (ret < 0)
1187*4882a593Smuzhiyun continue;
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun if (fwnode_ep.port != port)
1190*4882a593Smuzhiyun continue;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun if (fwnode_ep.id == endpoint)
1193*4882a593Smuzhiyun return ep;
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun if (!endpoint_next)
1196*4882a593Smuzhiyun continue;
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun /*
1199*4882a593Smuzhiyun * If the endpoint that has just been found is not the first
1200*4882a593Smuzhiyun * matching one and the ID of the one found previously is closer
1201*4882a593Smuzhiyun * to the requested endpoint ID, skip it.
1202*4882a593Smuzhiyun */
1203*4882a593Smuzhiyun if (fwnode_ep.id < endpoint ||
1204*4882a593Smuzhiyun (best_ep && best_ep_id < fwnode_ep.id))
1205*4882a593Smuzhiyun continue;
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun fwnode_handle_put(best_ep);
1208*4882a593Smuzhiyun best_ep = fwnode_handle_get(ep);
1209*4882a593Smuzhiyun best_ep_id = fwnode_ep.id;
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun return best_ep;
1213*4882a593Smuzhiyun }
1214*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun /**
1217*4882a593Smuzhiyun * fwnode_graph_parse_endpoint - parse common endpoint node properties
1218*4882a593Smuzhiyun * @fwnode: pointer to endpoint fwnode_handle
1219*4882a593Smuzhiyun * @endpoint: pointer to the fwnode endpoint data structure
1220*4882a593Smuzhiyun *
1221*4882a593Smuzhiyun * Parse @fwnode representing a graph endpoint node and store the
1222*4882a593Smuzhiyun * information in @endpoint. The caller must hold a reference to
1223*4882a593Smuzhiyun * @fwnode.
1224*4882a593Smuzhiyun */
fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)1225*4882a593Smuzhiyun int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1226*4882a593Smuzhiyun struct fwnode_endpoint *endpoint)
1227*4882a593Smuzhiyun {
1228*4882a593Smuzhiyun memset(endpoint, 0, sizeof(*endpoint));
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1233*4882a593Smuzhiyun
device_get_match_data(struct device * dev)1234*4882a593Smuzhiyun const void *device_get_match_data(struct device *dev)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(device_get_match_data);
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun static void *
fwnode_graph_devcon_match(struct fwnode_handle * fwnode,const char * con_id,void * data,devcon_match_fn_t match)1241*4882a593Smuzhiyun fwnode_graph_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
1242*4882a593Smuzhiyun void *data, devcon_match_fn_t match)
1243*4882a593Smuzhiyun {
1244*4882a593Smuzhiyun struct fwnode_handle *node;
1245*4882a593Smuzhiyun struct fwnode_handle *ep;
1246*4882a593Smuzhiyun void *ret;
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun fwnode_graph_for_each_endpoint(fwnode, ep) {
1249*4882a593Smuzhiyun node = fwnode_graph_get_remote_port_parent(ep);
1250*4882a593Smuzhiyun if (!fwnode_device_is_available(node)) {
1251*4882a593Smuzhiyun fwnode_handle_put(node);
1252*4882a593Smuzhiyun continue;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun ret = match(node, con_id, data);
1256*4882a593Smuzhiyun fwnode_handle_put(node);
1257*4882a593Smuzhiyun if (ret) {
1258*4882a593Smuzhiyun fwnode_handle_put(ep);
1259*4882a593Smuzhiyun return ret;
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun return NULL;
1263*4882a593Smuzhiyun }
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun static void *
fwnode_devcon_match(struct fwnode_handle * fwnode,const char * con_id,void * data,devcon_match_fn_t match)1266*4882a593Smuzhiyun fwnode_devcon_match(struct fwnode_handle *fwnode, const char *con_id,
1267*4882a593Smuzhiyun void *data, devcon_match_fn_t match)
1268*4882a593Smuzhiyun {
1269*4882a593Smuzhiyun struct fwnode_handle *node;
1270*4882a593Smuzhiyun void *ret;
1271*4882a593Smuzhiyun int i;
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun for (i = 0; ; i++) {
1274*4882a593Smuzhiyun node = fwnode_find_reference(fwnode, con_id, i);
1275*4882a593Smuzhiyun if (IS_ERR(node))
1276*4882a593Smuzhiyun break;
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun ret = match(node, NULL, data);
1279*4882a593Smuzhiyun fwnode_handle_put(node);
1280*4882a593Smuzhiyun if (ret)
1281*4882a593Smuzhiyun return ret;
1282*4882a593Smuzhiyun }
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun return NULL;
1285*4882a593Smuzhiyun }
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun /**
1288*4882a593Smuzhiyun * fwnode_connection_find_match - Find connection from a device node
1289*4882a593Smuzhiyun * @fwnode: Device node with the connection
1290*4882a593Smuzhiyun * @con_id: Identifier for the connection
1291*4882a593Smuzhiyun * @data: Data for the match function
1292*4882a593Smuzhiyun * @match: Function to check and convert the connection description
1293*4882a593Smuzhiyun *
1294*4882a593Smuzhiyun * Find a connection with unique identifier @con_id between @fwnode and another
1295*4882a593Smuzhiyun * device node. @match will be used to convert the connection description to
1296*4882a593Smuzhiyun * data the caller is expecting to be returned.
1297*4882a593Smuzhiyun */
fwnode_connection_find_match(struct fwnode_handle * fwnode,const char * con_id,void * data,devcon_match_fn_t match)1298*4882a593Smuzhiyun void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
1299*4882a593Smuzhiyun const char *con_id, void *data,
1300*4882a593Smuzhiyun devcon_match_fn_t match)
1301*4882a593Smuzhiyun {
1302*4882a593Smuzhiyun void *ret;
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun if (!fwnode || !match)
1305*4882a593Smuzhiyun return NULL;
1306*4882a593Smuzhiyun
1307*4882a593Smuzhiyun ret = fwnode_graph_devcon_match(fwnode, con_id, data, match);
1308*4882a593Smuzhiyun if (ret)
1309*4882a593Smuzhiyun return ret;
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun return fwnode_devcon_match(fwnode, con_id, data, match);
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
1314