// SPDX-License-Identifier: GPL-2.01/*2* property.c - Unified device property interface.3*4* Copyright (C) 2014, Intel Corporation5* Authors: Rafael J. Wysocki <[email protected]>6* Mika Westerberg <[email protected]>7*/89#include <linux/device.h>10#include <linux/err.h>11#include <linux/export.h>12#include <linux/kconfig.h>13#include <linux/of.h>14#include <linux/property.h>15#include <linux/phy.h>16#include <linux/slab.h>17#include <linux/string.h>18#include <linux/types.h>1920struct fwnode_handle *__dev_fwnode(struct device *dev)21{22return IS_ENABLED(CONFIG_OF) && dev->of_node ?23of_fwnode_handle(dev->of_node) : dev->fwnode;24}25EXPORT_SYMBOL_GPL(__dev_fwnode);2627const struct fwnode_handle *__dev_fwnode_const(const struct device *dev)28{29return IS_ENABLED(CONFIG_OF) && dev->of_node ?30of_fwnode_handle(dev->of_node) : dev->fwnode;31}32EXPORT_SYMBOL_GPL(__dev_fwnode_const);3334/**35* device_property_present - check if a property of a device is present36* @dev: Device whose property is being checked37* @propname: Name of the property38*39* Check if property @propname is present in the device firmware description.40*41* Return: true if property @propname is present. Otherwise, returns false.42*/43bool device_property_present(const struct device *dev, const char *propname)44{45return fwnode_property_present(dev_fwnode(dev), propname);46}47EXPORT_SYMBOL_GPL(device_property_present);4849/**50* fwnode_property_present - check if a property of a firmware node is present51* @fwnode: Firmware node whose property to check52* @propname: Name of the property53*54* Return: true if property @propname is present. Otherwise, returns false.55*/56bool fwnode_property_present(const struct fwnode_handle *fwnode,57const char *propname)58{59bool ret;6061if (IS_ERR_OR_NULL(fwnode))62return false;6364ret = fwnode_call_bool_op(fwnode, property_present, propname);65if (ret)66return ret;6768return fwnode_call_bool_op(fwnode->secondary, property_present, propname);69}70EXPORT_SYMBOL_GPL(fwnode_property_present);7172/**73* device_property_read_bool - Return the value for a boolean property of a device74* @dev: Device whose property is being checked75* @propname: Name of the property76*77* Return if property @propname is true or false in the device firmware description.78*79* Return: true if property @propname is present. Otherwise, returns false.80*/81bool device_property_read_bool(const struct device *dev, const char *propname)82{83return fwnode_property_read_bool(dev_fwnode(dev), propname);84}85EXPORT_SYMBOL_GPL(device_property_read_bool);8687/**88* fwnode_property_read_bool - Return the value for a boolean property of a firmware node89* @fwnode: Firmware node whose property to check90* @propname: Name of the property91*92* Return if property @propname is true or false in the firmware description.93*/94bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,95const char *propname)96{97bool ret;9899if (IS_ERR_OR_NULL(fwnode))100return false;101102ret = fwnode_call_bool_op(fwnode, property_read_bool, propname);103if (ret)104return ret;105106return fwnode_call_bool_op(fwnode->secondary, property_read_bool, propname);107}108EXPORT_SYMBOL_GPL(fwnode_property_read_bool);109110/**111* device_property_read_u8_array - return a u8 array property of a device112* @dev: Device to get the property of113* @propname: Name of the property114* @val: The values are stored here or %NULL to return the number of values115* @nval: Size of the @val array116*117* Function reads an array of u8 properties with @propname from the device118* firmware description and stores them to @val if found.119*120* It's recommended to call device_property_count_u8() instead of calling121* this function with @val equals %NULL and @nval equals 0.122*123* Return: number of values if @val was %NULL,124* %0 if the property was found (success),125* %-EINVAL if given arguments are not valid,126* %-ENODATA if the property does not have a value,127* %-EPROTO if the property is not an array of numbers,128* %-EOVERFLOW if the size of the property is not as expected.129* %-ENXIO if no suitable firmware interface is present.130*/131int device_property_read_u8_array(const struct device *dev, const char *propname,132u8 *val, size_t nval)133{134return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval);135}136EXPORT_SYMBOL_GPL(device_property_read_u8_array);137138/**139* device_property_read_u16_array - return a u16 array property of a device140* @dev: Device to get the property of141* @propname: Name of the property142* @val: The values are stored here or %NULL to return the number of values143* @nval: Size of the @val array144*145* Function reads an array of u16 properties with @propname from the device146* firmware description and stores them to @val if found.147*148* It's recommended to call device_property_count_u16() instead of calling149* this function with @val equals %NULL and @nval equals 0.150*151* Return: number of values if @val was %NULL,152* %0 if the property was found (success),153* %-EINVAL if given arguments are not valid,154* %-ENODATA if the property does not have a value,155* %-EPROTO if the property is not an array of numbers,156* %-EOVERFLOW if the size of the property is not as expected.157* %-ENXIO if no suitable firmware interface is present.158*/159int device_property_read_u16_array(const struct device *dev, const char *propname,160u16 *val, size_t nval)161{162return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval);163}164EXPORT_SYMBOL_GPL(device_property_read_u16_array);165166/**167* device_property_read_u32_array - return a u32 array property of a device168* @dev: Device to get the property of169* @propname: Name of the property170* @val: The values are stored here or %NULL to return the number of values171* @nval: Size of the @val array172*173* Function reads an array of u32 properties with @propname from the device174* firmware description and stores them to @val if found.175*176* It's recommended to call device_property_count_u32() instead of calling177* this function with @val equals %NULL and @nval equals 0.178*179* Return: number of values if @val was %NULL,180* %0 if the property was found (success),181* %-EINVAL if given arguments are not valid,182* %-ENODATA if the property does not have a value,183* %-EPROTO if the property is not an array of numbers,184* %-EOVERFLOW if the size of the property is not as expected.185* %-ENXIO if no suitable firmware interface is present.186*/187int device_property_read_u32_array(const struct device *dev, const char *propname,188u32 *val, size_t nval)189{190return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval);191}192EXPORT_SYMBOL_GPL(device_property_read_u32_array);193194/**195* device_property_read_u64_array - return a u64 array property of a device196* @dev: Device to get the property of197* @propname: Name of the property198* @val: The values are stored here or %NULL to return the number of values199* @nval: Size of the @val array200*201* Function reads an array of u64 properties with @propname from the device202* firmware description and stores them to @val if found.203*204* It's recommended to call device_property_count_u64() instead of calling205* this function with @val equals %NULL and @nval equals 0.206*207* Return: number of values if @val was %NULL,208* %0 if the property was found (success),209* %-EINVAL if given arguments are not valid,210* %-ENODATA if the property does not have a value,211* %-EPROTO if the property is not an array of numbers,212* %-EOVERFLOW if the size of the property is not as expected.213* %-ENXIO if no suitable firmware interface is present.214*/215int device_property_read_u64_array(const struct device *dev, const char *propname,216u64 *val, size_t nval)217{218return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval);219}220EXPORT_SYMBOL_GPL(device_property_read_u64_array);221222/**223* device_property_read_string_array - return a string array property of device224* @dev: Device to get the property of225* @propname: Name of the property226* @val: The values are stored here or %NULL to return the number of values227* @nval: Size of the @val array228*229* Function reads an array of string properties with @propname from the device230* firmware description and stores them to @val if found.231*232* It's recommended to call device_property_string_array_count() instead of calling233* this function with @val equals %NULL and @nval equals 0.234*235* Return: number of values read on success if @val is non-NULL,236* number of values available on success if @val is NULL,237* %-EINVAL if given arguments are not valid,238* %-ENODATA if the property does not have a value,239* %-EPROTO or %-EILSEQ if the property is not an array of strings,240* %-EOVERFLOW if the size of the property is not as expected.241* %-ENXIO if no suitable firmware interface is present.242*/243int device_property_read_string_array(const struct device *dev, const char *propname,244const char **val, size_t nval)245{246return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval);247}248EXPORT_SYMBOL_GPL(device_property_read_string_array);249250/**251* device_property_read_string - return a string property of a device252* @dev: Device to get the property of253* @propname: Name of the property254* @val: The value is stored here255*256* Function reads property @propname from the device firmware description and257* stores the value into @val if found. The value is checked to be a string.258*259* Return: %0 if the property was found (success),260* %-EINVAL if given arguments are not valid,261* %-ENODATA if the property does not have a value,262* %-EPROTO or %-EILSEQ if the property type is not a string.263* %-ENXIO if no suitable firmware interface is present.264*/265int device_property_read_string(const struct device *dev, const char *propname,266const char **val)267{268return fwnode_property_read_string(dev_fwnode(dev), propname, val);269}270EXPORT_SYMBOL_GPL(device_property_read_string);271272/**273* device_property_match_string - find a string in an array and return index274* @dev: Device to get the property of275* @propname: Name of the property holding the array276* @string: String to look for277*278* Find a given string in a string array and if it is found return the279* index back.280*281* Return: index, starting from %0, if the property was found (success),282* %-EINVAL if given arguments are not valid,283* %-ENODATA if the property does not have a value,284* %-EPROTO if the property is not an array of strings,285* %-ENXIO if no suitable firmware interface is present.286*/287int device_property_match_string(const struct device *dev, const char *propname,288const char *string)289{290return fwnode_property_match_string(dev_fwnode(dev), propname, string);291}292EXPORT_SYMBOL_GPL(device_property_match_string);293294static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,295const char *propname,296unsigned int elem_size, void *val,297size_t nval)298{299int ret;300301if (IS_ERR_OR_NULL(fwnode))302return -EINVAL;303304ret = fwnode_call_int_op(fwnode, property_read_int_array, propname,305elem_size, val, nval);306if (ret != -EINVAL)307return ret;308309return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname,310elem_size, val, nval);311}312313/**314* fwnode_property_read_u8_array - return a u8 array property of firmware node315* @fwnode: Firmware node to get the property of316* @propname: Name of the property317* @val: The values are stored here or %NULL to return the number of values318* @nval: Size of the @val array319*320* Read an array of u8 properties with @propname from @fwnode and stores them to321* @val if found.322*323* It's recommended to call fwnode_property_count_u8() instead of calling324* this function with @val equals %NULL and @nval equals 0.325*326* Return: number of values if @val was %NULL,327* %0 if the property was found (success),328* %-EINVAL if given arguments are not valid,329* %-ENODATA if the property does not have a value,330* %-EPROTO if the property is not an array of numbers,331* %-EOVERFLOW if the size of the property is not as expected,332* %-ENXIO if no suitable firmware interface is present.333*/334int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,335const char *propname, u8 *val, size_t nval)336{337return fwnode_property_read_int_array(fwnode, propname, sizeof(u8),338val, nval);339}340EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array);341342/**343* fwnode_property_read_u16_array - return a u16 array property of firmware node344* @fwnode: Firmware node to get the property of345* @propname: Name of the property346* @val: The values are stored here or %NULL to return the number of values347* @nval: Size of the @val array348*349* Read an array of u16 properties with @propname from @fwnode and store them to350* @val if found.351*352* It's recommended to call fwnode_property_count_u16() instead of calling353* this function with @val equals %NULL and @nval equals 0.354*355* Return: number of values if @val was %NULL,356* %0 if the property was found (success),357* %-EINVAL if given arguments are not valid,358* %-ENODATA if the property does not have a value,359* %-EPROTO if the property is not an array of numbers,360* %-EOVERFLOW if the size of the property is not as expected,361* %-ENXIO if no suitable firmware interface is present.362*/363int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,364const char *propname, u16 *val, size_t nval)365{366return fwnode_property_read_int_array(fwnode, propname, sizeof(u16),367val, nval);368}369EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array);370371/**372* fwnode_property_read_u32_array - return a u32 array property of firmware node373* @fwnode: Firmware node to get the property of374* @propname: Name of the property375* @val: The values are stored here or %NULL to return the number of values376* @nval: Size of the @val array377*378* Read an array of u32 properties with @propname from @fwnode store them to379* @val if found.380*381* It's recommended to call fwnode_property_count_u32() instead of calling382* this function with @val equals %NULL and @nval equals 0.383*384* Return: number of values if @val was %NULL,385* %0 if the property was found (success),386* %-EINVAL if given arguments are not valid,387* %-ENODATA if the property does not have a value,388* %-EPROTO if the property is not an array of numbers,389* %-EOVERFLOW if the size of the property is not as expected,390* %-ENXIO if no suitable firmware interface is present.391*/392int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,393const char *propname, u32 *val, size_t nval)394{395return fwnode_property_read_int_array(fwnode, propname, sizeof(u32),396val, nval);397}398EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array);399400/**401* fwnode_property_read_u64_array - return a u64 array property firmware node402* @fwnode: Firmware node to get the property of403* @propname: Name of the property404* @val: The values are stored here or %NULL to return the number of values405* @nval: Size of the @val array406*407* Read an array of u64 properties with @propname from @fwnode and store them to408* @val if found.409*410* It's recommended to call fwnode_property_count_u64() instead of calling411* this function with @val equals %NULL and @nval equals 0.412*413* Return: number of values if @val was %NULL,414* %0 if the property was found (success),415* %-EINVAL if given arguments are not valid,416* %-ENODATA if the property does not have a value,417* %-EPROTO if the property is not an array of numbers,418* %-EOVERFLOW if the size of the property is not as expected,419* %-ENXIO if no suitable firmware interface is present.420*/421int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,422const char *propname, u64 *val, size_t nval)423{424return fwnode_property_read_int_array(fwnode, propname, sizeof(u64),425val, nval);426}427EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array);428429/**430* fwnode_property_read_string_array - return string array property of a node431* @fwnode: Firmware node to get the property of432* @propname: Name of the property433* @val: The values are stored here or %NULL to return the number of values434* @nval: Size of the @val array435*436* Read an string list property @propname from the given firmware node and store437* them to @val if found.438*439* It's recommended to call fwnode_property_string_array_count() instead of calling440* this function with @val equals %NULL and @nval equals 0.441*442* Return: number of values read on success if @val is non-NULL,443* number of values available on success if @val is NULL,444* %-EINVAL if given arguments are not valid,445* %-ENODATA if the property does not have a value,446* %-EPROTO or %-EILSEQ if the property is not an array of strings,447* %-EOVERFLOW if the size of the property is not as expected,448* %-ENXIO if no suitable firmware interface is present.449*/450int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,451const char *propname, const char **val,452size_t nval)453{454int ret;455456if (IS_ERR_OR_NULL(fwnode))457return -EINVAL;458459ret = fwnode_call_int_op(fwnode, property_read_string_array, propname,460val, nval);461if (ret != -EINVAL)462return ret;463464return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname,465val, nval);466}467EXPORT_SYMBOL_GPL(fwnode_property_read_string_array);468469/**470* fwnode_property_read_string - return a string property of a firmware node471* @fwnode: Firmware node to get the property of472* @propname: Name of the property473* @val: The value is stored here474*475* Read property @propname from the given firmware node and store the value into476* @val if found. The value is checked to be a string.477*478* Return: %0 if the property was found (success),479* %-EINVAL if given arguments are not valid,480* %-ENODATA if the property does not have a value,481* %-EPROTO or %-EILSEQ if the property is not a string,482* %-ENXIO if no suitable firmware interface is present.483*/484int fwnode_property_read_string(const struct fwnode_handle *fwnode,485const char *propname, const char **val)486{487int ret = fwnode_property_read_string_array(fwnode, propname, val, 1);488489return ret < 0 ? ret : 0;490}491EXPORT_SYMBOL_GPL(fwnode_property_read_string);492493/**494* fwnode_property_match_string - find a string in an array and return index495* @fwnode: Firmware node to get the property of496* @propname: Name of the property holding the array497* @string: String to look for498*499* Find a given string in a string array and if it is found return the500* index back.501*502* Return: index, starting from %0, if the property was found (success),503* %-EINVAL if given arguments are not valid,504* %-ENODATA if the property does not have a value,505* %-EPROTO if the property is not an array of strings,506* %-ENXIO if no suitable firmware interface is present.507*/508int fwnode_property_match_string(const struct fwnode_handle *fwnode,509const char *propname, const char *string)510{511const char **values;512int nval, ret;513514nval = fwnode_property_string_array_count(fwnode, propname);515if (nval < 0)516return nval;517518if (nval == 0)519return -ENODATA;520521values = kcalloc(nval, sizeof(*values), GFP_KERNEL);522if (!values)523return -ENOMEM;524525ret = fwnode_property_read_string_array(fwnode, propname, values, nval);526if (ret < 0)527goto out_free;528529ret = match_string(values, nval, string);530if (ret < 0)531ret = -ENODATA;532533out_free:534kfree(values);535return ret;536}537EXPORT_SYMBOL_GPL(fwnode_property_match_string);538539/**540* fwnode_property_match_property_string - find a property string value in an array and return index541* @fwnode: Firmware node to get the property of542* @propname: Name of the property holding the string value543* @array: String array to search in544* @n: Size of the @array545*546* Find a property string value in a given @array and if it is found return547* the index back.548*549* Return: index, starting from %0, if the string value was found in the @array (success),550* %-ENOENT when the string value was not found in the @array,551* %-EINVAL if given arguments are not valid,552* %-ENODATA if the property does not have a value,553* %-EPROTO or %-EILSEQ if the property is not a string,554* %-ENXIO if no suitable firmware interface is present.555*/556int fwnode_property_match_property_string(const struct fwnode_handle *fwnode,557const char *propname, const char * const *array, size_t n)558{559const char *string;560int ret;561562ret = fwnode_property_read_string(fwnode, propname, &string);563if (ret)564return ret;565566ret = match_string(array, n, string);567if (ret < 0)568ret = -ENOENT;569570return ret;571}572EXPORT_SYMBOL_GPL(fwnode_property_match_property_string);573574/**575* fwnode_property_get_reference_args() - Find a reference with arguments576* @fwnode: Firmware node where to look for the reference577* @prop: The name of the property578* @nargs_prop: The name of the property telling the number of579* arguments in the referred node. NULL if @nargs is known,580* otherwise @nargs is ignored. Only relevant on OF.581* @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL.582* @index: Index of the reference, from zero onwards.583* @args: Result structure with reference and integer arguments.584* May be NULL.585*586* Obtain a reference based on a named property in an fwnode, with587* integer arguments.588*589* The caller is responsible for calling fwnode_handle_put() on the returned590* @args->fwnode pointer.591*592* Return: %0 on success593* %-ENOENT when the index is out of bounds, the index has an empty594* reference or the property was not found595* %-EINVAL on parse error596*/597int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,598const char *prop, const char *nargs_prop,599unsigned int nargs, unsigned int index,600struct fwnode_reference_args *args)601{602int ret;603604if (IS_ERR_OR_NULL(fwnode))605return -ENOENT;606607ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop,608nargs, index, args);609if (ret == 0)610return ret;611612if (IS_ERR_OR_NULL(fwnode->secondary))613return ret;614615return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop,616nargs, index, args);617}618EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args);619620/**621* fwnode_find_reference - Find named reference to a fwnode_handle622* @fwnode: Firmware node where to look for the reference623* @name: The name of the reference624* @index: Index of the reference625*626* @index can be used when the named reference holds a table of references.627*628* The caller is responsible for calling fwnode_handle_put() on the returned629* fwnode pointer.630*631* Return: a pointer to the reference fwnode, when found. Otherwise,632* returns an error pointer.633*/634struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,635const char *name,636unsigned int index)637{638struct fwnode_reference_args args;639int ret;640641ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index,642&args);643return ret ? ERR_PTR(ret) : args.fwnode;644}645EXPORT_SYMBOL_GPL(fwnode_find_reference);646647/**648* fwnode_get_name - Return the name of a node649* @fwnode: The firmware node650*651* Return: a pointer to the node name, or %NULL.652*/653const char *fwnode_get_name(const struct fwnode_handle *fwnode)654{655return fwnode_call_ptr_op(fwnode, get_name);656}657EXPORT_SYMBOL_GPL(fwnode_get_name);658659/**660* fwnode_get_name_prefix - Return the prefix of node for printing purposes661* @fwnode: The firmware node662*663* Return: the prefix of a node, intended to be printed right before the node.664* The prefix works also as a separator between the nodes.665*/666const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)667{668return fwnode_call_ptr_op(fwnode, get_name_prefix);669}670671/**672* fwnode_name_eq - Return true if node name is equal673* @fwnode: The firmware node674* @name: The name to which to compare the node name675*676* Compare the name provided as an argument to the name of the node, stopping677* the comparison at either NUL or '@' character, whichever comes first. This678* function is generally used for comparing node names while ignoring the679* possible unit address of the node.680*681* Return: true if the node name matches with the name provided in the @name682* argument, false otherwise.683*/684bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)685{686const char *node_name;687ptrdiff_t len;688689node_name = fwnode_get_name(fwnode);690if (!node_name)691return false;692693len = strchrnul(node_name, '@') - node_name;694695return str_has_prefix(node_name, name) == len;696}697EXPORT_SYMBOL_GPL(fwnode_name_eq);698699/**700* fwnode_get_parent - Return parent firwmare node701* @fwnode: Firmware whose parent is retrieved702*703* The caller is responsible for calling fwnode_handle_put() on the returned704* fwnode pointer.705*706* Return: parent firmware node of the given node if possible or %NULL if no707* parent was available.708*/709struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)710{711return fwnode_call_ptr_op(fwnode, get_parent);712}713EXPORT_SYMBOL_GPL(fwnode_get_parent);714715/**716* fwnode_get_next_parent - Iterate to the node's parent717* @fwnode: Firmware whose parent is retrieved718*719* This is like fwnode_get_parent() except that it drops the refcount720* on the passed node, making it suitable for iterating through a721* node's parents.722*723* The caller is responsible for calling fwnode_handle_put() on the returned724* fwnode pointer. Note that this function also puts a reference to @fwnode725* unconditionally.726*727* Return: parent firmware node of the given node if possible or %NULL if no728* parent was available.729*/730struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode)731{732struct fwnode_handle *parent = fwnode_get_parent(fwnode);733734fwnode_handle_put(fwnode);735736return parent;737}738EXPORT_SYMBOL_GPL(fwnode_get_next_parent);739740/**741* fwnode_count_parents - Return the number of parents a node has742* @fwnode: The node the parents of which are to be counted743*744* Return: the number of parents a node has.745*/746unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode)747{748struct fwnode_handle *parent;749unsigned int count = 0;750751fwnode_for_each_parent_node(fwnode, parent)752count++;753754return count;755}756EXPORT_SYMBOL_GPL(fwnode_count_parents);757758/**759* fwnode_get_nth_parent - Return an nth parent of a node760* @fwnode: The node the parent of which is requested761* @depth: Distance of the parent from the node762*763* The caller is responsible for calling fwnode_handle_put() on the returned764* fwnode pointer.765*766* Return: the nth parent of a node. If there is no parent at the requested767* @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to768* fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on.769*/770struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode,771unsigned int depth)772{773struct fwnode_handle *parent;774775if (depth == 0)776return fwnode_handle_get(fwnode);777778fwnode_for_each_parent_node(fwnode, parent) {779if (--depth == 0)780return parent;781}782return NULL;783}784EXPORT_SYMBOL_GPL(fwnode_get_nth_parent);785786/**787* fwnode_get_next_child_node - Return the next child node handle for a node788* @fwnode: Firmware node to find the next child node for.789* @child: Handle to one of the node's child nodes or a %NULL handle.790*791* The caller is responsible for calling fwnode_handle_put() on the returned792* fwnode pointer. Note that this function also puts a reference to @child793* unconditionally.794*/795struct fwnode_handle *796fwnode_get_next_child_node(const struct fwnode_handle *fwnode,797struct fwnode_handle *child)798{799return fwnode_call_ptr_op(fwnode, get_next_child_node, child);800}801EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);802803/**804* fwnode_get_next_available_child_node - Return the next available child node handle for a node805* @fwnode: Firmware node to find the next child node for.806* @child: Handle to one of the node's child nodes or a %NULL handle.807*808* The caller is responsible for calling fwnode_handle_put() on the returned809* fwnode pointer. Note that this function also puts a reference to @child810* unconditionally.811*/812struct fwnode_handle *813fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,814struct fwnode_handle *child)815{816struct fwnode_handle *next_child = child;817818if (IS_ERR_OR_NULL(fwnode))819return NULL;820821do {822next_child = fwnode_get_next_child_node(fwnode, next_child);823if (!next_child)824return NULL;825} while (!fwnode_device_is_available(next_child));826827return next_child;828}829EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);830831/**832* device_get_next_child_node - Return the next child node handle for a device833* @dev: Device to find the next child node for.834* @child: Handle to one of the device's child nodes or a %NULL handle.835*836* The caller is responsible for calling fwnode_handle_put() on the returned837* fwnode pointer. Note that this function also puts a reference to @child838* unconditionally.839*/840struct fwnode_handle *device_get_next_child_node(const struct device *dev,841struct fwnode_handle *child)842{843const struct fwnode_handle *fwnode = dev_fwnode(dev);844struct fwnode_handle *next;845846if (IS_ERR_OR_NULL(fwnode))847return NULL;848849/* Try to find a child in primary fwnode */850next = fwnode_get_next_child_node(fwnode, child);851if (next)852return next;853854/* When no more children in primary, continue with secondary */855return fwnode_get_next_child_node(fwnode->secondary, child);856}857EXPORT_SYMBOL_GPL(device_get_next_child_node);858859/**860* fwnode_get_named_child_node - Return first matching named child node handle861* @fwnode: Firmware node to find the named child node for.862* @childname: String to match child node name against.863*864* The caller is responsible for calling fwnode_handle_put() on the returned865* fwnode pointer.866*/867struct fwnode_handle *868fwnode_get_named_child_node(const struct fwnode_handle *fwnode,869const char *childname)870{871return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);872}873EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);874875/**876* device_get_named_child_node - Return first matching named child node handle877* @dev: Device to find the named child node for.878* @childname: String to match child node name against.879*880* The caller is responsible for calling fwnode_handle_put() on the returned881* fwnode pointer.882*/883struct fwnode_handle *device_get_named_child_node(const struct device *dev,884const char *childname)885{886return fwnode_get_named_child_node(dev_fwnode(dev), childname);887}888EXPORT_SYMBOL_GPL(device_get_named_child_node);889890/**891* fwnode_handle_get - Obtain a reference to a device node892* @fwnode: Pointer to the device node to obtain the reference to.893*894* The caller is responsible for calling fwnode_handle_put() on the returned895* fwnode pointer.896*897* Return: the fwnode handle.898*/899struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)900{901if (!fwnode_has_op(fwnode, get))902return fwnode;903904return fwnode_call_ptr_op(fwnode, get);905}906EXPORT_SYMBOL_GPL(fwnode_handle_get);907908/**909* fwnode_device_is_available - check if a device is available for use910* @fwnode: Pointer to the fwnode of the device.911*912* Return: true if device is available for use. Otherwise, returns false.913*914* For fwnode node types that don't implement the .device_is_available()915* operation, this function returns true.916*/917bool fwnode_device_is_available(const struct fwnode_handle *fwnode)918{919if (IS_ERR_OR_NULL(fwnode))920return false;921922if (!fwnode_has_op(fwnode, device_is_available))923return true;924925return fwnode_call_bool_op(fwnode, device_is_available);926}927EXPORT_SYMBOL_GPL(fwnode_device_is_available);928929/**930* fwnode_get_child_node_count - return the number of child nodes for a given firmware node931* @fwnode: Pointer to the parent firmware node932*933* Return: the number of child nodes for a given firmware node.934*/935unsigned int fwnode_get_child_node_count(const struct fwnode_handle *fwnode)936{937struct fwnode_handle *child;938unsigned int count = 0;939940fwnode_for_each_child_node(fwnode, child)941count++;942943return count;944}945EXPORT_SYMBOL_GPL(fwnode_get_child_node_count);946947/**948* fwnode_get_named_child_node_count - number of child nodes with given name949* @fwnode: Node which child nodes are counted.950* @name: String to match child node name against.951*952* Scan child nodes and count all the nodes with a specific name. Potential953* 'number' -ending after the 'at sign' for scanned names is ignored.954* E.g.::955* fwnode_get_named_child_node_count(fwnode, "channel");956* would match all the nodes::957* channel { }, channel@0 {}, channel@0xabba {}...958*959* Return: the number of child nodes with a matching name for a given device.960*/961unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,962const char *name)963{964struct fwnode_handle *child;965unsigned int count = 0;966967fwnode_for_each_named_child_node(fwnode, child, name)968count++;969970return count;971}972EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count);973974bool device_dma_supported(const struct device *dev)975{976return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);977}978EXPORT_SYMBOL_GPL(device_dma_supported);979980enum dev_dma_attr device_get_dma_attr(const struct device *dev)981{982if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))983return DEV_DMA_NOT_SUPPORTED;984985return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);986}987EXPORT_SYMBOL_GPL(device_get_dma_attr);988989/**990* fwnode_get_phy_mode - Get phy mode for given firmware node991* @fwnode: Pointer to the given node992*993* The function gets phy interface string from property 'phy-mode' or994* 'phy-connection-type', and return its index in phy_modes table, or errno in995* error case.996*/997int fwnode_get_phy_mode(const struct fwnode_handle *fwnode)998{999const char *pm;1000int err, i;10011002err = fwnode_property_read_string(fwnode, "phy-mode", &pm);1003if (err < 0)1004err = fwnode_property_read_string(fwnode,1005"phy-connection-type", &pm);1006if (err < 0)1007return err;10081009for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)1010if (!strcasecmp(pm, phy_modes(i)))1011return i;10121013return -ENODEV;1014}1015EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);10161017/**1018* device_get_phy_mode - Get phy mode for given device1019* @dev: Pointer to the given device1020*1021* The function gets phy interface string from property 'phy-mode' or1022* 'phy-connection-type', and return its index in phy_modes table, or errno in1023* error case.1024*/1025int device_get_phy_mode(struct device *dev)1026{1027return fwnode_get_phy_mode(dev_fwnode(dev));1028}1029EXPORT_SYMBOL_GPL(device_get_phy_mode);10301031/**1032* fwnode_iomap - Maps the memory mapped IO for a given fwnode1033* @fwnode: Pointer to the firmware node1034* @index: Index of the IO range1035*1036* Return: a pointer to the mapped memory.1037*/1038void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)1039{1040return fwnode_call_ptr_op(fwnode, iomap, index);1041}1042EXPORT_SYMBOL(fwnode_iomap);10431044/**1045* fwnode_irq_get - Get IRQ directly from a fwnode1046* @fwnode: Pointer to the firmware node1047* @index: Zero-based index of the IRQ1048*1049* Return: Linux IRQ number on success. Negative errno on failure.1050*/1051int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)1052{1053int ret;10541055ret = fwnode_call_int_op(fwnode, irq_get, index);1056/* We treat mapping errors as invalid case */1057if (ret == 0)1058return -EINVAL;10591060return ret;1061}1062EXPORT_SYMBOL(fwnode_irq_get);10631064/**1065* fwnode_irq_get_byname - Get IRQ from a fwnode using its name1066* @fwnode: Pointer to the firmware node1067* @name: IRQ name1068*1069* Description:1070* Find a match to the string @name in the 'interrupt-names' string array1071* in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ1072* number of the IRQ resource corresponding to the index of the matched1073* string.1074*1075* Return: Linux IRQ number on success, or negative errno otherwise.1076*/1077int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)1078{1079int index;10801081if (!name)1082return -EINVAL;10831084index = fwnode_property_match_string(fwnode, "interrupt-names", name);1085if (index < 0)1086return index;10871088return fwnode_irq_get(fwnode, index);1089}1090EXPORT_SYMBOL(fwnode_irq_get_byname);10911092/**1093* fwnode_graph_get_next_endpoint - Get next endpoint firmware node1094* @fwnode: Pointer to the parent firmware node1095* @prev: Previous endpoint node or %NULL to get the first1096*1097* The caller is responsible for calling fwnode_handle_put() on the returned1098* fwnode pointer. Note that this function also puts a reference to @prev1099* unconditionally.1100*1101* Return: an endpoint firmware node pointer or %NULL if no more endpoints1102* are available.1103*/1104struct fwnode_handle *1105fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,1106struct fwnode_handle *prev)1107{1108struct fwnode_handle *ep, *port_parent = NULL;1109const struct fwnode_handle *parent;11101111/*1112* If this function is in a loop and the previous iteration returned1113* an endpoint from fwnode->secondary, then we need to use the secondary1114* as parent rather than @fwnode.1115*/1116if (prev) {1117port_parent = fwnode_graph_get_port_parent(prev);1118parent = port_parent;1119} else {1120parent = fwnode;1121}1122if (IS_ERR_OR_NULL(parent))1123return NULL;11241125ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);1126if (ep)1127goto out_put_port_parent;11281129ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);11301131out_put_port_parent:1132fwnode_handle_put(port_parent);1133return ep;1134}1135EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);11361137/**1138* fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint1139* @endpoint: Endpoint firmware node of the port1140*1141* The caller is responsible for calling fwnode_handle_put() on the returned1142* fwnode pointer.1143*1144* Return: the firmware node of the device the @endpoint belongs to.1145*/1146struct fwnode_handle *1147fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)1148{1149struct fwnode_handle *port, *parent;11501151port = fwnode_get_parent(endpoint);1152parent = fwnode_call_ptr_op(port, graph_get_port_parent);11531154fwnode_handle_put(port);11551156return parent;1157}1158EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);11591160/**1161* fwnode_graph_get_remote_port_parent - Return fwnode of a remote device1162* @fwnode: Endpoint firmware node pointing to the remote endpoint1163*1164* Extracts firmware node of a remote device the @fwnode points to.1165*1166* The caller is responsible for calling fwnode_handle_put() on the returned1167* fwnode pointer.1168*/1169struct fwnode_handle *1170fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)1171{1172struct fwnode_handle *endpoint, *parent;11731174endpoint = fwnode_graph_get_remote_endpoint(fwnode);1175parent = fwnode_graph_get_port_parent(endpoint);11761177fwnode_handle_put(endpoint);11781179return parent;1180}1181EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);11821183/**1184* fwnode_graph_get_remote_port - Return fwnode of a remote port1185* @fwnode: Endpoint firmware node pointing to the remote endpoint1186*1187* Extracts firmware node of a remote port the @fwnode points to.1188*1189* The caller is responsible for calling fwnode_handle_put() on the returned1190* fwnode pointer.1191*/1192struct fwnode_handle *1193fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)1194{1195return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));1196}1197EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);11981199/**1200* fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint1201* @fwnode: Endpoint firmware node pointing to the remote endpoint1202*1203* Extracts firmware node of a remote endpoint the @fwnode points to.1204*1205* The caller is responsible for calling fwnode_handle_put() on the returned1206* fwnode pointer.1207*/1208struct fwnode_handle *1209fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)1210{1211return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);1212}1213EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);12141215static bool fwnode_graph_remote_available(struct fwnode_handle *ep)1216{1217struct fwnode_handle *dev_node;1218bool available;12191220dev_node = fwnode_graph_get_remote_port_parent(ep);1221available = fwnode_device_is_available(dev_node);1222fwnode_handle_put(dev_node);12231224return available;1225}12261227/**1228* fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers1229* @fwnode: parent fwnode_handle containing the graph1230* @port: identifier of the port node1231* @endpoint: identifier of the endpoint node under the port node1232* @flags: fwnode lookup flags1233*1234* The caller is responsible for calling fwnode_handle_put() on the returned1235* fwnode pointer.1236*1237* Return: the fwnode handle of the local endpoint corresponding the port and1238* endpoint IDs or %NULL if not found.1239*1240* If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint1241* has not been found, look for the closest endpoint ID greater than the1242* specified one and return the endpoint that corresponds to it, if present.1243*1244* Does not return endpoints that belong to disabled devices or endpoints that1245* are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.1246*/1247struct fwnode_handle *1248fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,1249u32 port, u32 endpoint, unsigned long flags)1250{1251struct fwnode_handle *ep, *best_ep = NULL;1252unsigned int best_ep_id = 0;1253bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;1254bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);12551256fwnode_graph_for_each_endpoint(fwnode, ep) {1257struct fwnode_endpoint fwnode_ep = { 0 };1258int ret;12591260if (enabled_only && !fwnode_graph_remote_available(ep))1261continue;12621263ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);1264if (ret < 0)1265continue;12661267if (fwnode_ep.port != port)1268continue;12691270if (fwnode_ep.id == endpoint)1271return ep;12721273if (!endpoint_next)1274continue;12751276/*1277* If the endpoint that has just been found is not the first1278* matching one and the ID of the one found previously is closer1279* to the requested endpoint ID, skip it.1280*/1281if (fwnode_ep.id < endpoint ||1282(best_ep && best_ep_id < fwnode_ep.id))1283continue;12841285fwnode_handle_put(best_ep);1286best_ep = fwnode_handle_get(ep);1287best_ep_id = fwnode_ep.id;1288}12891290return best_ep;1291}1292EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);12931294/**1295* fwnode_graph_get_endpoint_count - Count endpoints on a device node1296* @fwnode: The node related to a device1297* @flags: fwnode lookup flags1298* Count endpoints in a device node.1299*1300* If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints1301* and endpoints connected to disabled devices are counted.1302*/1303unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,1304unsigned long flags)1305{1306struct fwnode_handle *ep;1307unsigned int count = 0;13081309fwnode_graph_for_each_endpoint(fwnode, ep) {1310if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||1311fwnode_graph_remote_available(ep))1312count++;1313}13141315return count;1316}1317EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);13181319/**1320* fwnode_graph_parse_endpoint - parse common endpoint node properties1321* @fwnode: pointer to endpoint fwnode_handle1322* @endpoint: pointer to the fwnode endpoint data structure1323*1324* Parse @fwnode representing a graph endpoint node and store the1325* information in @endpoint. The caller must hold a reference to1326* @fwnode.1327*/1328int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,1329struct fwnode_endpoint *endpoint)1330{1331memset(endpoint, 0, sizeof(*endpoint));13321333return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);1334}1335EXPORT_SYMBOL(fwnode_graph_parse_endpoint);13361337const void *device_get_match_data(const struct device *dev)1338{1339return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);1340}1341EXPORT_SYMBOL_GPL(device_get_match_data);13421343static unsigned int fwnode_graph_devcon_matches(const struct fwnode_handle *fwnode,1344const char *con_id, void *data,1345devcon_match_fn_t match,1346void **matches,1347unsigned int matches_len)1348{1349struct fwnode_handle *node;1350struct fwnode_handle *ep;1351unsigned int count = 0;1352void *ret;13531354fwnode_graph_for_each_endpoint(fwnode, ep) {1355if (matches && count >= matches_len) {1356fwnode_handle_put(ep);1357break;1358}13591360node = fwnode_graph_get_remote_port_parent(ep);1361if (!fwnode_device_is_available(node)) {1362fwnode_handle_put(node);1363continue;1364}13651366ret = match(node, con_id, data);1367fwnode_handle_put(node);1368if (ret) {1369if (matches)1370matches[count] = ret;1371count++;1372}1373}1374return count;1375}13761377static unsigned int fwnode_devcon_matches(const struct fwnode_handle *fwnode,1378const char *con_id, void *data,1379devcon_match_fn_t match,1380void **matches,1381unsigned int matches_len)1382{1383struct fwnode_handle *node;1384unsigned int count = 0;1385unsigned int i;1386void *ret;13871388for (i = 0; ; i++) {1389if (matches && count >= matches_len)1390break;13911392node = fwnode_find_reference(fwnode, con_id, i);1393if (IS_ERR(node))1394break;13951396ret = match(node, NULL, data);1397fwnode_handle_put(node);1398if (ret) {1399if (matches)1400matches[count] = ret;1401count++;1402}1403}14041405return count;1406}14071408/**1409* fwnode_connection_find_match - Find connection from a device node1410* @fwnode: Device node with the connection1411* @con_id: Identifier for the connection1412* @data: Data for the match function1413* @match: Function to check and convert the connection description1414*1415* Find a connection with unique identifier @con_id between @fwnode and another1416* device node. @match will be used to convert the connection description to1417* data the caller is expecting to be returned.1418*/1419void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,1420const char *con_id, void *data,1421devcon_match_fn_t match)1422{1423unsigned int count;1424void *ret;14251426if (!fwnode || !match)1427return NULL;14281429count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);1430if (count)1431return ret;14321433count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);1434return count ? ret : NULL;1435}1436EXPORT_SYMBOL_GPL(fwnode_connection_find_match);14371438/**1439* fwnode_connection_find_matches - Find connections from a device node1440* @fwnode: Device node with the connection1441* @con_id: Identifier for the connection1442* @data: Data for the match function1443* @match: Function to check and convert the connection description1444* @matches: (Optional) array of pointers to fill with matches1445* @matches_len: Length of @matches1446*1447* Find up to @matches_len connections with unique identifier @con_id between1448* @fwnode and other device nodes. @match will be used to convert the1449* connection description to data the caller is expecting to be returned1450* through the @matches array.1451*1452* If @matches is %NULL @matches_len is ignored and the total number of resolved1453* matches is returned.1454*1455* Return: Number of matches resolved, or negative errno.1456*/1457int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,1458const char *con_id, void *data,1459devcon_match_fn_t match,1460void **matches, unsigned int matches_len)1461{1462unsigned int count_graph;1463unsigned int count_ref;14641465if (!fwnode || !match)1466return -EINVAL;14671468count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,1469matches, matches_len);14701471if (matches) {1472matches += count_graph;1473matches_len -= count_graph;1474}14751476count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,1477matches, matches_len);14781479return count_graph + count_ref;1480}1481EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);148214831484