Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * property.c - Unified device property interface.
0004  *
0005  * Copyright (C) 2014, Intel Corporation
0006  * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
0007  *          Mika Westerberg <mika.westerberg@linux.intel.com>
0008  */
0009 
0010 #include <linux/acpi.h>
0011 #include <linux/export.h>
0012 #include <linux/kernel.h>
0013 #include <linux/of.h>
0014 #include <linux/of_address.h>
0015 #include <linux/of_graph.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/property.h>
0018 #include <linux/phy.h>
0019 
0020 struct fwnode_handle *dev_fwnode(struct device *dev)
0021 {
0022     return IS_ENABLED(CONFIG_OF) && dev->of_node ?
0023         of_fwnode_handle(dev->of_node) : dev->fwnode;
0024 }
0025 EXPORT_SYMBOL_GPL(dev_fwnode);
0026 
0027 /**
0028  * device_property_present - check if a property of a device is present
0029  * @dev: Device whose property is being checked
0030  * @propname: Name of the property
0031  *
0032  * Check if property @propname is present in the device firmware description.
0033  */
0034 bool device_property_present(struct device *dev, const char *propname)
0035 {
0036     return fwnode_property_present(dev_fwnode(dev), propname);
0037 }
0038 EXPORT_SYMBOL_GPL(device_property_present);
0039 
0040 /**
0041  * fwnode_property_present - check if a property of a firmware node is present
0042  * @fwnode: Firmware node whose property to check
0043  * @propname: Name of the property
0044  */
0045 bool fwnode_property_present(const struct fwnode_handle *fwnode,
0046                  const char *propname)
0047 {
0048     bool ret;
0049 
0050     if (IS_ERR_OR_NULL(fwnode))
0051         return false;
0052 
0053     ret = fwnode_call_bool_op(fwnode, property_present, propname);
0054     if (ret)
0055         return ret;
0056 
0057     return fwnode_call_bool_op(fwnode->secondary, property_present, propname);
0058 }
0059 EXPORT_SYMBOL_GPL(fwnode_property_present);
0060 
0061 /**
0062  * device_property_read_u8_array - return a u8 array property of a device
0063  * @dev: Device to get the property of
0064  * @propname: Name of the property
0065  * @val: The values are stored here or %NULL to return the number of values
0066  * @nval: Size of the @val array
0067  *
0068  * Function reads an array of u8 properties with @propname from the device
0069  * firmware description and stores them to @val if found.
0070  *
0071  * It's recommended to call device_property_count_u8() instead of calling
0072  * this function with @val equals %NULL and @nval equals 0.
0073  *
0074  * Return: number of values if @val was %NULL,
0075  *         %0 if the property was found (success),
0076  *     %-EINVAL if given arguments are not valid,
0077  *     %-ENODATA if the property does not have a value,
0078  *     %-EPROTO if the property is not an array of numbers,
0079  *     %-EOVERFLOW if the size of the property is not as expected.
0080  *     %-ENXIO if no suitable firmware interface is present.
0081  */
0082 int device_property_read_u8_array(struct device *dev, const char *propname,
0083                   u8 *val, size_t nval)
0084 {
0085     return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);
0086 }
0087 EXPORT_SYMBOL_GPL(device_property_read_u8_array);
0088 
0089 /**
0090  * device_property_read_u16_array - return a u16 array property of a device
0091  * @dev: Device to get the property of
0092  * @propname: Name of the property
0093  * @val: The values are stored here or %NULL to return the number of values
0094  * @nval: Size of the @val array
0095  *
0096  * Function reads an array of u16 properties with @propname from the device
0097  * firmware description and stores them to @val if found.
0098  *
0099  * It's recommended to call device_property_count_u16() instead of calling
0100  * this function with @val equals %NULL and @nval equals 0.
0101  *
0102  * Return: number of values if @val was %NULL,
0103  *         %0 if the property was found (success),
0104  *     %-EINVAL if given arguments are not valid,
0105  *     %-ENODATA if the property does not have a value,
0106  *     %-EPROTO if the property is not an array of numbers,
0107  *     %-EOVERFLOW if the size of the property is not as expected.
0108  *     %-ENXIO if no suitable firmware interface is present.
0109  */
0110 int device_property_read_u16_array(struct device *dev, const char *propname,
0111                    u16 *val, size_t nval)
0112 {
0113     return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);
0114 }
0115 EXPORT_SYMBOL_GPL(device_property_read_u16_array);
0116 
0117 /**
0118  * device_property_read_u32_array - return a u32 array property of a device
0119  * @dev: Device to get the property of
0120  * @propname: Name of the property
0121  * @val: The values are stored here or %NULL to return the number of values
0122  * @nval: Size of the @val array
0123  *
0124  * Function reads an array of u32 properties with @propname from the device
0125  * firmware description and stores them to @val if found.
0126  *
0127  * It's recommended to call device_property_count_u32() instead of calling
0128  * this function with @val equals %NULL and @nval equals 0.
0129  *
0130  * Return: number of values if @val was %NULL,
0131  *         %0 if the property was found (success),
0132  *     %-EINVAL if given arguments are not valid,
0133  *     %-ENODATA if the property does not have a value,
0134  *     %-EPROTO if the property is not an array of numbers,
0135  *     %-EOVERFLOW if the size of the property is not as expected.
0136  *     %-ENXIO if no suitable firmware interface is present.
0137  */
0138 int device_property_read_u32_array(struct device *dev, const char *propname,
0139                    u32 *val, size_t nval)
0140 {
0141     return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);
0142 }
0143 EXPORT_SYMBOL_GPL(device_property_read_u32_array);
0144 
0145 /**
0146  * device_property_read_u64_array - return a u64 array property of a device
0147  * @dev: Device to get the property of
0148  * @propname: Name of the property
0149  * @val: The values are stored here or %NULL to return the number of values
0150  * @nval: Size of the @val array
0151  *
0152  * Function reads an array of u64 properties with @propname from the device
0153  * firmware description and stores them to @val if found.
0154  *
0155  * It's recommended to call device_property_count_u64() instead of calling
0156  * this function with @val equals %NULL and @nval equals 0.
0157  *
0158  * Return: number of values if @val was %NULL,
0159  *         %0 if the property was found (success),
0160  *     %-EINVAL if given arguments are not valid,
0161  *     %-ENODATA if the property does not have a value,
0162  *     %-EPROTO if the property is not an array of numbers,
0163  *     %-EOVERFLOW if the size of the property is not as expected.
0164  *     %-ENXIO if no suitable firmware interface is present.
0165  */
0166 int device_property_read_u64_array(struct device *dev, const char *propname,
0167                    u64 *val, size_t nval)
0168 {
0169     return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);
0170 }
0171 EXPORT_SYMBOL_GPL(device_property_read_u64_array);
0172 
0173 /**
0174  * device_property_read_string_array - return a string array property of device
0175  * @dev: Device to get the property of
0176  * @propname: Name of the property
0177  * @val: The values are stored here or %NULL to return the number of values
0178  * @nval: Size of the @val array
0179  *
0180  * Function reads an array of string properties with @propname from the device
0181  * firmware description and stores them to @val if found.
0182  *
0183  * It's recommended to call device_property_string_array_count() instead of calling
0184  * this function with @val equals %NULL and @nval equals 0.
0185  *
0186  * Return: number of values read on success if @val is non-NULL,
0187  *     number of values available on success if @val is NULL,
0188  *     %-EINVAL if given arguments are not valid,
0189  *     %-ENODATA if the property does not have a value,
0190  *     %-EPROTO or %-EILSEQ if the property is not an array of strings,
0191  *     %-EOVERFLOW if the size of the property is not as expected.
0192  *     %-ENXIO if no suitable firmware interface is present.
0193  */
0194 int device_property_read_string_array(struct device *dev, const char *propname,
0195                       const char **val, size_t nval)
0196 {
0197     return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);
0198 }
0199 EXPORT_SYMBOL_GPL(device_property_read_string_array);
0200 
0201 /**
0202  * device_property_read_string - return a string property of a device
0203  * @dev: Device to get the property of
0204  * @propname: Name of the property
0205  * @val: The value is stored here
0206  *
0207  * Function reads property @propname from the device firmware description and
0208  * stores the value into @val if found. The value is checked to be a string.
0209  *
0210  * Return: %0 if the property was found (success),
0211  *     %-EINVAL if given arguments are not valid,
0212  *     %-ENODATA if the property does not have a value,
0213  *     %-EPROTO or %-EILSEQ if the property type is not a string.
0214  *     %-ENXIO if no suitable firmware interface is present.
0215  */
0216 int device_property_read_string(struct device *dev, const char *propname,
0217                 const char **val)
0218 {
0219     return fwnode_property_read_string(dev_fwnode(dev), propname, val);
0220 }
0221 EXPORT_SYMBOL_GPL(device_property_read_string);
0222 
0223 /**
0224  * device_property_match_string - find a string in an array and return index
0225  * @dev: Device to get the property of
0226  * @propname: Name of the property holding the array
0227  * @string: String to look for
0228  *
0229  * Find a given string in a string array and if it is found return the
0230  * index back.
0231  *
0232  * Return: %0 if the property was found (success),
0233  *     %-EINVAL if given arguments are not valid,
0234  *     %-ENODATA if the property does not have a value,
0235  *     %-EPROTO if the property is not an array of strings,
0236  *     %-ENXIO if no suitable firmware interface is present.
0237  */
0238 int device_property_match_string(struct device *dev, const char *propname,
0239                  const char *string)
0240 {
0241     return fwnode_property_match_string(dev_fwnode(dev), propname, string);
0242 }
0243 EXPORT_SYMBOL_GPL(device_property_match_string);
0244 
0245 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
0246                       const char *propname,
0247                       unsigned int elem_size, void *val,
0248                       size_t nval)
0249 {
0250     int ret;
0251 
0252     if (IS_ERR_OR_NULL(fwnode))
0253         return -EINVAL;
0254 
0255     ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,
0256                  elem_size, val, nval);
0257     if (ret != -EINVAL)
0258         return ret;
0259 
0260     return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,
0261                   elem_size, val, nval);
0262 }
0263 
0264 /**
0265  * fwnode_property_read_u8_array - return a u8 array property of firmware node
0266  * @fwnode: Firmware node to get the property of
0267  * @propname: Name of the property
0268  * @val: The values are stored here or %NULL to return the number of values
0269  * @nval: Size of the @val array
0270  *
0271  * Read an array of u8 properties with @propname from @fwnode and stores them to
0272  * @val if found.
0273  *
0274  * It's recommended to call fwnode_property_count_u8() instead of calling
0275  * this function with @val equals %NULL and @nval equals 0.
0276  *
0277  * Return: number of values if @val was %NULL,
0278  *         %0 if the property was found (success),
0279  *     %-EINVAL if given arguments are not valid,
0280  *     %-ENODATA if the property does not have a value,
0281  *     %-EPROTO if the property is not an array of numbers,
0282  *     %-EOVERFLOW if the size of the property is not as expected,
0283  *     %-ENXIO if no suitable firmware interface is present.
0284  */
0285 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
0286                   const char *propname, u8 *val, size_t nval)
0287 {
0288     return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),
0289                           val, nval);
0290 }
0291 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);
0292 
0293 /**
0294  * fwnode_property_read_u16_array - return a u16 array property of firmware node
0295  * @fwnode: Firmware node to get the property of
0296  * @propname: Name of the property
0297  * @val: The values are stored here or %NULL to return the number of values
0298  * @nval: Size of the @val array
0299  *
0300  * Read an array of u16 properties with @propname from @fwnode and store them to
0301  * @val if found.
0302  *
0303  * It's recommended to call fwnode_property_count_u16() instead of calling
0304  * this function with @val equals %NULL and @nval equals 0.
0305  *
0306  * Return: number of values if @val was %NULL,
0307  *         %0 if the property was found (success),
0308  *     %-EINVAL if given arguments are not valid,
0309  *     %-ENODATA if the property does not have a value,
0310  *     %-EPROTO if the property is not an array of numbers,
0311  *     %-EOVERFLOW if the size of the property is not as expected,
0312  *     %-ENXIO if no suitable firmware interface is present.
0313  */
0314 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
0315                    const char *propname, u16 *val, size_t nval)
0316 {
0317     return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),
0318                           val, nval);
0319 }
0320 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);
0321 
0322 /**
0323  * fwnode_property_read_u32_array - return a u32 array property of firmware node
0324  * @fwnode: Firmware node to get the property of
0325  * @propname: Name of the property
0326  * @val: The values are stored here or %NULL to return the number of values
0327  * @nval: Size of the @val array
0328  *
0329  * Read an array of u32 properties with @propname from @fwnode store them to
0330  * @val if found.
0331  *
0332  * It's recommended to call fwnode_property_count_u32() instead of calling
0333  * this function with @val equals %NULL and @nval equals 0.
0334  *
0335  * Return: number of values if @val was %NULL,
0336  *         %0 if the property was found (success),
0337  *     %-EINVAL if given arguments are not valid,
0338  *     %-ENODATA if the property does not have a value,
0339  *     %-EPROTO if the property is not an array of numbers,
0340  *     %-EOVERFLOW if the size of the property is not as expected,
0341  *     %-ENXIO if no suitable firmware interface is present.
0342  */
0343 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
0344                    const char *propname, u32 *val, size_t nval)
0345 {
0346     return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),
0347                           val, nval);
0348 }
0349 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);
0350 
0351 /**
0352  * fwnode_property_read_u64_array - return a u64 array property firmware node
0353  * @fwnode: Firmware node to get the property of
0354  * @propname: Name of the property
0355  * @val: The values are stored here or %NULL to return the number of values
0356  * @nval: Size of the @val array
0357  *
0358  * Read an array of u64 properties with @propname from @fwnode and store them to
0359  * @val if found.
0360  *
0361  * It's recommended to call fwnode_property_count_u64() instead of calling
0362  * this function with @val equals %NULL and @nval equals 0.
0363  *
0364  * Return: number of values if @val was %NULL,
0365  *         %0 if the property was found (success),
0366  *     %-EINVAL if given arguments are not valid,
0367  *     %-ENODATA if the property does not have a value,
0368  *     %-EPROTO if the property is not an array of numbers,
0369  *     %-EOVERFLOW if the size of the property is not as expected,
0370  *     %-ENXIO if no suitable firmware interface is present.
0371  */
0372 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
0373                    const char *propname, u64 *val, size_t nval)
0374 {
0375     return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),
0376                           val, nval);
0377 }
0378 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);
0379 
0380 /**
0381  * fwnode_property_read_string_array - return string array property of a node
0382  * @fwnode: Firmware node to get the property of
0383  * @propname: Name of the property
0384  * @val: The values are stored here or %NULL to return the number of values
0385  * @nval: Size of the @val array
0386  *
0387  * Read an string list property @propname from the given firmware node and store
0388  * them to @val if found.
0389  *
0390  * It's recommended to call fwnode_property_string_array_count() instead of calling
0391  * this function with @val equals %NULL and @nval equals 0.
0392  *
0393  * Return: number of values read on success if @val is non-NULL,
0394  *     number of values available on success if @val is NULL,
0395  *     %-EINVAL if given arguments are not valid,
0396  *     %-ENODATA if the property does not have a value,
0397  *     %-EPROTO or %-EILSEQ if the property is not an array of strings,
0398  *     %-EOVERFLOW if the size of the property is not as expected,
0399  *     %-ENXIO if no suitable firmware interface is present.
0400  */
0401 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
0402                       const char *propname, const char **val,
0403                       size_t nval)
0404 {
0405     int ret;
0406 
0407     if (IS_ERR_OR_NULL(fwnode))
0408         return -EINVAL;
0409 
0410     ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,
0411                  val, nval);
0412     if (ret != -EINVAL)
0413         return ret;
0414 
0415     return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,
0416                   val, nval);
0417 }
0418 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);
0419 
0420 /**
0421  * fwnode_property_read_string - return a string property of a firmware node
0422  * @fwnode: Firmware node to get the property of
0423  * @propname: Name of the property
0424  * @val: The value is stored here
0425  *
0426  * Read property @propname from the given firmware node and store the value into
0427  * @val if found.  The value is checked to be a string.
0428  *
0429  * Return: %0 if the property was found (success),
0430  *     %-EINVAL if given arguments are not valid,
0431  *     %-ENODATA if the property does not have a value,
0432  *     %-EPROTO or %-EILSEQ if the property is not a string,
0433  *     %-ENXIO if no suitable firmware interface is present.
0434  */
0435 int fwnode_property_read_string(const struct fwnode_handle *fwnode,
0436                 const char *propname, const char **val)
0437 {
0438     int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);
0439 
0440     return ret < 0 ? ret : 0;
0441 }
0442 EXPORT_SYMBOL_GPL(fwnode_property_read_string);
0443 
0444 /**
0445  * fwnode_property_match_string - find a string in an array and return index
0446  * @fwnode: Firmware node to get the property of
0447  * @propname: Name of the property holding the array
0448  * @string: String to look for
0449  *
0450  * Find a given string in a string array and if it is found return the
0451  * index back.
0452  *
0453  * Return: %0 if the property was found (success),
0454  *     %-EINVAL if given arguments are not valid,
0455  *     %-ENODATA if the property does not have a value,
0456  *     %-EPROTO if the property is not an array of strings,
0457  *     %-ENXIO if no suitable firmware interface is present.
0458  */
0459 int fwnode_property_match_string(const struct fwnode_handle *fwnode,
0460     const char *propname, const char *string)
0461 {
0462     const char **values;
0463     int nval, ret;
0464 
0465     nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
0466     if (nval < 0)
0467         return nval;
0468 
0469     if (nval == 0)
0470         return -ENODATA;
0471 
0472     values = kcalloc(nval, sizeof(*values), GFP_KERNEL);
0473     if (!values)
0474         return -ENOMEM;
0475 
0476     ret = fwnode_property_read_string_array(fwnode, propname, values, nval);
0477     if (ret < 0)
0478         goto out;
0479 
0480     ret = match_string(values, nval, string);
0481     if (ret < 0)
0482         ret = -ENODATA;
0483 out:
0484     kfree(values);
0485     return ret;
0486 }
0487 EXPORT_SYMBOL_GPL(fwnode_property_match_string);
0488 
0489 /**
0490  * fwnode_property_get_reference_args() - Find a reference with arguments
0491  * @fwnode: Firmware node where to look for the reference
0492  * @prop:   The name of the property
0493  * @nargs_prop: The name of the property telling the number of
0494  *      arguments in the referred node. NULL if @nargs is known,
0495  *      otherwise @nargs is ignored. Only relevant on OF.
0496  * @nargs:  Number of arguments. Ignored if @nargs_prop is non-NULL.
0497  * @index:  Index of the reference, from zero onwards.
0498  * @args:   Result structure with reference and integer arguments.
0499  *
0500  * Obtain a reference based on a named property in an fwnode, with
0501  * integer arguments.
0502  *
0503  * Caller is responsible to call fwnode_handle_put() on the returned
0504  * args->fwnode pointer.
0505  *
0506  * Returns: %0 on success
0507  *      %-ENOENT when the index is out of bounds, the index has an empty
0508  *           reference or the property was not found
0509  *      %-EINVAL on parse error
0510  */
0511 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
0512                        const char *prop, const char *nargs_prop,
0513                        unsigned int nargs, unsigned int index,
0514                        struct fwnode_reference_args *args)
0515 {
0516     int ret;
0517 
0518     if (IS_ERR_OR_NULL(fwnode))
0519         return -ENOENT;
0520 
0521     ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,
0522                  nargs, index, args);
0523     if (ret == 0)
0524         return ret;
0525 
0526     if (IS_ERR_OR_NULL(fwnode->secondary))
0527         return ret;
0528 
0529     return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,
0530                   nargs, index, args);
0531 }
0532 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);
0533 
0534 /**
0535  * fwnode_find_reference - Find named reference to a fwnode_handle
0536  * @fwnode: Firmware node where to look for the reference
0537  * @name: The name of the reference
0538  * @index: Index of the reference
0539  *
0540  * @index can be used when the named reference holds a table of references.
0541  *
0542  * Returns pointer to the reference fwnode, or ERR_PTR. Caller is responsible to
0543  * call fwnode_handle_put() on the returned fwnode pointer.
0544  */
0545 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
0546                         const char *name,
0547                         unsigned int index)
0548 {
0549     struct fwnode_reference_args args;
0550     int ret;
0551 
0552     ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,
0553                          &args);
0554     return ret ? ERR_PTR(ret) : args.fwnode;
0555 }
0556 EXPORT_SYMBOL_GPL(fwnode_find_reference);
0557 
0558 /**
0559  * fwnode_get_name - Return the name of a node
0560  * @fwnode: The firmware node
0561  *
0562  * Returns a pointer to the node name.
0563  */
0564 const char *fwnode_get_name(const struct fwnode_handle *fwnode)
0565 {
0566     return fwnode_call_ptr_op(fwnode, get_name);
0567 }
0568 EXPORT_SYMBOL_GPL(fwnode_get_name);
0569 
0570 /**
0571  * fwnode_get_name_prefix - Return the prefix of node for printing purposes
0572  * @fwnode: The firmware node
0573  *
0574  * Returns the prefix of a node, intended to be printed right before the node.
0575  * The prefix works also as a separator between the nodes.
0576  */
0577 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
0578 {
0579     return fwnode_call_ptr_op(fwnode, get_name_prefix);
0580 }
0581 
0582 /**
0583  * fwnode_get_parent - Return parent firwmare node
0584  * @fwnode: Firmware whose parent is retrieved
0585  *
0586  * Return parent firmware node of the given node if possible or %NULL if no
0587  * parent was available.
0588  */
0589 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
0590 {
0591     return fwnode_call_ptr_op(fwnode, get_parent);
0592 }
0593 EXPORT_SYMBOL_GPL(fwnode_get_parent);
0594 
0595 /**
0596  * fwnode_get_next_parent - Iterate to the node's parent
0597  * @fwnode: Firmware whose parent is retrieved
0598  *
0599  * This is like fwnode_get_parent() except that it drops the refcount
0600  * on the passed node, making it suitable for iterating through a
0601  * node's parents.
0602  *
0603  * Returns a node pointer with refcount incremented, use
0604  * fwnode_handle_node() on it when done.
0605  */
0606 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)
0607 {
0608     struct fwnode_handle *parent = fwnode_get_parent(fwnode);
0609 
0610     fwnode_handle_put(fwnode);
0611 
0612     return parent;
0613 }
0614 EXPORT_SYMBOL_GPL(fwnode_get_next_parent);
0615 
0616 /**
0617  * fwnode_get_next_parent_dev - Find device of closest ancestor fwnode
0618  * @fwnode: firmware node
0619  *
0620  * Given a firmware node (@fwnode), this function finds its closest ancestor
0621  * firmware node that has a corresponding struct device and returns that struct
0622  * device.
0623  *
0624  * The caller of this function is expected to call put_device() on the returned
0625  * device when they are done.
0626  */
0627 struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode)
0628 {
0629     struct fwnode_handle *parent;
0630     struct device *dev;
0631 
0632     fwnode_for_each_parent_node(fwnode, parent) {
0633         dev = get_dev_from_fwnode(parent);
0634         if (dev) {
0635             fwnode_handle_put(parent);
0636             return dev;
0637         }
0638     }
0639     return NULL;
0640 }
0641 
0642 /**
0643  * fwnode_count_parents - Return the number of parents a node has
0644  * @fwnode: The node the parents of which are to be counted
0645  *
0646  * Returns the number of parents a node has.
0647  */
0648 unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)
0649 {
0650     struct fwnode_handle *parent;
0651     unsigned int count = 0;
0652 
0653     fwnode_for_each_parent_node(fwnode, parent)
0654         count++;
0655 
0656     return count;
0657 }
0658 EXPORT_SYMBOL_GPL(fwnode_count_parents);
0659 
0660 /**
0661  * fwnode_get_nth_parent - Return an nth parent of a node
0662  * @fwnode: The node the parent of which is requested
0663  * @depth: Distance of the parent from the node
0664  *
0665  * Returns the nth parent of a node. If there is no parent at the requested
0666  * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to
0667  * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.
0668  *
0669  * The caller is responsible for calling fwnode_handle_put() for the returned
0670  * node.
0671  */
0672 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,
0673                         unsigned int depth)
0674 {
0675     struct fwnode_handle *parent;
0676 
0677     if (depth == 0)
0678         return fwnode_handle_get(fwnode);
0679 
0680     fwnode_for_each_parent_node(fwnode, parent) {
0681         if (--depth == 0)
0682             return parent;
0683     }
0684     return NULL;
0685 }
0686 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);
0687 
0688 /**
0689  * fwnode_is_ancestor_of - Test if @ancestor is ancestor of @child
0690  * @ancestor: Firmware which is tested for being an ancestor
0691  * @child: Firmware which is tested for being the child
0692  *
0693  * A node is considered an ancestor of itself too.
0694  *
0695  * Returns true if @ancestor is an ancestor of @child. Otherwise, returns false.
0696  */
0697 bool fwnode_is_ancestor_of(struct fwnode_handle *ancestor, struct fwnode_handle *child)
0698 {
0699     struct fwnode_handle *parent;
0700 
0701     if (IS_ERR_OR_NULL(ancestor))
0702         return false;
0703 
0704     if (child == ancestor)
0705         return true;
0706 
0707     fwnode_for_each_parent_node(child, parent) {
0708         if (parent == ancestor) {
0709             fwnode_handle_put(parent);
0710             return true;
0711         }
0712     }
0713     return false;
0714 }
0715 
0716 /**
0717  * fwnode_get_next_child_node - Return the next child node handle for a node
0718  * @fwnode: Firmware node to find the next child node for.
0719  * @child: Handle to one of the node's child nodes or a %NULL handle.
0720  */
0721 struct fwnode_handle *
0722 fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
0723                struct fwnode_handle *child)
0724 {
0725     return fwnode_call_ptr_op(fwnode, get_next_child_node, child);
0726 }
0727 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
0728 
0729 /**
0730  * fwnode_get_next_available_child_node - Return the next
0731  * available child node handle for a node
0732  * @fwnode: Firmware node to find the next child node for.
0733  * @child: Handle to one of the node's child nodes or a %NULL handle.
0734  */
0735 struct fwnode_handle *
0736 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
0737                      struct fwnode_handle *child)
0738 {
0739     struct fwnode_handle *next_child = child;
0740 
0741     if (IS_ERR_OR_NULL(fwnode))
0742         return NULL;
0743 
0744     do {
0745         next_child = fwnode_get_next_child_node(fwnode, next_child);
0746         if (!next_child)
0747             return NULL;
0748     } while (!fwnode_device_is_available(next_child));
0749 
0750     return next_child;
0751 }
0752 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
0753 
0754 /**
0755  * device_get_next_child_node - Return the next child node handle for a device
0756  * @dev: Device to find the next child node for.
0757  * @child: Handle to one of the device's child nodes or a null handle.
0758  */
0759 struct fwnode_handle *device_get_next_child_node(struct device *dev,
0760                          struct fwnode_handle *child)
0761 {
0762     const struct fwnode_handle *fwnode = dev_fwnode(dev);
0763     struct fwnode_handle *next;
0764 
0765     if (IS_ERR_OR_NULL(fwnode))
0766         return NULL;
0767 
0768     /* Try to find a child in primary fwnode */
0769     next = fwnode_get_next_child_node(fwnode, child);
0770     if (next)
0771         return next;
0772 
0773     /* When no more children in primary, continue with secondary */
0774     return fwnode_get_next_child_node(fwnode->secondary, child);
0775 }
0776 EXPORT_SYMBOL_GPL(device_get_next_child_node);
0777 
0778 /**
0779  * fwnode_get_named_child_node - Return first matching named child node handle
0780  * @fwnode: Firmware node to find the named child node for.
0781  * @childname: String to match child node name against.
0782  */
0783 struct fwnode_handle *
0784 fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
0785                 const char *childname)
0786 {
0787     return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
0788 }
0789 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
0790 
0791 /**
0792  * device_get_named_child_node - Return first matching named child node handle
0793  * @dev: Device to find the named child node for.
0794  * @childname: String to match child node name against.
0795  */
0796 struct fwnode_handle *device_get_named_child_node(struct device *dev,
0797                           const char *childname)
0798 {
0799     return fwnode_get_named_child_node(dev_fwnode(dev), childname);
0800 }
0801 EXPORT_SYMBOL_GPL(device_get_named_child_node);
0802 
0803 /**
0804  * fwnode_handle_get - Obtain a reference to a device node
0805  * @fwnode: Pointer to the device node to obtain the reference to.
0806  *
0807  * Returns the fwnode handle.
0808  */
0809 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
0810 {
0811     if (!fwnode_has_op(fwnode, get))
0812         return fwnode;
0813 
0814     return fwnode_call_ptr_op(fwnode, get);
0815 }
0816 EXPORT_SYMBOL_GPL(fwnode_handle_get);
0817 
0818 /**
0819  * fwnode_handle_put - Drop reference to a device node
0820  * @fwnode: Pointer to the device node to drop the reference to.
0821  *
0822  * This has to be used when terminating device_for_each_child_node() iteration
0823  * with break or return to prevent stale device node references from being left
0824  * behind.
0825  */
0826 void fwnode_handle_put(struct fwnode_handle *fwnode)
0827 {
0828     fwnode_call_void_op(fwnode, put);
0829 }
0830 EXPORT_SYMBOL_GPL(fwnode_handle_put);
0831 
0832 /**
0833  * fwnode_device_is_available - check if a device is available for use
0834  * @fwnode: Pointer to the fwnode of the device.
0835  *
0836  * For fwnode node types that don't implement the .device_is_available()
0837  * operation, this function returns true.
0838  */
0839 bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
0840 {
0841     if (IS_ERR_OR_NULL(fwnode))
0842         return false;
0843 
0844     if (!fwnode_has_op(fwnode, device_is_available))
0845         return true;
0846 
0847     return fwnode_call_bool_op(fwnode, device_is_available);
0848 }
0849 EXPORT_SYMBOL_GPL(fwnode_device_is_available);
0850 
0851 /**
0852  * device_get_child_node_count - return the number of child nodes for device
0853  * @dev: Device to cound the child nodes for
0854  */
0855 unsigned int device_get_child_node_count(struct device *dev)
0856 {
0857     struct fwnode_handle *child;
0858     unsigned int count = 0;
0859 
0860     device_for_each_child_node(dev, child)
0861         count++;
0862 
0863     return count;
0864 }
0865 EXPORT_SYMBOL_GPL(device_get_child_node_count);
0866 
0867 bool device_dma_supported(struct device *dev)
0868 {
0869     return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
0870 }
0871 EXPORT_SYMBOL_GPL(device_dma_supported);
0872 
0873 enum dev_dma_attr device_get_dma_attr(struct device *dev)
0874 {
0875     if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))
0876         return DEV_DMA_NOT_SUPPORTED;
0877 
0878     return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);
0879 }
0880 EXPORT_SYMBOL_GPL(device_get_dma_attr);
0881 
0882 /**
0883  * fwnode_get_phy_mode - Get phy mode for given firmware node
0884  * @fwnode: Pointer to the given node
0885  *
0886  * The function gets phy interface string from property 'phy-mode' or
0887  * 'phy-connection-type', and return its index in phy_modes table, or errno in
0888  * error case.
0889  */
0890 int fwnode_get_phy_mode(struct fwnode_handle *fwnode)
0891 {
0892     const char *pm;
0893     int err, i;
0894 
0895     err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
0896     if (err < 0)
0897         err = fwnode_property_read_string(fwnode,
0898                           "phy-connection-type", &pm);
0899     if (err < 0)
0900         return err;
0901 
0902     for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
0903         if (!strcasecmp(pm, phy_modes(i)))
0904             return i;
0905 
0906     return -ENODEV;
0907 }
0908 EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
0909 
0910 /**
0911  * device_get_phy_mode - Get phy mode for given device
0912  * @dev:    Pointer to the given device
0913  *
0914  * The function gets phy interface string from property 'phy-mode' or
0915  * 'phy-connection-type', and return its index in phy_modes table, or errno in
0916  * error case.
0917  */
0918 int device_get_phy_mode(struct device *dev)
0919 {
0920     return fwnode_get_phy_mode(dev_fwnode(dev));
0921 }
0922 EXPORT_SYMBOL_GPL(device_get_phy_mode);
0923 
0924 /**
0925  * fwnode_iomap - Maps the memory mapped IO for a given fwnode
0926  * @fwnode: Pointer to the firmware node
0927  * @index:  Index of the IO range
0928  *
0929  * Returns a pointer to the mapped memory.
0930  */
0931 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)
0932 {
0933     return fwnode_call_ptr_op(fwnode, iomap, index);
0934 }
0935 EXPORT_SYMBOL(fwnode_iomap);
0936 
0937 /**
0938  * fwnode_irq_get - Get IRQ directly from a fwnode
0939  * @fwnode: Pointer to the firmware node
0940  * @index:  Zero-based index of the IRQ
0941  *
0942  * Returns Linux IRQ number on success. Other values are determined
0943  * accordingly to acpi_/of_ irq_get() operation.
0944  */
0945 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
0946 {
0947     return fwnode_call_int_op(fwnode, irq_get, index);
0948 }
0949 EXPORT_SYMBOL(fwnode_irq_get);
0950 
0951 /**
0952  * fwnode_irq_get_byname - Get IRQ from a fwnode using its name
0953  * @fwnode: Pointer to the firmware node
0954  * @name:   IRQ name
0955  *
0956  * Description:
0957  * Find a match to the string @name in the 'interrupt-names' string array
0958  * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ
0959  * number of the IRQ resource corresponding to the index of the matched
0960  * string.
0961  *
0962  * Return:
0963  * Linux IRQ number on success, or negative errno otherwise.
0964  */
0965 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
0966 {
0967     int index;
0968 
0969     if (!name)
0970         return -EINVAL;
0971 
0972     index = fwnode_property_match_string(fwnode, "interrupt-names",  name);
0973     if (index < 0)
0974         return index;
0975 
0976     return fwnode_irq_get(fwnode, index);
0977 }
0978 EXPORT_SYMBOL(fwnode_irq_get_byname);
0979 
0980 /**
0981  * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
0982  * @fwnode: Pointer to the parent firmware node
0983  * @prev: Previous endpoint node or %NULL to get the first
0984  *
0985  * Returns an endpoint firmware node pointer or %NULL if no more endpoints
0986  * are available.
0987  */
0988 struct fwnode_handle *
0989 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
0990                    struct fwnode_handle *prev)
0991 {
0992     const struct fwnode_handle *parent;
0993     struct fwnode_handle *ep;
0994 
0995     /*
0996      * If this function is in a loop and the previous iteration returned
0997      * an endpoint from fwnode->secondary, then we need to use the secondary
0998      * as parent rather than @fwnode.
0999      */
1000     if (prev)
1001         parent = fwnode_graph_get_port_parent(prev);
1002     else
1003         parent = fwnode;
1004     if (IS_ERR_OR_NULL(parent))
1005         return NULL;
1006 
1007     ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
1008     if (ep)
1009         return ep;
1010 
1011     return fwnode_graph_get_next_endpoint(parent->secondary, NULL);
1012 }
1013 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1014 
1015 /**
1016  * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1017  * @endpoint: Endpoint firmware node of the port
1018  *
1019  * Return: the firmware node of the device the @endpoint belongs to.
1020  */
1021 struct fwnode_handle *
1022 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1023 {
1024     struct fwnode_handle *port, *parent;
1025 
1026     port = fwnode_get_parent(endpoint);
1027     parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1028 
1029     fwnode_handle_put(port);
1030 
1031     return parent;
1032 }
1033 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1034 
1035 /**
1036  * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1037  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1038  *
1039  * Extracts firmware node of a remote device the @fwnode points to.
1040  */
1041 struct fwnode_handle *
1042 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1043 {
1044     struct fwnode_handle *endpoint, *parent;
1045 
1046     endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1047     parent = fwnode_graph_get_port_parent(endpoint);
1048 
1049     fwnode_handle_put(endpoint);
1050 
1051     return parent;
1052 }
1053 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1054 
1055 /**
1056  * fwnode_graph_get_remote_port - Return fwnode of a remote port
1057  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1058  *
1059  * Extracts firmware node of a remote port the @fwnode points to.
1060  */
1061 struct fwnode_handle *
1062 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1063 {
1064     return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1065 }
1066 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1067 
1068 /**
1069  * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1070  * @fwnode: Endpoint firmware node pointing to the remote endpoint
1071  *
1072  * Extracts firmware node of a remote endpoint the @fwnode points to.
1073  */
1074 struct fwnode_handle *
1075 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1076 {
1077     return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1078 }
1079 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1080 
1081 static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
1082 {
1083     struct fwnode_handle *dev_node;
1084     bool available;
1085 
1086     dev_node = fwnode_graph_get_remote_port_parent(ep);
1087     available = fwnode_device_is_available(dev_node);
1088     fwnode_handle_put(dev_node);
1089 
1090     return available;
1091 }
1092 
1093 /**
1094  * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1095  * @fwnode: parent fwnode_handle containing the graph
1096  * @port: identifier of the port node
1097  * @endpoint: identifier of the endpoint node under the port node
1098  * @flags: fwnode lookup flags
1099  *
1100  * Return the fwnode handle of the local endpoint corresponding the port and
1101  * endpoint IDs or NULL if not found.
1102  *
1103  * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1104  * has not been found, look for the closest endpoint ID greater than the
1105  * specified one and return the endpoint that corresponds to it, if present.
1106  *
1107  * Does not return endpoints that belong to disabled devices or endpoints that
1108  * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1109  *
1110  * The returned endpoint needs to be released by calling fwnode_handle_put() on
1111  * it when it is not needed any more.
1112  */
1113 struct fwnode_handle *
1114 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1115                 u32 port, u32 endpoint, unsigned long flags)
1116 {
1117     struct fwnode_handle *ep, *best_ep = NULL;
1118     unsigned int best_ep_id = 0;
1119     bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1120     bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1121 
1122     fwnode_graph_for_each_endpoint(fwnode, ep) {
1123         struct fwnode_endpoint fwnode_ep = { 0 };
1124         int ret;
1125 
1126         if (enabled_only && !fwnode_graph_remote_available(ep))
1127             continue;
1128 
1129         ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1130         if (ret < 0)
1131             continue;
1132 
1133         if (fwnode_ep.port != port)
1134             continue;
1135 
1136         if (fwnode_ep.id == endpoint)
1137             return ep;
1138 
1139         if (!endpoint_next)
1140             continue;
1141 
1142         /*
1143          * If the endpoint that has just been found is not the first
1144          * matching one and the ID of the one found previously is closer
1145          * to the requested endpoint ID, skip it.
1146          */
1147         if (fwnode_ep.id < endpoint ||
1148             (best_ep && best_ep_id < fwnode_ep.id))
1149             continue;
1150 
1151         fwnode_handle_put(best_ep);
1152         best_ep = fwnode_handle_get(ep);
1153         best_ep_id = fwnode_ep.id;
1154     }
1155 
1156     return best_ep;
1157 }
1158 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1159 
1160 /**
1161  * fwnode_graph_get_endpoint_count - Count endpoints on a device node
1162  * @fwnode: The node related to a device
1163  * @flags: fwnode lookup flags
1164  * Count endpoints in a device node.
1165  *
1166  * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
1167  * and endpoints connected to disabled devices are counted.
1168  */
1169 unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
1170                          unsigned long flags)
1171 {
1172     struct fwnode_handle *ep;
1173     unsigned int count = 0;
1174 
1175     fwnode_graph_for_each_endpoint(fwnode, ep) {
1176         if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
1177             fwnode_graph_remote_available(ep))
1178             count++;
1179     }
1180 
1181     return count;
1182 }
1183 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
1184 
1185 /**
1186  * fwnode_graph_parse_endpoint - parse common endpoint node properties
1187  * @fwnode: pointer to endpoint fwnode_handle
1188  * @endpoint: pointer to the fwnode endpoint data structure
1189  *
1190  * Parse @fwnode representing a graph endpoint node and store the
1191  * information in @endpoint. The caller must hold a reference to
1192  * @fwnode.
1193  */
1194 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1195                 struct fwnode_endpoint *endpoint)
1196 {
1197     memset(endpoint, 0, sizeof(*endpoint));
1198 
1199     return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1200 }
1201 EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1202 
1203 const void *device_get_match_data(struct device *dev)
1204 {
1205     return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1206 }
1207 EXPORT_SYMBOL_GPL(device_get_match_data);
1208 
1209 static unsigned int fwnode_graph_devcon_matches(struct fwnode_handle *fwnode,
1210                         const char *con_id, void *data,
1211                         devcon_match_fn_t match,
1212                         void **matches,
1213                         unsigned int matches_len)
1214 {
1215     struct fwnode_handle *node;
1216     struct fwnode_handle *ep;
1217     unsigned int count = 0;
1218     void *ret;
1219 
1220     fwnode_graph_for_each_endpoint(fwnode, ep) {
1221         if (matches && count >= matches_len) {
1222             fwnode_handle_put(ep);
1223             break;
1224         }
1225 
1226         node = fwnode_graph_get_remote_port_parent(ep);
1227         if (!fwnode_device_is_available(node)) {
1228             fwnode_handle_put(node);
1229             continue;
1230         }
1231 
1232         ret = match(node, con_id, data);
1233         fwnode_handle_put(node);
1234         if (ret) {
1235             if (matches)
1236                 matches[count] = ret;
1237             count++;
1238         }
1239     }
1240     return count;
1241 }
1242 
1243 static unsigned int fwnode_devcon_matches(struct fwnode_handle *fwnode,
1244                       const char *con_id, void *data,
1245                       devcon_match_fn_t match,
1246                       void **matches,
1247                       unsigned int matches_len)
1248 {
1249     struct fwnode_handle *node;
1250     unsigned int count = 0;
1251     unsigned int i;
1252     void *ret;
1253 
1254     for (i = 0; ; i++) {
1255         if (matches && count >= matches_len)
1256             break;
1257 
1258         node = fwnode_find_reference(fwnode, con_id, i);
1259         if (IS_ERR(node))
1260             break;
1261 
1262         ret = match(node, NULL, data);
1263         fwnode_handle_put(node);
1264         if (ret) {
1265             if (matches)
1266                 matches[count] = ret;
1267             count++;
1268         }
1269     }
1270 
1271     return count;
1272 }
1273 
1274 /**
1275  * fwnode_connection_find_match - Find connection from a device node
1276  * @fwnode: Device node with the connection
1277  * @con_id: Identifier for the connection
1278  * @data: Data for the match function
1279  * @match: Function to check and convert the connection description
1280  *
1281  * Find a connection with unique identifier @con_id between @fwnode and another
1282  * device node. @match will be used to convert the connection description to
1283  * data the caller is expecting to be returned.
1284  */
1285 void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
1286                    const char *con_id, void *data,
1287                    devcon_match_fn_t match)
1288 {
1289     unsigned int count;
1290     void *ret;
1291 
1292     if (!fwnode || !match)
1293         return NULL;
1294 
1295     count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1296     if (count)
1297         return ret;
1298 
1299     count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1300     return count ? ret : NULL;
1301 }
1302 EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
1303 
1304 /**
1305  * fwnode_connection_find_matches - Find connections from a device node
1306  * @fwnode: Device node with the connection
1307  * @con_id: Identifier for the connection
1308  * @data: Data for the match function
1309  * @match: Function to check and convert the connection description
1310  * @matches: (Optional) array of pointers to fill with matches
1311  * @matches_len: Length of @matches
1312  *
1313  * Find up to @matches_len connections with unique identifier @con_id between
1314  * @fwnode and other device nodes. @match will be used to convert the
1315  * connection description to data the caller is expecting to be returned
1316  * through the @matches array.
1317  * If @matches is NULL @matches_len is ignored and the total number of resolved
1318  * matches is returned.
1319  *
1320  * Return: Number of matches resolved, or negative errno.
1321  */
1322 int fwnode_connection_find_matches(struct fwnode_handle *fwnode,
1323                    const char *con_id, void *data,
1324                    devcon_match_fn_t match,
1325                    void **matches, unsigned int matches_len)
1326 {
1327     unsigned int count_graph;
1328     unsigned int count_ref;
1329 
1330     if (!fwnode || !match)
1331         return -EINVAL;
1332 
1333     count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,
1334                           matches, matches_len);
1335 
1336     if (matches) {
1337         matches += count_graph;
1338         matches_len -= count_graph;
1339     }
1340 
1341     count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,
1342                       matches, matches_len);
1343 
1344     return count_graph + count_ref;
1345 }
1346 EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);