xref: /openbmc/linux/drivers/of/property.c (revision 6f72b445)
1af6074fcSRob Herring // SPDX-License-Identifier: GPL-2.0+
21df09bc6SSakari Ailus /*
31df09bc6SSakari Ailus  * drivers/of/property.c - Procedures for accessing and interpreting
41df09bc6SSakari Ailus  *			   Devicetree properties and graphs.
51df09bc6SSakari Ailus  *
61df09bc6SSakari Ailus  * Initially created by copying procedures from drivers/of/base.c. This
71df09bc6SSakari Ailus  * file contains the OF property as well as the OF graph interface
81df09bc6SSakari Ailus  * functions.
91df09bc6SSakari Ailus  *
101df09bc6SSakari Ailus  * Paul Mackerras	August 1996.
111df09bc6SSakari Ailus  * Copyright (C) 1996-2005 Paul Mackerras.
121df09bc6SSakari Ailus  *
131df09bc6SSakari Ailus  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
141df09bc6SSakari Ailus  *    {engebret|bergner}@us.ibm.com
151df09bc6SSakari Ailus  *
161df09bc6SSakari Ailus  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
171df09bc6SSakari Ailus  *
181df09bc6SSakari Ailus  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
191df09bc6SSakari Ailus  *  Grant Likely.
201df09bc6SSakari Ailus  */
211df09bc6SSakari Ailus 
221df09bc6SSakari Ailus #define pr_fmt(fmt)	"OF: " fmt
231df09bc6SSakari Ailus 
241df09bc6SSakari Ailus #include <linux/of.h>
258c756a0aSSakari Ailus #include <linux/of_address.h>
261df09bc6SSakari Ailus #include <linux/of_device.h>
271df09bc6SSakari Ailus #include <linux/of_graph.h>
284104ca77SSaravana Kannan #include <linux/of_irq.h>
291df09bc6SSakari Ailus #include <linux/string.h>
30a3e1d1a7SSaravana Kannan #include <linux/moduleparam.h>
311df09bc6SSakari Ailus 
321df09bc6SSakari Ailus #include "of_private.h"
331df09bc6SSakari Ailus 
341df09bc6SSakari Ailus /**
354ec0a44bSDmitry Osipenko  * of_graph_is_present() - check graph's presence
364ec0a44bSDmitry Osipenko  * @node: pointer to device_node containing graph port
374ec0a44bSDmitry Osipenko  *
384ec0a44bSDmitry Osipenko  * Return: True if @node has a port or ports (with a port) sub-node,
394ec0a44bSDmitry Osipenko  * false otherwise.
404ec0a44bSDmitry Osipenko  */
of_graph_is_present(const struct device_node * node)414ec0a44bSDmitry Osipenko bool of_graph_is_present(const struct device_node *node)
424ec0a44bSDmitry Osipenko {
434ec0a44bSDmitry Osipenko 	struct device_node *ports, *port;
444ec0a44bSDmitry Osipenko 
454ec0a44bSDmitry Osipenko 	ports = of_get_child_by_name(node, "ports");
464ec0a44bSDmitry Osipenko 	if (ports)
474ec0a44bSDmitry Osipenko 		node = ports;
484ec0a44bSDmitry Osipenko 
494ec0a44bSDmitry Osipenko 	port = of_get_child_by_name(node, "port");
504ec0a44bSDmitry Osipenko 	of_node_put(ports);
514ec0a44bSDmitry Osipenko 	of_node_put(port);
524ec0a44bSDmitry Osipenko 
534ec0a44bSDmitry Osipenko 	return !!port;
544ec0a44bSDmitry Osipenko }
554ec0a44bSDmitry Osipenko EXPORT_SYMBOL(of_graph_is_present);
564ec0a44bSDmitry Osipenko 
574ec0a44bSDmitry Osipenko /**
581df09bc6SSakari Ailus  * of_property_count_elems_of_size - Count the number of elements in a property
591df09bc6SSakari Ailus  *
601df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
611df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
621df09bc6SSakari Ailus  * @elem_size:	size of the individual element
631df09bc6SSakari Ailus  *
641df09bc6SSakari Ailus  * Search for a property in a device node and count the number of elements of
658c8239c2SRob Herring  * size elem_size in it.
668c8239c2SRob Herring  *
678c8239c2SRob Herring  * Return: The number of elements on sucess, -EINVAL if the property does not
688c8239c2SRob Herring  * exist or its length does not match a multiple of elem_size and -ENODATA if
698c8239c2SRob Herring  * the property does not have a value.
701df09bc6SSakari Ailus  */
of_property_count_elems_of_size(const struct device_node * np,const char * propname,int elem_size)711df09bc6SSakari Ailus int of_property_count_elems_of_size(const struct device_node *np,
721df09bc6SSakari Ailus 				const char *propname, int elem_size)
731df09bc6SSakari Ailus {
741df09bc6SSakari Ailus 	struct property *prop = of_find_property(np, propname, NULL);
751df09bc6SSakari Ailus 
761df09bc6SSakari Ailus 	if (!prop)
771df09bc6SSakari Ailus 		return -EINVAL;
781df09bc6SSakari Ailus 	if (!prop->value)
791df09bc6SSakari Ailus 		return -ENODATA;
801df09bc6SSakari Ailus 
811df09bc6SSakari Ailus 	if (prop->length % elem_size != 0) {
820d638a07SRob Herring 		pr_err("size of %s in node %pOF is not a multiple of %d\n",
830d638a07SRob Herring 		       propname, np, elem_size);
841df09bc6SSakari Ailus 		return -EINVAL;
851df09bc6SSakari Ailus 	}
861df09bc6SSakari Ailus 
871df09bc6SSakari Ailus 	return prop->length / elem_size;
881df09bc6SSakari Ailus }
891df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
901df09bc6SSakari Ailus 
911df09bc6SSakari Ailus /**
921df09bc6SSakari Ailus  * of_find_property_value_of_size
931df09bc6SSakari Ailus  *
941df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
951df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
961df09bc6SSakari Ailus  * @min:	minimum allowed length of property value
971df09bc6SSakari Ailus  * @max:	maximum allowed length of property value (0 means unlimited)
981df09bc6SSakari Ailus  * @len:	if !=NULL, actual length is written to here
991df09bc6SSakari Ailus  *
1001df09bc6SSakari Ailus  * Search for a property in a device node and valid the requested size.
1018c8239c2SRob Herring  *
1028c8239c2SRob Herring  * Return: The property value on success, -EINVAL if the property does not
1031df09bc6SSakari Ailus  * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1041df09bc6SSakari Ailus  * property data is too small or too large.
1051df09bc6SSakari Ailus  *
1061df09bc6SSakari Ailus  */
of_find_property_value_of_size(const struct device_node * np,const char * propname,u32 min,u32 max,size_t * len)1071df09bc6SSakari Ailus static void *of_find_property_value_of_size(const struct device_node *np,
1081df09bc6SSakari Ailus 			const char *propname, u32 min, u32 max, size_t *len)
1091df09bc6SSakari Ailus {
1101df09bc6SSakari Ailus 	struct property *prop = of_find_property(np, propname, NULL);
1111df09bc6SSakari Ailus 
1121df09bc6SSakari Ailus 	if (!prop)
1131df09bc6SSakari Ailus 		return ERR_PTR(-EINVAL);
1141df09bc6SSakari Ailus 	if (!prop->value)
1151df09bc6SSakari Ailus 		return ERR_PTR(-ENODATA);
1161df09bc6SSakari Ailus 	if (prop->length < min)
1171df09bc6SSakari Ailus 		return ERR_PTR(-EOVERFLOW);
1181df09bc6SSakari Ailus 	if (max && prop->length > max)
1191df09bc6SSakari Ailus 		return ERR_PTR(-EOVERFLOW);
1201df09bc6SSakari Ailus 
1211df09bc6SSakari Ailus 	if (len)
1221df09bc6SSakari Ailus 		*len = prop->length;
1231df09bc6SSakari Ailus 
1241df09bc6SSakari Ailus 	return prop->value;
1251df09bc6SSakari Ailus }
1261df09bc6SSakari Ailus 
1271df09bc6SSakari Ailus /**
1281df09bc6SSakari Ailus  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1291df09bc6SSakari Ailus  *
1301df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
1311df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
1321df09bc6SSakari Ailus  * @index:	index of the u32 in the list of values
1331df09bc6SSakari Ailus  * @out_value:	pointer to return value, modified only if no error.
1341df09bc6SSakari Ailus  *
1351df09bc6SSakari Ailus  * Search for a property in a device node and read nth 32-bit value from
1368c8239c2SRob Herring  * it.
1378c8239c2SRob Herring  *
1388c8239c2SRob Herring  * Return: 0 on success, -EINVAL if the property does not exist,
1391df09bc6SSakari Ailus  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1401df09bc6SSakari Ailus  * property data isn't large enough.
1411df09bc6SSakari Ailus  *
1421df09bc6SSakari Ailus  * The out_value is modified only if a valid u32 value can be decoded.
1431df09bc6SSakari Ailus  */
of_property_read_u32_index(const struct device_node * np,const char * propname,u32 index,u32 * out_value)1441df09bc6SSakari Ailus int of_property_read_u32_index(const struct device_node *np,
1451df09bc6SSakari Ailus 				       const char *propname,
1461df09bc6SSakari Ailus 				       u32 index, u32 *out_value)
1471df09bc6SSakari Ailus {
1481df09bc6SSakari Ailus 	const u32 *val = of_find_property_value_of_size(np, propname,
1491df09bc6SSakari Ailus 					((index + 1) * sizeof(*out_value)),
1501df09bc6SSakari Ailus 					0,
1511df09bc6SSakari Ailus 					NULL);
1521df09bc6SSakari Ailus 
1531df09bc6SSakari Ailus 	if (IS_ERR(val))
1541df09bc6SSakari Ailus 		return PTR_ERR(val);
1551df09bc6SSakari Ailus 
1561df09bc6SSakari Ailus 	*out_value = be32_to_cpup(((__be32 *)val) + index);
1571df09bc6SSakari Ailus 	return 0;
1581df09bc6SSakari Ailus }
1591df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1601df09bc6SSakari Ailus 
1611df09bc6SSakari Ailus /**
1621df09bc6SSakari Ailus  * of_property_read_u64_index - Find and read a u64 from a multi-value property.
1631df09bc6SSakari Ailus  *
1641df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
1651df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
1661df09bc6SSakari Ailus  * @index:	index of the u64 in the list of values
1671df09bc6SSakari Ailus  * @out_value:	pointer to return value, modified only if no error.
1681df09bc6SSakari Ailus  *
1691df09bc6SSakari Ailus  * Search for a property in a device node and read nth 64-bit value from
1708c8239c2SRob Herring  * it.
1718c8239c2SRob Herring  *
1728c8239c2SRob Herring  * Return: 0 on success, -EINVAL if the property does not exist,
1731df09bc6SSakari Ailus  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1741df09bc6SSakari Ailus  * property data isn't large enough.
1751df09bc6SSakari Ailus  *
1761df09bc6SSakari Ailus  * The out_value is modified only if a valid u64 value can be decoded.
1771df09bc6SSakari Ailus  */
of_property_read_u64_index(const struct device_node * np,const char * propname,u32 index,u64 * out_value)1781df09bc6SSakari Ailus int of_property_read_u64_index(const struct device_node *np,
1791df09bc6SSakari Ailus 				       const char *propname,
1801df09bc6SSakari Ailus 				       u32 index, u64 *out_value)
1811df09bc6SSakari Ailus {
1821df09bc6SSakari Ailus 	const u64 *val = of_find_property_value_of_size(np, propname,
1831df09bc6SSakari Ailus 					((index + 1) * sizeof(*out_value)),
1841df09bc6SSakari Ailus 					0, NULL);
1851df09bc6SSakari Ailus 
1861df09bc6SSakari Ailus 	if (IS_ERR(val))
1871df09bc6SSakari Ailus 		return PTR_ERR(val);
1881df09bc6SSakari Ailus 
1891df09bc6SSakari Ailus 	*out_value = be64_to_cpup(((__be64 *)val) + index);
1901df09bc6SSakari Ailus 	return 0;
1911df09bc6SSakari Ailus }
1921df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_u64_index);
1931df09bc6SSakari Ailus 
1941df09bc6SSakari Ailus /**
1951df09bc6SSakari Ailus  * of_property_read_variable_u8_array - Find and read an array of u8 from a
1961df09bc6SSakari Ailus  * property, with bounds on the minimum and maximum array size.
1971df09bc6SSakari Ailus  *
1981df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
1991df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
2007f3fefeeSMatti Vaittinen  * @out_values:	pointer to found values.
2011df09bc6SSakari Ailus  * @sz_min:	minimum number of array elements to read
2021df09bc6SSakari Ailus  * @sz_max:	maximum number of array elements to read, if zero there is no
2031df09bc6SSakari Ailus  *		upper limit on the number of elements in the dts entry but only
2041df09bc6SSakari Ailus  *		sz_min will be read.
2051df09bc6SSakari Ailus  *
2061df09bc6SSakari Ailus  * Search for a property in a device node and read 8-bit value(s) from
2078c8239c2SRob Herring  * it.
2081df09bc6SSakari Ailus  *
2091df09bc6SSakari Ailus  * dts entry of array should be like:
2108c8239c2SRob Herring  *  ``property = /bits/ 8 <0x50 0x60 0x70>;``
2118c8239c2SRob Herring  *
2128c8239c2SRob Herring  * Return: The number of elements read on success, -EINVAL if the property
2138c8239c2SRob Herring  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
2148c8239c2SRob Herring  * if the property data is smaller than sz_min or longer than sz_max.
2151df09bc6SSakari Ailus  *
2161df09bc6SSakari Ailus  * The out_values is modified only if a valid u8 value can be decoded.
2171df09bc6SSakari Ailus  */
of_property_read_variable_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz_min,size_t sz_max)2181df09bc6SSakari Ailus int of_property_read_variable_u8_array(const struct device_node *np,
2191df09bc6SSakari Ailus 					const char *propname, u8 *out_values,
2201df09bc6SSakari Ailus 					size_t sz_min, size_t sz_max)
2211df09bc6SSakari Ailus {
2221df09bc6SSakari Ailus 	size_t sz, count;
2231df09bc6SSakari Ailus 	const u8 *val = of_find_property_value_of_size(np, propname,
2241df09bc6SSakari Ailus 						(sz_min * sizeof(*out_values)),
2251df09bc6SSakari Ailus 						(sz_max * sizeof(*out_values)),
2261df09bc6SSakari Ailus 						&sz);
2271df09bc6SSakari Ailus 
2281df09bc6SSakari Ailus 	if (IS_ERR(val))
2291df09bc6SSakari Ailus 		return PTR_ERR(val);
2301df09bc6SSakari Ailus 
2311df09bc6SSakari Ailus 	if (!sz_max)
2321df09bc6SSakari Ailus 		sz = sz_min;
2331df09bc6SSakari Ailus 	else
2341df09bc6SSakari Ailus 		sz /= sizeof(*out_values);
2351df09bc6SSakari Ailus 
2361df09bc6SSakari Ailus 	count = sz;
2371df09bc6SSakari Ailus 	while (count--)
2381df09bc6SSakari Ailus 		*out_values++ = *val++;
2391df09bc6SSakari Ailus 
2401df09bc6SSakari Ailus 	return sz;
2411df09bc6SSakari Ailus }
2421df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
2431df09bc6SSakari Ailus 
2441df09bc6SSakari Ailus /**
2451df09bc6SSakari Ailus  * of_property_read_variable_u16_array - Find and read an array of u16 from a
2461df09bc6SSakari Ailus  * property, with bounds on the minimum and maximum array size.
2471df09bc6SSakari Ailus  *
2481df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
2491df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
2507f3fefeeSMatti Vaittinen  * @out_values:	pointer to found values.
2511df09bc6SSakari Ailus  * @sz_min:	minimum number of array elements to read
2521df09bc6SSakari Ailus  * @sz_max:	maximum number of array elements to read, if zero there is no
2531df09bc6SSakari Ailus  *		upper limit on the number of elements in the dts entry but only
2541df09bc6SSakari Ailus  *		sz_min will be read.
2551df09bc6SSakari Ailus  *
2561df09bc6SSakari Ailus  * Search for a property in a device node and read 16-bit value(s) from
2578c8239c2SRob Herring  * it.
2581df09bc6SSakari Ailus  *
2591df09bc6SSakari Ailus  * dts entry of array should be like:
2608c8239c2SRob Herring  *  ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
2618c8239c2SRob Herring  *
2628c8239c2SRob Herring  * Return: The number of elements read on success, -EINVAL if the property
2638c8239c2SRob Herring  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
2648c8239c2SRob Herring  * if the property data is smaller than sz_min or longer than sz_max.
2651df09bc6SSakari Ailus  *
2661df09bc6SSakari Ailus  * The out_values is modified only if a valid u16 value can be decoded.
2671df09bc6SSakari Ailus  */
of_property_read_variable_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz_min,size_t sz_max)2681df09bc6SSakari Ailus int of_property_read_variable_u16_array(const struct device_node *np,
2691df09bc6SSakari Ailus 					const char *propname, u16 *out_values,
2701df09bc6SSakari Ailus 					size_t sz_min, size_t sz_max)
2711df09bc6SSakari Ailus {
2721df09bc6SSakari Ailus 	size_t sz, count;
2731df09bc6SSakari Ailus 	const __be16 *val = of_find_property_value_of_size(np, propname,
2741df09bc6SSakari Ailus 						(sz_min * sizeof(*out_values)),
2751df09bc6SSakari Ailus 						(sz_max * sizeof(*out_values)),
2761df09bc6SSakari Ailus 						&sz);
2771df09bc6SSakari Ailus 
2781df09bc6SSakari Ailus 	if (IS_ERR(val))
2791df09bc6SSakari Ailus 		return PTR_ERR(val);
2801df09bc6SSakari Ailus 
2811df09bc6SSakari Ailus 	if (!sz_max)
2821df09bc6SSakari Ailus 		sz = sz_min;
2831df09bc6SSakari Ailus 	else
2841df09bc6SSakari Ailus 		sz /= sizeof(*out_values);
2851df09bc6SSakari Ailus 
2861df09bc6SSakari Ailus 	count = sz;
2871df09bc6SSakari Ailus 	while (count--)
2881df09bc6SSakari Ailus 		*out_values++ = be16_to_cpup(val++);
2891df09bc6SSakari Ailus 
2901df09bc6SSakari Ailus 	return sz;
2911df09bc6SSakari Ailus }
2921df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
2931df09bc6SSakari Ailus 
2941df09bc6SSakari Ailus /**
2951df09bc6SSakari Ailus  * of_property_read_variable_u32_array - Find and read an array of 32 bit
2961df09bc6SSakari Ailus  * integers from a property, with bounds on the minimum and maximum array size.
2971df09bc6SSakari Ailus  *
2981df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
2991df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
3007f3fefeeSMatti Vaittinen  * @out_values:	pointer to return found values.
3011df09bc6SSakari Ailus  * @sz_min:	minimum number of array elements to read
3021df09bc6SSakari Ailus  * @sz_max:	maximum number of array elements to read, if zero there is no
3031df09bc6SSakari Ailus  *		upper limit on the number of elements in the dts entry but only
3041df09bc6SSakari Ailus  *		sz_min will be read.
3051df09bc6SSakari Ailus  *
3061df09bc6SSakari Ailus  * Search for a property in a device node and read 32-bit value(s) from
3078c8239c2SRob Herring  * it.
3088c8239c2SRob Herring  *
3098c8239c2SRob Herring  * Return: The number of elements read on success, -EINVAL if the property
3101df09bc6SSakari Ailus  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
3111df09bc6SSakari Ailus  * if the property data is smaller than sz_min or longer than sz_max.
3121df09bc6SSakari Ailus  *
3131df09bc6SSakari Ailus  * The out_values is modified only if a valid u32 value can be decoded.
3141df09bc6SSakari Ailus  */
of_property_read_variable_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz_min,size_t sz_max)3151df09bc6SSakari Ailus int of_property_read_variable_u32_array(const struct device_node *np,
3161df09bc6SSakari Ailus 			       const char *propname, u32 *out_values,
3171df09bc6SSakari Ailus 			       size_t sz_min, size_t sz_max)
3181df09bc6SSakari Ailus {
3191df09bc6SSakari Ailus 	size_t sz, count;
3201df09bc6SSakari Ailus 	const __be32 *val = of_find_property_value_of_size(np, propname,
3211df09bc6SSakari Ailus 						(sz_min * sizeof(*out_values)),
3221df09bc6SSakari Ailus 						(sz_max * sizeof(*out_values)),
3231df09bc6SSakari Ailus 						&sz);
3241df09bc6SSakari Ailus 
3251df09bc6SSakari Ailus 	if (IS_ERR(val))
3261df09bc6SSakari Ailus 		return PTR_ERR(val);
3271df09bc6SSakari Ailus 
3281df09bc6SSakari Ailus 	if (!sz_max)
3291df09bc6SSakari Ailus 		sz = sz_min;
3301df09bc6SSakari Ailus 	else
3311df09bc6SSakari Ailus 		sz /= sizeof(*out_values);
3321df09bc6SSakari Ailus 
3331df09bc6SSakari Ailus 	count = sz;
3341df09bc6SSakari Ailus 	while (count--)
3351df09bc6SSakari Ailus 		*out_values++ = be32_to_cpup(val++);
3361df09bc6SSakari Ailus 
3371df09bc6SSakari Ailus 	return sz;
3381df09bc6SSakari Ailus }
3391df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
3401df09bc6SSakari Ailus 
3411df09bc6SSakari Ailus /**
3421df09bc6SSakari Ailus  * of_property_read_u64 - Find and read a 64 bit integer from a property
3431df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
3441df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
3451df09bc6SSakari Ailus  * @out_value:	pointer to return value, modified only if return value is 0.
3461df09bc6SSakari Ailus  *
3471df09bc6SSakari Ailus  * Search for a property in a device node and read a 64-bit value from
3488c8239c2SRob Herring  * it.
3498c8239c2SRob Herring  *
3508c8239c2SRob Herring  * Return: 0 on success, -EINVAL if the property does not exist,
3511df09bc6SSakari Ailus  * -ENODATA if property does not have a value, and -EOVERFLOW if the
3521df09bc6SSakari Ailus  * property data isn't large enough.
3531df09bc6SSakari Ailus  *
3541df09bc6SSakari Ailus  * The out_value is modified only if a valid u64 value can be decoded.
3551df09bc6SSakari Ailus  */
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)3561df09bc6SSakari Ailus int of_property_read_u64(const struct device_node *np, const char *propname,
3571df09bc6SSakari Ailus 			 u64 *out_value)
3581df09bc6SSakari Ailus {
3591df09bc6SSakari Ailus 	const __be32 *val = of_find_property_value_of_size(np, propname,
3601df09bc6SSakari Ailus 						sizeof(*out_value),
3611df09bc6SSakari Ailus 						0,
3621df09bc6SSakari Ailus 						NULL);
3631df09bc6SSakari Ailus 
3641df09bc6SSakari Ailus 	if (IS_ERR(val))
3651df09bc6SSakari Ailus 		return PTR_ERR(val);
3661df09bc6SSakari Ailus 
3671df09bc6SSakari Ailus 	*out_value = of_read_number(val, 2);
3681df09bc6SSakari Ailus 	return 0;
3691df09bc6SSakari Ailus }
3701df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_u64);
3711df09bc6SSakari Ailus 
3721df09bc6SSakari Ailus /**
3731df09bc6SSakari Ailus  * of_property_read_variable_u64_array - Find and read an array of 64 bit
3741df09bc6SSakari Ailus  * integers from a property, with bounds on the minimum and maximum array size.
3751df09bc6SSakari Ailus  *
3761df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
3771df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
3787f3fefeeSMatti Vaittinen  * @out_values:	pointer to found values.
3791df09bc6SSakari Ailus  * @sz_min:	minimum number of array elements to read
3801df09bc6SSakari Ailus  * @sz_max:	maximum number of array elements to read, if zero there is no
3811df09bc6SSakari Ailus  *		upper limit on the number of elements in the dts entry but only
3821df09bc6SSakari Ailus  *		sz_min will be read.
3831df09bc6SSakari Ailus  *
3841df09bc6SSakari Ailus  * Search for a property in a device node and read 64-bit value(s) from
3858c8239c2SRob Herring  * it.
3868c8239c2SRob Herring  *
3878c8239c2SRob Herring  * Return: The number of elements read on success, -EINVAL if the property
3881df09bc6SSakari Ailus  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
3891df09bc6SSakari Ailus  * if the property data is smaller than sz_min or longer than sz_max.
3901df09bc6SSakari Ailus  *
3911df09bc6SSakari Ailus  * The out_values is modified only if a valid u64 value can be decoded.
3921df09bc6SSakari Ailus  */
of_property_read_variable_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz_min,size_t sz_max)3931df09bc6SSakari Ailus int of_property_read_variable_u64_array(const struct device_node *np,
3941df09bc6SSakari Ailus 			       const char *propname, u64 *out_values,
3951df09bc6SSakari Ailus 			       size_t sz_min, size_t sz_max)
3961df09bc6SSakari Ailus {
3971df09bc6SSakari Ailus 	size_t sz, count;
3981df09bc6SSakari Ailus 	const __be32 *val = of_find_property_value_of_size(np, propname,
3991df09bc6SSakari Ailus 						(sz_min * sizeof(*out_values)),
4001df09bc6SSakari Ailus 						(sz_max * sizeof(*out_values)),
4011df09bc6SSakari Ailus 						&sz);
4021df09bc6SSakari Ailus 
4031df09bc6SSakari Ailus 	if (IS_ERR(val))
4041df09bc6SSakari Ailus 		return PTR_ERR(val);
4051df09bc6SSakari Ailus 
4061df09bc6SSakari Ailus 	if (!sz_max)
4071df09bc6SSakari Ailus 		sz = sz_min;
4081df09bc6SSakari Ailus 	else
4091df09bc6SSakari Ailus 		sz /= sizeof(*out_values);
4101df09bc6SSakari Ailus 
4111df09bc6SSakari Ailus 	count = sz;
4121df09bc6SSakari Ailus 	while (count--) {
4131df09bc6SSakari Ailus 		*out_values++ = of_read_number(val, 2);
4141df09bc6SSakari Ailus 		val += 2;
4151df09bc6SSakari Ailus 	}
4161df09bc6SSakari Ailus 
4171df09bc6SSakari Ailus 	return sz;
4181df09bc6SSakari Ailus }
4191df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
4201df09bc6SSakari Ailus 
4211df09bc6SSakari Ailus /**
4221df09bc6SSakari Ailus  * of_property_read_string - Find and read a string from a property
4231df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
4241df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
4251df09bc6SSakari Ailus  * @out_string:	pointer to null terminated return string, modified only if
4261df09bc6SSakari Ailus  *		return value is 0.
4271df09bc6SSakari Ailus  *
4281df09bc6SSakari Ailus  * Search for a property in a device tree node and retrieve a null
4298c8239c2SRob Herring  * terminated string value (pointer to data, not a copy).
4308c8239c2SRob Herring  *
4318c8239c2SRob Herring  * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
4328c8239c2SRob Herring  * property does not have a value, and -EILSEQ if the string is not
4338c8239c2SRob Herring  * null-terminated within the length of the property data.
4341df09bc6SSakari Ailus  *
435f688d619SStefano Stabellini  * Note that the empty string "" has length of 1, thus -ENODATA cannot
436f688d619SStefano Stabellini  * be interpreted as an empty string.
437f688d619SStefano Stabellini  *
4381df09bc6SSakari Ailus  * The out_string pointer is modified only if a valid string can be decoded.
4391df09bc6SSakari Ailus  */
of_property_read_string(const struct device_node * np,const char * propname,const char ** out_string)4401df09bc6SSakari Ailus int of_property_read_string(const struct device_node *np, const char *propname,
4411df09bc6SSakari Ailus 				const char **out_string)
4421df09bc6SSakari Ailus {
4431df09bc6SSakari Ailus 	const struct property *prop = of_find_property(np, propname, NULL);
4441df09bc6SSakari Ailus 	if (!prop)
4451df09bc6SSakari Ailus 		return -EINVAL;
446f688d619SStefano Stabellini 	if (!prop->length)
4471df09bc6SSakari Ailus 		return -ENODATA;
4481df09bc6SSakari Ailus 	if (strnlen(prop->value, prop->length) >= prop->length)
4491df09bc6SSakari Ailus 		return -EILSEQ;
4501df09bc6SSakari Ailus 	*out_string = prop->value;
4511df09bc6SSakari Ailus 	return 0;
4521df09bc6SSakari Ailus }
4531df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_string);
4541df09bc6SSakari Ailus 
4551df09bc6SSakari Ailus /**
4561df09bc6SSakari Ailus  * of_property_match_string() - Find string in a list and return index
4571df09bc6SSakari Ailus  * @np: pointer to node containing string list property
4581df09bc6SSakari Ailus  * @propname: string list property name
4591df09bc6SSakari Ailus  * @string: pointer to string to search for in string list
4601df09bc6SSakari Ailus  *
4611df09bc6SSakari Ailus  * This function searches a string list property and returns the index
4621df09bc6SSakari Ailus  * of a specific string value.
4631df09bc6SSakari Ailus  */
of_property_match_string(const struct device_node * np,const char * propname,const char * string)4641df09bc6SSakari Ailus int of_property_match_string(const struct device_node *np, const char *propname,
4651df09bc6SSakari Ailus 			     const char *string)
4661df09bc6SSakari Ailus {
4671df09bc6SSakari Ailus 	const struct property *prop = of_find_property(np, propname, NULL);
4681df09bc6SSakari Ailus 	size_t l;
4691df09bc6SSakari Ailus 	int i;
4701df09bc6SSakari Ailus 	const char *p, *end;
4711df09bc6SSakari Ailus 
4721df09bc6SSakari Ailus 	if (!prop)
4731df09bc6SSakari Ailus 		return -EINVAL;
4741df09bc6SSakari Ailus 	if (!prop->value)
4751df09bc6SSakari Ailus 		return -ENODATA;
4761df09bc6SSakari Ailus 
4771df09bc6SSakari Ailus 	p = prop->value;
4781df09bc6SSakari Ailus 	end = p + prop->length;
4791df09bc6SSakari Ailus 
4801df09bc6SSakari Ailus 	for (i = 0; p < end; i++, p += l) {
4811df09bc6SSakari Ailus 		l = strnlen(p, end - p) + 1;
4821df09bc6SSakari Ailus 		if (p + l > end)
4831df09bc6SSakari Ailus 			return -EILSEQ;
4841df09bc6SSakari Ailus 		pr_debug("comparing %s with %s\n", string, p);
4851df09bc6SSakari Ailus 		if (strcmp(string, p) == 0)
4861df09bc6SSakari Ailus 			return i; /* Found it; return index */
4871df09bc6SSakari Ailus 	}
4881df09bc6SSakari Ailus 	return -ENODATA;
4891df09bc6SSakari Ailus }
4901df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_match_string);
4911df09bc6SSakari Ailus 
4921df09bc6SSakari Ailus /**
4931df09bc6SSakari Ailus  * of_property_read_string_helper() - Utility helper for parsing string properties
4941df09bc6SSakari Ailus  * @np:		device node from which the property value is to be read.
4951df09bc6SSakari Ailus  * @propname:	name of the property to be searched.
4961df09bc6SSakari Ailus  * @out_strs:	output array of string pointers.
4971df09bc6SSakari Ailus  * @sz:		number of array elements to read.
4981df09bc6SSakari Ailus  * @skip:	Number of strings to skip over at beginning of list.
4991df09bc6SSakari Ailus  *
5001df09bc6SSakari Ailus  * Don't call this function directly. It is a utility helper for the
5011df09bc6SSakari Ailus  * of_property_read_string*() family of functions.
5021df09bc6SSakari Ailus  */
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int skip)5031df09bc6SSakari Ailus int of_property_read_string_helper(const struct device_node *np,
5041df09bc6SSakari Ailus 				   const char *propname, const char **out_strs,
5051df09bc6SSakari Ailus 				   size_t sz, int skip)
5061df09bc6SSakari Ailus {
5071df09bc6SSakari Ailus 	const struct property *prop = of_find_property(np, propname, NULL);
5081df09bc6SSakari Ailus 	int l = 0, i = 0;
5091df09bc6SSakari Ailus 	const char *p, *end;
5101df09bc6SSakari Ailus 
5111df09bc6SSakari Ailus 	if (!prop)
5121df09bc6SSakari Ailus 		return -EINVAL;
5131df09bc6SSakari Ailus 	if (!prop->value)
5141df09bc6SSakari Ailus 		return -ENODATA;
5151df09bc6SSakari Ailus 	p = prop->value;
5161df09bc6SSakari Ailus 	end = p + prop->length;
5171df09bc6SSakari Ailus 
5181df09bc6SSakari Ailus 	for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
5191df09bc6SSakari Ailus 		l = strnlen(p, end - p) + 1;
5201df09bc6SSakari Ailus 		if (p + l > end)
5211df09bc6SSakari Ailus 			return -EILSEQ;
5221df09bc6SSakari Ailus 		if (out_strs && i >= skip)
5231df09bc6SSakari Ailus 			*out_strs++ = p;
5241df09bc6SSakari Ailus 	}
5251df09bc6SSakari Ailus 	i -= skip;
5261df09bc6SSakari Ailus 	return i <= 0 ? -ENODATA : i;
5271df09bc6SSakari Ailus }
5281df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_property_read_string_helper);
5291df09bc6SSakari Ailus 
of_prop_next_u32(struct property * prop,const __be32 * cur,u32 * pu)5301df09bc6SSakari Ailus const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
5311df09bc6SSakari Ailus 			       u32 *pu)
5321df09bc6SSakari Ailus {
5331df09bc6SSakari Ailus 	const void *curv = cur;
5341df09bc6SSakari Ailus 
5351df09bc6SSakari Ailus 	if (!prop)
5361df09bc6SSakari Ailus 		return NULL;
5371df09bc6SSakari Ailus 
5381df09bc6SSakari Ailus 	if (!cur) {
5391df09bc6SSakari Ailus 		curv = prop->value;
5401df09bc6SSakari Ailus 		goto out_val;
5411df09bc6SSakari Ailus 	}
5421df09bc6SSakari Ailus 
5431df09bc6SSakari Ailus 	curv += sizeof(*cur);
5441df09bc6SSakari Ailus 	if (curv >= prop->value + prop->length)
5451df09bc6SSakari Ailus 		return NULL;
5461df09bc6SSakari Ailus 
5471df09bc6SSakari Ailus out_val:
5481df09bc6SSakari Ailus 	*pu = be32_to_cpup(curv);
5491df09bc6SSakari Ailus 	return curv;
5501df09bc6SSakari Ailus }
5511df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_prop_next_u32);
5521df09bc6SSakari Ailus 
of_prop_next_string(struct property * prop,const char * cur)5531df09bc6SSakari Ailus const char *of_prop_next_string(struct property *prop, const char *cur)
5541df09bc6SSakari Ailus {
5551df09bc6SSakari Ailus 	const void *curv = cur;
5561df09bc6SSakari Ailus 
5571df09bc6SSakari Ailus 	if (!prop)
5581df09bc6SSakari Ailus 		return NULL;
5591df09bc6SSakari Ailus 
5601df09bc6SSakari Ailus 	if (!cur)
5611df09bc6SSakari Ailus 		return prop->value;
5621df09bc6SSakari Ailus 
5631df09bc6SSakari Ailus 	curv += strlen(cur) + 1;
5641df09bc6SSakari Ailus 	if (curv >= prop->value + prop->length)
5651df09bc6SSakari Ailus 		return NULL;
5661df09bc6SSakari Ailus 
5671df09bc6SSakari Ailus 	return curv;
5681df09bc6SSakari Ailus }
5691df09bc6SSakari Ailus EXPORT_SYMBOL_GPL(of_prop_next_string);
5701df09bc6SSakari Ailus 
5711df09bc6SSakari Ailus /**
5721df09bc6SSakari Ailus  * of_graph_parse_endpoint() - parse common endpoint node properties
5731df09bc6SSakari Ailus  * @node: pointer to endpoint device_node
5741df09bc6SSakari Ailus  * @endpoint: pointer to the OF endpoint data structure
5751df09bc6SSakari Ailus  *
5761df09bc6SSakari Ailus  * The caller should hold a reference to @node.
5771df09bc6SSakari Ailus  */
of_graph_parse_endpoint(const struct device_node * node,struct of_endpoint * endpoint)5781df09bc6SSakari Ailus int of_graph_parse_endpoint(const struct device_node *node,
5791df09bc6SSakari Ailus 			    struct of_endpoint *endpoint)
5801df09bc6SSakari Ailus {
5811df09bc6SSakari Ailus 	struct device_node *port_node = of_get_parent(node);
5821df09bc6SSakari Ailus 
5830d638a07SRob Herring 	WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
5840d638a07SRob Herring 		  __func__, node);
5851df09bc6SSakari Ailus 
5861df09bc6SSakari Ailus 	memset(endpoint, 0, sizeof(*endpoint));
5871df09bc6SSakari Ailus 
5881df09bc6SSakari Ailus 	endpoint->local_node = node;
5891df09bc6SSakari Ailus 	/*
5901df09bc6SSakari Ailus 	 * It doesn't matter whether the two calls below succeed.
5911df09bc6SSakari Ailus 	 * If they don't then the default value 0 is used.
5921df09bc6SSakari Ailus 	 */
5931df09bc6SSakari Ailus 	of_property_read_u32(port_node, "reg", &endpoint->port);
5941df09bc6SSakari Ailus 	of_property_read_u32(node, "reg", &endpoint->id);
5951df09bc6SSakari Ailus 
5961df09bc6SSakari Ailus 	of_node_put(port_node);
5971df09bc6SSakari Ailus 
5981df09bc6SSakari Ailus 	return 0;
5991df09bc6SSakari Ailus }
6001df09bc6SSakari Ailus EXPORT_SYMBOL(of_graph_parse_endpoint);
6011df09bc6SSakari Ailus 
6021df09bc6SSakari Ailus /**
6031df09bc6SSakari Ailus  * of_graph_get_port_by_id() - get the port matching a given id
6041df09bc6SSakari Ailus  * @parent: pointer to the parent device node
6051df09bc6SSakari Ailus  * @id: id of the port
6061df09bc6SSakari Ailus  *
6071df09bc6SSakari Ailus  * Return: A 'port' node pointer with refcount incremented. The caller
6081df09bc6SSakari Ailus  * has to use of_node_put() on it when done.
6091df09bc6SSakari Ailus  */
of_graph_get_port_by_id(struct device_node * parent,u32 id)6101df09bc6SSakari Ailus struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
6111df09bc6SSakari Ailus {
6121df09bc6SSakari Ailus 	struct device_node *node, *port;
6131df09bc6SSakari Ailus 
6141df09bc6SSakari Ailus 	node = of_get_child_by_name(parent, "ports");
6151df09bc6SSakari Ailus 	if (node)
6161df09bc6SSakari Ailus 		parent = node;
6171df09bc6SSakari Ailus 
6181df09bc6SSakari Ailus 	for_each_child_of_node(parent, port) {
6191df09bc6SSakari Ailus 		u32 port_id = 0;
6201df09bc6SSakari Ailus 
621b3e46d1aSRob Herring 		if (!of_node_name_eq(port, "port"))
6221df09bc6SSakari Ailus 			continue;
6231df09bc6SSakari Ailus 		of_property_read_u32(port, "reg", &port_id);
6241df09bc6SSakari Ailus 		if (id == port_id)
6251df09bc6SSakari Ailus 			break;
6261df09bc6SSakari Ailus 	}
6271df09bc6SSakari Ailus 
6281df09bc6SSakari Ailus 	of_node_put(node);
6291df09bc6SSakari Ailus 
6301df09bc6SSakari Ailus 	return port;
6311df09bc6SSakari Ailus }
6321df09bc6SSakari Ailus EXPORT_SYMBOL(of_graph_get_port_by_id);
6331df09bc6SSakari Ailus 
6341df09bc6SSakari Ailus /**
6351df09bc6SSakari Ailus  * of_graph_get_next_endpoint() - get next endpoint node
6361df09bc6SSakari Ailus  * @parent: pointer to the parent device node
6371df09bc6SSakari Ailus  * @prev: previous endpoint node, or NULL to get first
6381df09bc6SSakari Ailus  *
6391df09bc6SSakari Ailus  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
6401df09bc6SSakari Ailus  * of the passed @prev node is decremented.
6411df09bc6SSakari Ailus  */
of_graph_get_next_endpoint(const struct device_node * parent,struct device_node * prev)6421df09bc6SSakari Ailus struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
6431df09bc6SSakari Ailus 					struct device_node *prev)
6441df09bc6SSakari Ailus {
6451df09bc6SSakari Ailus 	struct device_node *endpoint;
6461df09bc6SSakari Ailus 	struct device_node *port;
6471df09bc6SSakari Ailus 
6481df09bc6SSakari Ailus 	if (!parent)
6491df09bc6SSakari Ailus 		return NULL;
6501df09bc6SSakari Ailus 
6511df09bc6SSakari Ailus 	/*
6521df09bc6SSakari Ailus 	 * Start by locating the port node. If no previous endpoint is specified
6531df09bc6SSakari Ailus 	 * search for the first port node, otherwise get the previous endpoint
6541df09bc6SSakari Ailus 	 * parent port node.
6551df09bc6SSakari Ailus 	 */
6561df09bc6SSakari Ailus 	if (!prev) {
6571df09bc6SSakari Ailus 		struct device_node *node;
6581df09bc6SSakari Ailus 
6591df09bc6SSakari Ailus 		node = of_get_child_by_name(parent, "ports");
6601df09bc6SSakari Ailus 		if (node)
6611df09bc6SSakari Ailus 			parent = node;
6621df09bc6SSakari Ailus 
6631df09bc6SSakari Ailus 		port = of_get_child_by_name(parent, "port");
6641df09bc6SSakari Ailus 		of_node_put(node);
6651df09bc6SSakari Ailus 
6661df09bc6SSakari Ailus 		if (!port) {
6670d638a07SRob Herring 			pr_err("graph: no port node found in %pOF\n", parent);
6681df09bc6SSakari Ailus 			return NULL;
6691df09bc6SSakari Ailus 		}
6701df09bc6SSakari Ailus 	} else {
6711df09bc6SSakari Ailus 		port = of_get_parent(prev);
6720d638a07SRob Herring 		if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
6730d638a07SRob Herring 			      __func__, prev))
6741df09bc6SSakari Ailus 			return NULL;
6751df09bc6SSakari Ailus 	}
6761df09bc6SSakari Ailus 
6771df09bc6SSakari Ailus 	while (1) {
6781df09bc6SSakari Ailus 		/*
6791df09bc6SSakari Ailus 		 * Now that we have a port node, get the next endpoint by
6801df09bc6SSakari Ailus 		 * getting the next child. If the previous endpoint is NULL this
6811df09bc6SSakari Ailus 		 * will return the first child.
6821df09bc6SSakari Ailus 		 */
6831df09bc6SSakari Ailus 		endpoint = of_get_next_child(port, prev);
6841df09bc6SSakari Ailus 		if (endpoint) {
6851df09bc6SSakari Ailus 			of_node_put(port);
6861df09bc6SSakari Ailus 			return endpoint;
6871df09bc6SSakari Ailus 		}
6881df09bc6SSakari Ailus 
6891df09bc6SSakari Ailus 		/* No more endpoints under this port, try the next one. */
6901df09bc6SSakari Ailus 		prev = NULL;
6911df09bc6SSakari Ailus 
6921df09bc6SSakari Ailus 		do {
6931df09bc6SSakari Ailus 			port = of_get_next_child(parent, port);
6941df09bc6SSakari Ailus 			if (!port)
6951df09bc6SSakari Ailus 				return NULL;
696b3e46d1aSRob Herring 		} while (!of_node_name_eq(port, "port"));
6971df09bc6SSakari Ailus 	}
6981df09bc6SSakari Ailus }
6991df09bc6SSakari Ailus EXPORT_SYMBOL(of_graph_get_next_endpoint);
7001df09bc6SSakari Ailus 
7011df09bc6SSakari Ailus /**
7021df09bc6SSakari Ailus  * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
7031df09bc6SSakari Ailus  * @parent: pointer to the parent device node
7041df09bc6SSakari Ailus  * @port_reg: identifier (value of reg property) of the parent port node
7051df09bc6SSakari Ailus  * @reg: identifier (value of reg property) of the endpoint node
7061df09bc6SSakari Ailus  *
7071df09bc6SSakari Ailus  * Return: An 'endpoint' node pointer which is identified by reg and at the same
7081df09bc6SSakari Ailus  * is the child of a port node identified by port_reg. reg and port_reg are
709deb387d4SMaxime Ripard  * ignored when they are -1. Use of_node_put() on the pointer when done.
7101df09bc6SSakari Ailus  */
of_graph_get_endpoint_by_regs(const struct device_node * parent,int port_reg,int reg)7111df09bc6SSakari Ailus struct device_node *of_graph_get_endpoint_by_regs(
7121df09bc6SSakari Ailus 	const struct device_node *parent, int port_reg, int reg)
7131df09bc6SSakari Ailus {
7141df09bc6SSakari Ailus 	struct of_endpoint endpoint;
7151df09bc6SSakari Ailus 	struct device_node *node = NULL;
7161df09bc6SSakari Ailus 
7171df09bc6SSakari Ailus 	for_each_endpoint_of_node(parent, node) {
7181df09bc6SSakari Ailus 		of_graph_parse_endpoint(node, &endpoint);
7191df09bc6SSakari Ailus 		if (((port_reg == -1) || (endpoint.port == port_reg)) &&
7201df09bc6SSakari Ailus 			((reg == -1) || (endpoint.id == reg)))
7211df09bc6SSakari Ailus 			return node;
7221df09bc6SSakari Ailus 	}
7231df09bc6SSakari Ailus 
7241df09bc6SSakari Ailus 	return NULL;
7251df09bc6SSakari Ailus }
7261df09bc6SSakari Ailus EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
7271df09bc6SSakari Ailus 
7281df09bc6SSakari Ailus /**
729b8ba92b1SRob Herring  * of_graph_get_remote_endpoint() - get remote endpoint node
730b8ba92b1SRob Herring  * @node: pointer to a local endpoint device_node
731b8ba92b1SRob Herring  *
732b8ba92b1SRob Herring  * Return: Remote endpoint node associated with remote endpoint node linked
733b8ba92b1SRob Herring  *	   to @node. Use of_node_put() on it when done.
734b8ba92b1SRob Herring  */
of_graph_get_remote_endpoint(const struct device_node * node)735b8ba92b1SRob Herring struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
736b8ba92b1SRob Herring {
737b8ba92b1SRob Herring 	/* Get remote endpoint node. */
738b8ba92b1SRob Herring 	return of_parse_phandle(node, "remote-endpoint", 0);
739b8ba92b1SRob Herring }
740b8ba92b1SRob Herring EXPORT_SYMBOL(of_graph_get_remote_endpoint);
741b8ba92b1SRob Herring 
742b8ba92b1SRob Herring /**
743b8ba92b1SRob Herring  * of_graph_get_port_parent() - get port's parent node
744b8ba92b1SRob Herring  * @node: pointer to a local endpoint device_node
745b8ba92b1SRob Herring  *
746b8ba92b1SRob Herring  * Return: device node associated with endpoint node linked
747b8ba92b1SRob Herring  *	   to @node. Use of_node_put() on it when done.
748b8ba92b1SRob Herring  */
of_graph_get_port_parent(struct device_node * node)749b8ba92b1SRob Herring struct device_node *of_graph_get_port_parent(struct device_node *node)
750b8ba92b1SRob Herring {
751b8ba92b1SRob Herring 	unsigned int depth;
752b8ba92b1SRob Herring 
753c0a480d1STony Lindgren 	if (!node)
754c0a480d1STony Lindgren 		return NULL;
755c0a480d1STony Lindgren 
756c0a480d1STony Lindgren 	/*
757c0a480d1STony Lindgren 	 * Preserve usecount for passed in node as of_get_next_parent()
758c0a480d1STony Lindgren 	 * will do of_node_put() on it.
759c0a480d1STony Lindgren 	 */
760c0a480d1STony Lindgren 	of_node_get(node);
761c0a480d1STony Lindgren 
762b8ba92b1SRob Herring 	/* Walk 3 levels up only if there is 'ports' node. */
763b8ba92b1SRob Herring 	for (depth = 3; depth && node; depth--) {
764b8ba92b1SRob Herring 		node = of_get_next_parent(node);
765c20fc130SSaravana Kannan 		if (depth == 2 && !of_node_name_eq(node, "ports") &&
766c20fc130SSaravana Kannan 		    !of_node_name_eq(node, "in-ports") &&
767c20fc130SSaravana Kannan 		    !of_node_name_eq(node, "out-ports"))
768b8ba92b1SRob Herring 			break;
769b8ba92b1SRob Herring 	}
770b8ba92b1SRob Herring 	return node;
771b8ba92b1SRob Herring }
772b8ba92b1SRob Herring EXPORT_SYMBOL(of_graph_get_port_parent);
773b8ba92b1SRob Herring 
774b8ba92b1SRob Herring /**
7751df09bc6SSakari Ailus  * of_graph_get_remote_port_parent() - get remote port's parent node
7761df09bc6SSakari Ailus  * @node: pointer to a local endpoint device_node
7771df09bc6SSakari Ailus  *
7781df09bc6SSakari Ailus  * Return: Remote device node associated with remote endpoint node linked
7791df09bc6SSakari Ailus  *	   to @node. Use of_node_put() on it when done.
7801df09bc6SSakari Ailus  */
of_graph_get_remote_port_parent(const struct device_node * node)7811df09bc6SSakari Ailus struct device_node *of_graph_get_remote_port_parent(
7821df09bc6SSakari Ailus 			       const struct device_node *node)
7831df09bc6SSakari Ailus {
784c0a480d1STony Lindgren 	struct device_node *np, *pp;
7851df09bc6SSakari Ailus 
7861df09bc6SSakari Ailus 	/* Get remote endpoint node. */
787b8ba92b1SRob Herring 	np = of_graph_get_remote_endpoint(node);
7881df09bc6SSakari Ailus 
789c0a480d1STony Lindgren 	pp = of_graph_get_port_parent(np);
790c0a480d1STony Lindgren 
791c0a480d1STony Lindgren 	of_node_put(np);
792c0a480d1STony Lindgren 
793c0a480d1STony Lindgren 	return pp;
7941df09bc6SSakari Ailus }
7951df09bc6SSakari Ailus EXPORT_SYMBOL(of_graph_get_remote_port_parent);
7961df09bc6SSakari Ailus 
7971df09bc6SSakari Ailus /**
7981df09bc6SSakari Ailus  * of_graph_get_remote_port() - get remote port node
7991df09bc6SSakari Ailus  * @node: pointer to a local endpoint device_node
8001df09bc6SSakari Ailus  *
8011df09bc6SSakari Ailus  * Return: Remote port node associated with remote endpoint node linked
8021df09bc6SSakari Ailus  * to @node. Use of_node_put() on it when done.
8031df09bc6SSakari Ailus  */
of_graph_get_remote_port(const struct device_node * node)8041df09bc6SSakari Ailus struct device_node *of_graph_get_remote_port(const struct device_node *node)
8051df09bc6SSakari Ailus {
8061df09bc6SSakari Ailus 	struct device_node *np;
8071df09bc6SSakari Ailus 
8081df09bc6SSakari Ailus 	/* Get remote endpoint node. */
809b8ba92b1SRob Herring 	np = of_graph_get_remote_endpoint(node);
8101df09bc6SSakari Ailus 	if (!np)
8111df09bc6SSakari Ailus 		return NULL;
8121df09bc6SSakari Ailus 	return of_get_next_parent(np);
8131df09bc6SSakari Ailus }
8141df09bc6SSakari Ailus EXPORT_SYMBOL(of_graph_get_remote_port);
8151df09bc6SSakari Ailus 
of_graph_get_endpoint_count(const struct device_node * np)816b8ba92b1SRob Herring int of_graph_get_endpoint_count(const struct device_node *np)
817b8ba92b1SRob Herring {
818b8ba92b1SRob Herring 	struct device_node *endpoint;
819b8ba92b1SRob Herring 	int num = 0;
820b8ba92b1SRob Herring 
821b8ba92b1SRob Herring 	for_each_endpoint_of_node(np, endpoint)
822b8ba92b1SRob Herring 		num++;
823b8ba92b1SRob Herring 
824b8ba92b1SRob Herring 	return num;
825b8ba92b1SRob Herring }
826b8ba92b1SRob Herring EXPORT_SYMBOL(of_graph_get_endpoint_count);
827b8ba92b1SRob Herring 
8281df09bc6SSakari Ailus /**
8291df09bc6SSakari Ailus  * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
8301df09bc6SSakari Ailus  * @node: pointer to parent device_node containing graph port/endpoint
8311df09bc6SSakari Ailus  * @port: identifier (value of reg property) of the parent port node
8321df09bc6SSakari Ailus  * @endpoint: identifier (value of reg property) of the endpoint node
8331df09bc6SSakari Ailus  *
8341df09bc6SSakari Ailus  * Return: Remote device node associated with remote endpoint node linked
8351df09bc6SSakari Ailus  * to @node. Use of_node_put() on it when done.
8361df09bc6SSakari Ailus  */
of_graph_get_remote_node(const struct device_node * node,u32 port,u32 endpoint)8371df09bc6SSakari Ailus struct device_node *of_graph_get_remote_node(const struct device_node *node,
8381df09bc6SSakari Ailus 					     u32 port, u32 endpoint)
8391df09bc6SSakari Ailus {
8401df09bc6SSakari Ailus 	struct device_node *endpoint_node, *remote;
8411df09bc6SSakari Ailus 
8421df09bc6SSakari Ailus 	endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
8431df09bc6SSakari Ailus 	if (!endpoint_node) {
8440d638a07SRob Herring 		pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
8450d638a07SRob Herring 			 port, endpoint, node);
8461df09bc6SSakari Ailus 		return NULL;
8471df09bc6SSakari Ailus 	}
8481df09bc6SSakari Ailus 
8491df09bc6SSakari Ailus 	remote = of_graph_get_remote_port_parent(endpoint_node);
8501df09bc6SSakari Ailus 	of_node_put(endpoint_node);
8511df09bc6SSakari Ailus 	if (!remote) {
8521df09bc6SSakari Ailus 		pr_debug("no valid remote node\n");
8531df09bc6SSakari Ailus 		return NULL;
8541df09bc6SSakari Ailus 	}
8551df09bc6SSakari Ailus 
8561df09bc6SSakari Ailus 	if (!of_device_is_available(remote)) {
8571df09bc6SSakari Ailus 		pr_debug("not available for remote node\n");
85828b170e8SJulia Lawall 		of_node_put(remote);
8591df09bc6SSakari Ailus 		return NULL;
8601df09bc6SSakari Ailus 	}
8611df09bc6SSakari Ailus 
8621df09bc6SSakari Ailus 	return remote;
8631df09bc6SSakari Ailus }
8641df09bc6SSakari Ailus EXPORT_SYMBOL(of_graph_get_remote_node);
8653708184aSSakari Ailus 
of_fwnode_get(struct fwnode_handle * fwnode)866cf89a31cSSakari Ailus static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
8673708184aSSakari Ailus {
868cf89a31cSSakari Ailus 	return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
8693708184aSSakari Ailus }
8703708184aSSakari Ailus 
of_fwnode_put(struct fwnode_handle * fwnode)8713708184aSSakari Ailus static void of_fwnode_put(struct fwnode_handle *fwnode)
8723708184aSSakari Ailus {
8733708184aSSakari Ailus 	of_node_put(to_of_node(fwnode));
8743708184aSSakari Ailus }
8753708184aSSakari Ailus 
of_fwnode_device_is_available(const struct fwnode_handle * fwnode)87637ba983cSSakari Ailus static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
8772294b3afSSakari Ailus {
8782294b3afSSakari Ailus 	return of_device_is_available(to_of_node(fwnode));
8792294b3afSSakari Ailus }
8802294b3afSSakari Ailus 
of_fwnode_device_dma_supported(const struct fwnode_handle * fwnode)8818c756a0aSSakari Ailus static bool of_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
8828c756a0aSSakari Ailus {
8838c756a0aSSakari Ailus 	return true;
8848c756a0aSSakari Ailus }
8858c756a0aSSakari Ailus 
8868c756a0aSSakari Ailus static enum dev_dma_attr
of_fwnode_device_get_dma_attr(const struct fwnode_handle * fwnode)8878c756a0aSSakari Ailus of_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
8888c756a0aSSakari Ailus {
8898c756a0aSSakari Ailus 	if (of_dma_is_coherent(to_of_node(fwnode)))
8908c756a0aSSakari Ailus 		return DEV_DMA_COHERENT;
8918c756a0aSSakari Ailus 	else
8928c756a0aSSakari Ailus 		return DEV_DMA_NON_COHERENT;
8938c756a0aSSakari Ailus }
8948c756a0aSSakari Ailus 
of_fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)89537ba983cSSakari Ailus static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
8963708184aSSakari Ailus 				       const char *propname)
8973708184aSSakari Ailus {
8983708184aSSakari Ailus 	return of_property_read_bool(to_of_node(fwnode), propname);
8993708184aSSakari Ailus }
9003708184aSSakari Ailus 
of_fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)90137ba983cSSakari Ailus static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
9023708184aSSakari Ailus 					     const char *propname,
9033708184aSSakari Ailus 					     unsigned int elem_size, void *val,
9043708184aSSakari Ailus 					     size_t nval)
9053708184aSSakari Ailus {
90637ba983cSSakari Ailus 	const struct device_node *node = to_of_node(fwnode);
9073708184aSSakari Ailus 
9083708184aSSakari Ailus 	if (!val)
9093708184aSSakari Ailus 		return of_property_count_elems_of_size(node, propname,
9103708184aSSakari Ailus 						       elem_size);
9113708184aSSakari Ailus 
9123708184aSSakari Ailus 	switch (elem_size) {
9133708184aSSakari Ailus 	case sizeof(u8):
9143708184aSSakari Ailus 		return of_property_read_u8_array(node, propname, val, nval);
9153708184aSSakari Ailus 	case sizeof(u16):
9163708184aSSakari Ailus 		return of_property_read_u16_array(node, propname, val, nval);
9173708184aSSakari Ailus 	case sizeof(u32):
9183708184aSSakari Ailus 		return of_property_read_u32_array(node, propname, val, nval);
9193708184aSSakari Ailus 	case sizeof(u64):
9203708184aSSakari Ailus 		return of_property_read_u64_array(node, propname, val, nval);
9213708184aSSakari Ailus 	}
9223708184aSSakari Ailus 
9233708184aSSakari Ailus 	return -ENXIO;
9243708184aSSakari Ailus }
9253708184aSSakari Ailus 
92637ba983cSSakari Ailus static int
of_fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)92737ba983cSSakari Ailus of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
92837ba983cSSakari Ailus 				     const char *propname, const char **val,
92937ba983cSSakari Ailus 				     size_t nval)
9303708184aSSakari Ailus {
93137ba983cSSakari Ailus 	const struct device_node *node = to_of_node(fwnode);
9323708184aSSakari Ailus 
9333708184aSSakari Ailus 	return val ?
9343708184aSSakari Ailus 		of_property_read_string_array(node, propname, val, nval) :
9353708184aSSakari Ailus 		of_property_count_strings(node, propname);
9363708184aSSakari Ailus }
9373708184aSSakari Ailus 
of_fwnode_get_name(const struct fwnode_handle * fwnode)938bc0500c1SSakari Ailus static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
939bc0500c1SSakari Ailus {
940bc0500c1SSakari Ailus 	return kbasename(to_of_node(fwnode)->full_name);
941bc0500c1SSakari Ailus }
942bc0500c1SSakari Ailus 
of_fwnode_get_name_prefix(const struct fwnode_handle * fwnode)943e7e242bcSSakari Ailus static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
944e7e242bcSSakari Ailus {
945e7e242bcSSakari Ailus 	/* Root needs no prefix here (its name is "/"). */
946e7e242bcSSakari Ailus 	if (!to_of_node(fwnode)->parent)
947e7e242bcSSakari Ailus 		return "";
948e7e242bcSSakari Ailus 
949e7e242bcSSakari Ailus 	return "/";
950e7e242bcSSakari Ailus }
951e7e242bcSSakari Ailus 
95237ba983cSSakari Ailus static struct fwnode_handle *
of_fwnode_get_parent(const struct fwnode_handle * fwnode)95337ba983cSSakari Ailus of_fwnode_get_parent(const struct fwnode_handle *fwnode)
9543708184aSSakari Ailus {
9553708184aSSakari Ailus 	return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
9563708184aSSakari Ailus }
9573708184aSSakari Ailus 
9583708184aSSakari Ailus static struct fwnode_handle *
of_fwnode_get_next_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)95937ba983cSSakari Ailus of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
9603708184aSSakari Ailus 			      struct fwnode_handle *child)
9613708184aSSakari Ailus {
9623708184aSSakari Ailus 	return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
9633708184aSSakari Ailus 							    to_of_node(child)));
9643708184aSSakari Ailus }
9653708184aSSakari Ailus 
9663708184aSSakari Ailus static struct fwnode_handle *
of_fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)96737ba983cSSakari Ailus of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
9683708184aSSakari Ailus 			       const char *childname)
9693708184aSSakari Ailus {
97037ba983cSSakari Ailus 	const struct device_node *node = to_of_node(fwnode);
9713708184aSSakari Ailus 	struct device_node *child;
9723708184aSSakari Ailus 
9733708184aSSakari Ailus 	for_each_available_child_of_node(node, child)
974b3e46d1aSRob Herring 		if (of_node_name_eq(child, childname))
9753708184aSSakari Ailus 			return of_fwnode_handle(child);
9763708184aSSakari Ailus 
9773708184aSSakari Ailus 	return NULL;
9783708184aSSakari Ailus }
9793708184aSSakari Ailus 
9803e3119d3SSakari Ailus static int
of_fwnode_get_reference_args(const struct fwnode_handle * fwnode,const char * prop,const char * nargs_prop,unsigned int nargs,unsigned int index,struct fwnode_reference_args * args)9813e3119d3SSakari Ailus of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
9823e3119d3SSakari Ailus 			     const char *prop, const char *nargs_prop,
9833e3119d3SSakari Ailus 			     unsigned int nargs, unsigned int index,
9843e3119d3SSakari Ailus 			     struct fwnode_reference_args *args)
9853e3119d3SSakari Ailus {
9863e3119d3SSakari Ailus 	struct of_phandle_args of_args;
9873e3119d3SSakari Ailus 	unsigned int i;
9883e3119d3SSakari Ailus 	int ret;
9893e3119d3SSakari Ailus 
9903e3119d3SSakari Ailus 	if (nargs_prop)
9913e3119d3SSakari Ailus 		ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
9923e3119d3SSakari Ailus 						 nargs_prop, index, &of_args);
9933e3119d3SSakari Ailus 	else
9943e3119d3SSakari Ailus 		ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
9953e3119d3SSakari Ailus 						       nargs, index, &of_args);
9963e3119d3SSakari Ailus 	if (ret < 0)
9973e3119d3SSakari Ailus 		return ret;
99860d865bdSYang Yingliang 	if (!args) {
99960d865bdSYang Yingliang 		of_node_put(of_args.np);
10003e3119d3SSakari Ailus 		return 0;
100160d865bdSYang Yingliang 	}
10023e3119d3SSakari Ailus 
10033e3119d3SSakari Ailus 	args->nargs = of_args.args_count;
10043e3119d3SSakari Ailus 	args->fwnode = of_fwnode_handle(of_args.np);
10053e3119d3SSakari Ailus 
10063e3119d3SSakari Ailus 	for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
10073e3119d3SSakari Ailus 		args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
10083e3119d3SSakari Ailus 
10093e3119d3SSakari Ailus 	return 0;
10103e3119d3SSakari Ailus }
10113e3119d3SSakari Ailus 
10123b27d00eSSakari Ailus static struct fwnode_handle *
of_fwnode_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)101337ba983cSSakari Ailus of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
10143b27d00eSSakari Ailus 				  struct fwnode_handle *prev)
10153b27d00eSSakari Ailus {
10163b27d00eSSakari Ailus 	return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
10173b27d00eSSakari Ailus 							   to_of_node(prev)));
10183b27d00eSSakari Ailus }
10193b27d00eSSakari Ailus 
10203b27d00eSSakari Ailus static struct fwnode_handle *
of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle * fwnode)102137ba983cSSakari Ailus of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
10223b27d00eSSakari Ailus {
1023358155edSKuninori Morimoto 	return of_fwnode_handle(
1024358155edSKuninori Morimoto 		of_graph_get_remote_endpoint(to_of_node(fwnode)));
10253b27d00eSSakari Ailus }
10263b27d00eSSakari Ailus 
10273b27d00eSSakari Ailus static struct fwnode_handle *
of_fwnode_graph_get_port_parent(struct fwnode_handle * fwnode)10283b27d00eSSakari Ailus of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
10293b27d00eSSakari Ailus {
10303b27d00eSSakari Ailus 	struct device_node *np;
10313b27d00eSSakari Ailus 
10323b27d00eSSakari Ailus 	/* Get the parent of the port */
10333314c6bdSNiklas Söderlund 	np = of_get_parent(to_of_node(fwnode));
10343b27d00eSSakari Ailus 	if (!np)
10353b27d00eSSakari Ailus 		return NULL;
10363b27d00eSSakari Ailus 
10373b27d00eSSakari Ailus 	/* Is this the "ports" node? If not, it's the port parent. */
1038b3e46d1aSRob Herring 	if (!of_node_name_eq(np, "ports"))
10393b27d00eSSakari Ailus 		return of_fwnode_handle(np);
10403b27d00eSSakari Ailus 
10413b27d00eSSakari Ailus 	return of_fwnode_handle(of_get_next_parent(np));
10423b27d00eSSakari Ailus }
10433b27d00eSSakari Ailus 
of_fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)104437ba983cSSakari Ailus static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
10453b27d00eSSakari Ailus 					  struct fwnode_endpoint *endpoint)
10463b27d00eSSakari Ailus {
104737ba983cSSakari Ailus 	const struct device_node *node = to_of_node(fwnode);
10483b27d00eSSakari Ailus 	struct device_node *port_node = of_get_parent(node);
10493b27d00eSSakari Ailus 
10503b27d00eSSakari Ailus 	endpoint->local_fwnode = fwnode;
10513b27d00eSSakari Ailus 
10523b27d00eSSakari Ailus 	of_property_read_u32(port_node, "reg", &endpoint->port);
10533b27d00eSSakari Ailus 	of_property_read_u32(node, "reg", &endpoint->id);
10543b27d00eSSakari Ailus 
10553b27d00eSSakari Ailus 	of_node_put(port_node);
10563b27d00eSSakari Ailus 
10573b27d00eSSakari Ailus 	return 0;
10583b27d00eSSakari Ailus }
10593b27d00eSSakari Ailus 
106067dcc26dSAndy Shevchenko static const void *
of_fwnode_device_get_match_data(const struct fwnode_handle * fwnode,const struct device * dev)10611c2c82eaSSinan Kaya of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
10621c2c82eaSSinan Kaya 				const struct device *dev)
10631c2c82eaSSinan Kaya {
106467dcc26dSAndy Shevchenko 	return of_device_get_match_data(dev);
10651c2c82eaSSinan Kaya }
10661c2c82eaSSinan Kaya 
of_link_to_phandle(struct device_node * con_np,struct device_node * sup_np)10674a032827SSaravana Kannan static void of_link_to_phandle(struct device_node *con_np,
10688a06d1eaSSaravana Kannan 			      struct device_node *sup_np)
1069a3e1d1a7SSaravana Kannan {
10704a032827SSaravana Kannan 	struct device_node *tmp_np = of_node_get(sup_np);
1071a3e1d1a7SSaravana Kannan 
10724a032827SSaravana Kannan 	/* Check that sup_np and its ancestors are available. */
10734a032827SSaravana Kannan 	while (tmp_np) {
10744a032827SSaravana Kannan 		if (of_fwnode_handle(tmp_np)->dev) {
10754a032827SSaravana Kannan 			of_node_put(tmp_np);
10764a032827SSaravana Kannan 			break;
1077a3e1d1a7SSaravana Kannan 		}
1078a3e1d1a7SSaravana Kannan 
10794a032827SSaravana Kannan 		if (!of_device_is_available(tmp_np)) {
10804a032827SSaravana Kannan 			of_node_put(tmp_np);
10814a032827SSaravana Kannan 			return;
1082a3e1d1a7SSaravana Kannan 		}
10838a06d1eaSSaravana Kannan 
10844a032827SSaravana Kannan 		tmp_np = of_get_next_parent(tmp_np);
1085ba861f8eSSaravana Kannan 	}
10868a06d1eaSSaravana Kannan 
10878a06d1eaSSaravana Kannan 	fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1088a3e1d1a7SSaravana Kannan }
1089a3e1d1a7SSaravana Kannan 
1090a3e1d1a7SSaravana Kannan /**
1091a3e1d1a7SSaravana Kannan  * parse_prop_cells - Property parsing function for suppliers
1092a3e1d1a7SSaravana Kannan  *
1093a3e1d1a7SSaravana Kannan  * @np:		Pointer to device tree node containing a list
1094a3e1d1a7SSaravana Kannan  * @prop_name:	Name of property to be parsed. Expected to hold phandle values
1095a3e1d1a7SSaravana Kannan  * @index:	For properties holding a list of phandles, this is the index
1096a3e1d1a7SSaravana Kannan  *		into the list.
1097a3e1d1a7SSaravana Kannan  * @list_name:	Property name that is known to contain list of phandle(s) to
1098a3e1d1a7SSaravana Kannan  *		supplier(s)
1099a3e1d1a7SSaravana Kannan  * @cells_name:	property name that specifies phandles' arguments count
1100a3e1d1a7SSaravana Kannan  *
1101a3e1d1a7SSaravana Kannan  * This is a helper function to parse properties that have a known fixed name
1102a3e1d1a7SSaravana Kannan  * and are a list of phandles and phandle arguments.
1103a3e1d1a7SSaravana Kannan  *
1104a3e1d1a7SSaravana Kannan  * Returns:
1105a3e1d1a7SSaravana Kannan  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1106a3e1d1a7SSaravana Kannan  *   on it when done.
1107a3e1d1a7SSaravana Kannan  * - NULL if no phandle found at index
1108a3e1d1a7SSaravana Kannan  */
parse_prop_cells(struct device_node * np,const char * prop_name,int index,const char * list_name,const char * cells_name)1109a3e1d1a7SSaravana Kannan static struct device_node *parse_prop_cells(struct device_node *np,
1110a3e1d1a7SSaravana Kannan 					    const char *prop_name, int index,
1111a3e1d1a7SSaravana Kannan 					    const char *list_name,
1112a3e1d1a7SSaravana Kannan 					    const char *cells_name)
1113a3e1d1a7SSaravana Kannan {
1114a3e1d1a7SSaravana Kannan 	struct of_phandle_args sup_args;
1115a3e1d1a7SSaravana Kannan 
1116a3e1d1a7SSaravana Kannan 	if (strcmp(prop_name, list_name))
1117a3e1d1a7SSaravana Kannan 		return NULL;
1118a3e1d1a7SSaravana Kannan 
1119ff24fed1SMichael Walle 	if (__of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
1120a3e1d1a7SSaravana Kannan 					 &sup_args))
1121a3e1d1a7SSaravana Kannan 		return NULL;
1122a3e1d1a7SSaravana Kannan 
1123a3e1d1a7SSaravana Kannan 	return sup_args.np;
1124a3e1d1a7SSaravana Kannan }
1125a3e1d1a7SSaravana Kannan 
1126a436ef4aSSaravana Kannan #define DEFINE_SIMPLE_PROP(fname, name, cells)				  \
1127a436ef4aSSaravana Kannan static struct device_node *parse_##fname(struct device_node *np,	  \
1128a436ef4aSSaravana Kannan 					const char *prop_name, int index) \
1129a436ef4aSSaravana Kannan {									  \
1130a436ef4aSSaravana Kannan 	return parse_prop_cells(np, prop_name, index, name, cells);	  \
1131a3e1d1a7SSaravana Kannan }
1132a3e1d1a7SSaravana Kannan 
strcmp_suffix(const char * str,const char * suffix)1133a3e1d1a7SSaravana Kannan static int strcmp_suffix(const char *str, const char *suffix)
1134a3e1d1a7SSaravana Kannan {
1135a3e1d1a7SSaravana Kannan 	unsigned int len, suffix_len;
1136a3e1d1a7SSaravana Kannan 
1137a3e1d1a7SSaravana Kannan 	len = strlen(str);
1138a3e1d1a7SSaravana Kannan 	suffix_len = strlen(suffix);
1139a3e1d1a7SSaravana Kannan 	if (len <= suffix_len)
1140a3e1d1a7SSaravana Kannan 		return -1;
1141a3e1d1a7SSaravana Kannan 	return strcmp(str + len - suffix_len, suffix);
1142a3e1d1a7SSaravana Kannan }
1143a3e1d1a7SSaravana Kannan 
1144a436ef4aSSaravana Kannan /**
1145a436ef4aSSaravana Kannan  * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1146a436ef4aSSaravana Kannan  *
1147a436ef4aSSaravana Kannan  * @np:		Pointer to device tree node containing a list
1148a436ef4aSSaravana Kannan  * @prop_name:	Name of property to be parsed. Expected to hold phandle values
1149a436ef4aSSaravana Kannan  * @index:	For properties holding a list of phandles, this is the index
1150a436ef4aSSaravana Kannan  *		into the list.
1151a436ef4aSSaravana Kannan  * @suffix:	Property suffix that is known to contain list of phandle(s) to
1152a436ef4aSSaravana Kannan  *		supplier(s)
1153a436ef4aSSaravana Kannan  * @cells_name:	property name that specifies phandles' arguments count
1154a436ef4aSSaravana Kannan  *
1155a436ef4aSSaravana Kannan  * This is a helper function to parse properties that have a known fixed suffix
1156a436ef4aSSaravana Kannan  * and are a list of phandles and phandle arguments.
1157a436ef4aSSaravana Kannan  *
1158a436ef4aSSaravana Kannan  * Returns:
1159a436ef4aSSaravana Kannan  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1160a436ef4aSSaravana Kannan  *   on it when done.
1161a436ef4aSSaravana Kannan  * - NULL if no phandle found at index
1162a436ef4aSSaravana Kannan  */
parse_suffix_prop_cells(struct device_node * np,const char * prop_name,int index,const char * suffix,const char * cells_name)1163a436ef4aSSaravana Kannan static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1164a436ef4aSSaravana Kannan 					    const char *prop_name, int index,
1165a436ef4aSSaravana Kannan 					    const char *suffix,
1166a436ef4aSSaravana Kannan 					    const char *cells_name)
1167a3e1d1a7SSaravana Kannan {
1168a436ef4aSSaravana Kannan 	struct of_phandle_args sup_args;
1169a436ef4aSSaravana Kannan 
1170a436ef4aSSaravana Kannan 	if (strcmp_suffix(prop_name, suffix))
1171a3e1d1a7SSaravana Kannan 		return NULL;
1172a3e1d1a7SSaravana Kannan 
1173a436ef4aSSaravana Kannan 	if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1174a436ef4aSSaravana Kannan 				       &sup_args))
1175a436ef4aSSaravana Kannan 		return NULL;
1176a436ef4aSSaravana Kannan 
1177a436ef4aSSaravana Kannan 	return sup_args.np;
1178a436ef4aSSaravana Kannan }
1179a436ef4aSSaravana Kannan 
1180a436ef4aSSaravana Kannan #define DEFINE_SUFFIX_PROP(fname, suffix, cells)			     \
1181a436ef4aSSaravana Kannan static struct device_node *parse_##fname(struct device_node *np,	     \
1182a436ef4aSSaravana Kannan 					const char *prop_name, int index)    \
1183a436ef4aSSaravana Kannan {									     \
1184a436ef4aSSaravana Kannan 	return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1185a3e1d1a7SSaravana Kannan }
1186a3e1d1a7SSaravana Kannan 
1187a3e1d1a7SSaravana Kannan /**
1188a3e1d1a7SSaravana Kannan  * struct supplier_bindings - Property parsing functions for suppliers
1189a3e1d1a7SSaravana Kannan  *
1190a3e1d1a7SSaravana Kannan  * @parse_prop: function name
1191a3e1d1a7SSaravana Kannan  *	parse_prop() finds the node corresponding to a supplier phandle
1192a3e1d1a7SSaravana Kannan  * @parse_prop.np: Pointer to device node holding supplier phandle property
1193a3e1d1a7SSaravana Kannan  * @parse_prop.prop_name: Name of property holding a phandle value
1194a3e1d1a7SSaravana Kannan  * @parse_prop.index: For properties holding a list of phandles, this is the
1195a3e1d1a7SSaravana Kannan  *		      index into the list
1196e0dce2c9SSaravana Kannan  * @get_con_dev: If the consumer node containing the property is never converted
1197e0dce2c9SSaravana Kannan  *		 to a struct device, implement this ops so fw_devlink can use it
1198e0dce2c9SSaravana Kannan  *		 to find the true consumer.
11993915fed9SLee Jones  * @optional: Describes whether a supplier is mandatory or not
1200a3e1d1a7SSaravana Kannan  *
1201a3e1d1a7SSaravana Kannan  * Returns:
1202a3e1d1a7SSaravana Kannan  * parse_prop() return values are
1203a3e1d1a7SSaravana Kannan  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1204a3e1d1a7SSaravana Kannan  *   on it when done.
1205a3e1d1a7SSaravana Kannan  * - NULL if no phandle found at index
1206a3e1d1a7SSaravana Kannan  */
1207a3e1d1a7SSaravana Kannan struct supplier_bindings {
1208a3e1d1a7SSaravana Kannan 	struct device_node *(*parse_prop)(struct device_node *np,
1209a3e1d1a7SSaravana Kannan 					  const char *prop_name, int index);
1210e0dce2c9SSaravana Kannan 	struct device_node *(*get_con_dev)(struct device_node *np);
1211a9dd8f3cSSaravana Kannan 	bool optional;
1212a3e1d1a7SSaravana Kannan };
1213a3e1d1a7SSaravana Kannan 
1214a436ef4aSSaravana Kannan DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1215a436ef4aSSaravana Kannan DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
12168e12257dSSaravana Kannan DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
12178e12257dSSaravana Kannan DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
121823429e2cSNuno Sa DEFINE_SIMPLE_PROP(io_channels, "io-channels", "#io-channel-cells")
12197f00be96SSaravana Kannan DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
12207f00be96SSaravana Kannan DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
12212f7afc34SSaravana Kannan DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
12222f7afc34SSaravana Kannan DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
122378056e70SSaravana Kannan DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1224e2d81720SMichael Walle DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", "#nvmem-cell-cells")
122553e6a671SSaravana Kannan DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
122653e6a671SSaravana Kannan DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1227fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1228fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1229fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1230fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1231fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1232fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1233fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1234fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1235fb820b49SSaravana Kannan DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
12366b2117adSSaravana Kannan DEFINE_SIMPLE_PROP(pwms, "pwms", "#pwm-cells")
12376b2117adSSaravana Kannan DEFINE_SIMPLE_PROP(resets, "resets", "#reset-cells")
123818c585c7SSaravana Kannan DEFINE_SIMPLE_PROP(leds, "leds", NULL)
123918c585c7SSaravana Kannan DEFINE_SIMPLE_PROP(backlight, "backlight", NULL)
1240fbf0ea2dSDouglas Anderson DEFINE_SIMPLE_PROP(panel, "panel", NULL)
1241a436ef4aSSaravana Kannan DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
12427f00be96SSaravana Kannan DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1243d473d32cSIlya Lipnitskiy 
parse_gpios(struct device_node * np,const char * prop_name,int index)1244d473d32cSIlya Lipnitskiy static struct device_node *parse_gpios(struct device_node *np,
1245d473d32cSIlya Lipnitskiy 				       const char *prop_name, int index)
1246d473d32cSIlya Lipnitskiy {
1247d473d32cSIlya Lipnitskiy 	if (!strcmp_suffix(prop_name, ",nr-gpios"))
1248d473d32cSIlya Lipnitskiy 		return NULL;
1249d473d32cSIlya Lipnitskiy 
1250d473d32cSIlya Lipnitskiy 	return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1251d473d32cSIlya Lipnitskiy 				       "#gpio-cells");
1252d473d32cSIlya Lipnitskiy }
1253a436ef4aSSaravana Kannan 
parse_iommu_maps(struct device_node * np,const char * prop_name,int index)1254e149573bSWill Deacon static struct device_node *parse_iommu_maps(struct device_node *np,
1255e149573bSWill Deacon 					    const char *prop_name, int index)
1256e149573bSWill Deacon {
1257e149573bSWill Deacon 	if (strcmp(prop_name, "iommu-map"))
1258e149573bSWill Deacon 		return NULL;
1259e149573bSWill Deacon 
1260e149573bSWill Deacon 	return of_parse_phandle(np, prop_name, (index * 4) + 1);
1261e149573bSWill Deacon }
1262e149573bSWill Deacon 
parse_gpio_compat(struct device_node * np,const char * prop_name,int index)1263e13f5b7aSSaravana Kannan static struct device_node *parse_gpio_compat(struct device_node *np,
1264e13f5b7aSSaravana Kannan 					     const char *prop_name, int index)
1265e13f5b7aSSaravana Kannan {
1266e13f5b7aSSaravana Kannan 	struct of_phandle_args sup_args;
1267e13f5b7aSSaravana Kannan 
1268e13f5b7aSSaravana Kannan 	if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1269e13f5b7aSSaravana Kannan 		return NULL;
1270e13f5b7aSSaravana Kannan 
1271e13f5b7aSSaravana Kannan 	/*
1272e13f5b7aSSaravana Kannan 	 * Ignore node with gpio-hog property since its gpios are all provided
1273e13f5b7aSSaravana Kannan 	 * by its parent.
1274e13f5b7aSSaravana Kannan 	 */
127516b0c7caSRob Herring 	if (of_property_read_bool(np, "gpio-hog"))
1276e13f5b7aSSaravana Kannan 		return NULL;
1277e13f5b7aSSaravana Kannan 
1278e13f5b7aSSaravana Kannan 	if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1279e13f5b7aSSaravana Kannan 				       &sup_args))
1280e13f5b7aSSaravana Kannan 		return NULL;
1281e13f5b7aSSaravana Kannan 
1282e13f5b7aSSaravana Kannan 	return sup_args.np;
1283e13f5b7aSSaravana Kannan }
1284e13f5b7aSSaravana Kannan 
parse_interrupts(struct device_node * np,const char * prop_name,int index)12854104ca77SSaravana Kannan static struct device_node *parse_interrupts(struct device_node *np,
12864104ca77SSaravana Kannan 					    const char *prop_name, int index)
12874104ca77SSaravana Kannan {
1288f265f06aSSaravana Kannan 	struct of_phandle_args sup_args;
1289f265f06aSSaravana Kannan 
1290bd6d617aSSaravana Kannan 	if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1291bd6d617aSSaravana Kannan 		return NULL;
1292bd6d617aSSaravana Kannan 
1293f265f06aSSaravana Kannan 	if (strcmp(prop_name, "interrupts") &&
1294f265f06aSSaravana Kannan 	    strcmp(prop_name, "interrupts-extended"))
12954104ca77SSaravana Kannan 		return NULL;
12964104ca77SSaravana Kannan 
1297f265f06aSSaravana Kannan 	return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
12984104ca77SSaravana Kannan }
12994104ca77SSaravana Kannan 
parse_remote_endpoint(struct device_node * np,const char * prop_name,int index)1300c3dc7cb8SSaravana Kannan static struct device_node *parse_remote_endpoint(struct device_node *np,
1301c3dc7cb8SSaravana Kannan 						 const char *prop_name,
1302c3dc7cb8SSaravana Kannan 						 int index)
1303c3dc7cb8SSaravana Kannan {
1304c3dc7cb8SSaravana Kannan 	/* Return NULL for index > 0 to signify end of remote-endpoints. */
1305*6f72b445SSaravana Kannan 	if (index > 0 || strcmp(prop_name, "remote-endpoint"))
1306c3dc7cb8SSaravana Kannan 		return NULL;
1307c3dc7cb8SSaravana Kannan 
1308c3dc7cb8SSaravana Kannan 	return of_graph_get_remote_port_parent(np);
1309c3dc7cb8SSaravana Kannan }
1310c3dc7cb8SSaravana Kannan 
1311af1b967aSSaravana Kannan static const struct supplier_bindings of_supplier_bindings[] = {
1312a3e1d1a7SSaravana Kannan 	{ .parse_prop = parse_clocks, },
1313a3e1d1a7SSaravana Kannan 	{ .parse_prop = parse_interconnects, },
1314a9dd8f3cSSaravana Kannan 	{ .parse_prop = parse_iommus, .optional = true, },
1315a9dd8f3cSSaravana Kannan 	{ .parse_prop = parse_iommu_maps, .optional = true, },
13168e12257dSSaravana Kannan 	{ .parse_prop = parse_mboxes, },
13178e12257dSSaravana Kannan 	{ .parse_prop = parse_io_channels, },
13187f00be96SSaravana Kannan 	{ .parse_prop = parse_interrupt_parent, },
1319a9dd8f3cSSaravana Kannan 	{ .parse_prop = parse_dmas, .optional = true, },
13202f7afc34SSaravana Kannan 	{ .parse_prop = parse_power_domains, },
13212f7afc34SSaravana Kannan 	{ .parse_prop = parse_hwlocks, },
132278056e70SSaravana Kannan 	{ .parse_prop = parse_extcon, },
132353e6a671SSaravana Kannan 	{ .parse_prop = parse_nvmem_cells, },
132453e6a671SSaravana Kannan 	{ .parse_prop = parse_phys, },
132553e6a671SSaravana Kannan 	{ .parse_prop = parse_wakeup_parent, },
1326fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl0, },
1327fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl1, },
1328fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl2, },
1329fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl3, },
1330fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl4, },
1331fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl5, },
1332fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl6, },
1333fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl7, },
1334fb820b49SSaravana Kannan 	{ .parse_prop = parse_pinctrl8, },
1335e0dce2c9SSaravana Kannan 	{
1336e0dce2c9SSaravana Kannan 		.parse_prop = parse_remote_endpoint,
1337e0dce2c9SSaravana Kannan 		.get_con_dev = of_graph_get_port_parent,
1338e0dce2c9SSaravana Kannan 	},
13396b2117adSSaravana Kannan 	{ .parse_prop = parse_pwms, },
13406b2117adSSaravana Kannan 	{ .parse_prop = parse_resets, },
134118c585c7SSaravana Kannan 	{ .parse_prop = parse_leds, },
134218c585c7SSaravana Kannan 	{ .parse_prop = parse_backlight, },
1343fbf0ea2dSDouglas Anderson 	{ .parse_prop = parse_panel, },
1344e13f5b7aSSaravana Kannan 	{ .parse_prop = parse_gpio_compat, },
13454104ca77SSaravana Kannan 	{ .parse_prop = parse_interrupts, },
1346a3e1d1a7SSaravana Kannan 	{ .parse_prop = parse_regulators, },
13477f00be96SSaravana Kannan 	{ .parse_prop = parse_gpio, },
13487f00be96SSaravana Kannan 	{ .parse_prop = parse_gpios, },
1349af1b967aSSaravana Kannan 	{}
1350a3e1d1a7SSaravana Kannan };
1351a3e1d1a7SSaravana Kannan 
1352a3e1d1a7SSaravana Kannan /**
1353a3e1d1a7SSaravana Kannan  * of_link_property - Create device links to suppliers listed in a property
1354a3e1d1a7SSaravana Kannan  * @con_np: The consumer device tree node which contains the property
1355a3e1d1a7SSaravana Kannan  * @prop_name: Name of property to be parsed
1356a3e1d1a7SSaravana Kannan  *
1357a3e1d1a7SSaravana Kannan  * This function checks if the property @prop_name that is present in the
1358a3e1d1a7SSaravana Kannan  * @con_np device tree node is one of the known common device tree bindings
1359a3e1d1a7SSaravana Kannan  * that list phandles to suppliers. If @prop_name isn't one, this function
1360a3e1d1a7SSaravana Kannan  * doesn't do anything.
1361a3e1d1a7SSaravana Kannan  *
13628a06d1eaSSaravana Kannan  * If @prop_name is one, this function attempts to create fwnode links from the
13638a06d1eaSSaravana Kannan  * consumer device tree node @con_np to all the suppliers device tree nodes
13648a06d1eaSSaravana Kannan  * listed in @prop_name.
1365a3e1d1a7SSaravana Kannan  *
13668a06d1eaSSaravana Kannan  * Any failed attempt to create a fwnode link will NOT result in an immediate
1367a3e1d1a7SSaravana Kannan  * return.  of_link_property() must create links to all the available supplier
13688a06d1eaSSaravana Kannan  * device tree nodes even when attempts to create a link to one or more
13698a06d1eaSSaravana Kannan  * suppliers fail.
1370a3e1d1a7SSaravana Kannan  */
of_link_property(struct device_node * con_np,const char * prop_name)13718a06d1eaSSaravana Kannan static int of_link_property(struct device_node *con_np, const char *prop_name)
1372a3e1d1a7SSaravana Kannan {
1373a3e1d1a7SSaravana Kannan 	struct device_node *phandle;
1374af1b967aSSaravana Kannan 	const struct supplier_bindings *s = of_supplier_bindings;
1375a3e1d1a7SSaravana Kannan 	unsigned int i = 0;
1376a3e1d1a7SSaravana Kannan 	bool matched = false;
1377a3e1d1a7SSaravana Kannan 
1378a3e1d1a7SSaravana Kannan 	/* Do not stop at first failed link, link all available suppliers. */
1379a3e1d1a7SSaravana Kannan 	while (!matched && s->parse_prop) {
1380a9dd8f3cSSaravana Kannan 		if (s->optional && !fw_devlink_is_strict()) {
1381a9dd8f3cSSaravana Kannan 			s++;
1382a9dd8f3cSSaravana Kannan 			continue;
1383a9dd8f3cSSaravana Kannan 		}
1384a9dd8f3cSSaravana Kannan 
1385a3e1d1a7SSaravana Kannan 		while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1386f7514a66SSaravana Kannan 			struct device_node *con_dev_np;
1387f7514a66SSaravana Kannan 
1388e0dce2c9SSaravana Kannan 			con_dev_np = s->get_con_dev
1389e0dce2c9SSaravana Kannan 					? s->get_con_dev(con_np)
1390f7514a66SSaravana Kannan 					: of_node_get(con_np);
1391a3e1d1a7SSaravana Kannan 			matched = true;
1392a3e1d1a7SSaravana Kannan 			i++;
1393f7514a66SSaravana Kannan 			of_link_to_phandle(con_dev_np, phandle);
1394a3e1d1a7SSaravana Kannan 			of_node_put(phandle);
1395f7514a66SSaravana Kannan 			of_node_put(con_dev_np);
1396a3e1d1a7SSaravana Kannan 		}
1397a3e1d1a7SSaravana Kannan 		s++;
1398a3e1d1a7SSaravana Kannan 	}
1399065cac6cSYang Li 	return 0;
1400a3e1d1a7SSaravana Kannan }
1401a3e1d1a7SSaravana Kannan 
of_fwnode_iomap(struct fwnode_handle * fwnode,int index)140268b979d0SSakari Ailus static void __iomem *of_fwnode_iomap(struct fwnode_handle *fwnode, int index)
140368b979d0SSakari Ailus {
140468b979d0SSakari Ailus #ifdef CONFIG_OF_ADDRESS
140568b979d0SSakari Ailus 	return of_iomap(to_of_node(fwnode), index);
140668b979d0SSakari Ailus #else
140768b979d0SSakari Ailus 	return NULL;
140868b979d0SSakari Ailus #endif
140968b979d0SSakari Ailus }
141068b979d0SSakari Ailus 
of_fwnode_irq_get(const struct fwnode_handle * fwnode,unsigned int index)141199c63707SSakari Ailus static int of_fwnode_irq_get(const struct fwnode_handle *fwnode,
141299c63707SSakari Ailus 			     unsigned int index)
141399c63707SSakari Ailus {
141499c63707SSakari Ailus 	return of_irq_get(to_of_node(fwnode), index);
141599c63707SSakari Ailus }
141699c63707SSakari Ailus 
of_fwnode_add_links(struct fwnode_handle * fwnode)14172d09e6ebSSaravana Kannan static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1418a3e1d1a7SSaravana Kannan {
14198a06d1eaSSaravana Kannan 	struct property *p;
14208a06d1eaSSaravana Kannan 	struct device_node *con_np = to_of_node(fwnode);
1421a3e1d1a7SSaravana Kannan 
14224a48b66bSSaravana Kannan 	if (IS_ENABLED(CONFIG_X86))
14234a48b66bSSaravana Kannan 		return 0;
14244a48b66bSSaravana Kannan 
14258a06d1eaSSaravana Kannan 	if (!con_np)
14268a06d1eaSSaravana Kannan 		return -EINVAL;
14278a06d1eaSSaravana Kannan 
14288a06d1eaSSaravana Kannan 	for_each_property_of_node(con_np, p)
14298a06d1eaSSaravana Kannan 		of_link_property(con_np, p->name);
14308a06d1eaSSaravana Kannan 
14318a06d1eaSSaravana Kannan 	return 0;
1432a3e1d1a7SSaravana Kannan }
1433a3e1d1a7SSaravana Kannan 
14343708184aSSakari Ailus const struct fwnode_operations of_fwnode_ops = {
14353708184aSSakari Ailus 	.get = of_fwnode_get,
14363708184aSSakari Ailus 	.put = of_fwnode_put,
14372294b3afSSakari Ailus 	.device_is_available = of_fwnode_device_is_available,
14381c2c82eaSSinan Kaya 	.device_get_match_data = of_fwnode_device_get_match_data,
14398c756a0aSSakari Ailus 	.device_dma_supported = of_fwnode_device_dma_supported,
14408c756a0aSSakari Ailus 	.device_get_dma_attr = of_fwnode_device_get_dma_attr,
14413708184aSSakari Ailus 	.property_present = of_fwnode_property_present,
14423708184aSSakari Ailus 	.property_read_int_array = of_fwnode_property_read_int_array,
14433708184aSSakari Ailus 	.property_read_string_array = of_fwnode_property_read_string_array,
1444bc0500c1SSakari Ailus 	.get_name = of_fwnode_get_name,
1445e7e242bcSSakari Ailus 	.get_name_prefix = of_fwnode_get_name_prefix,
14463708184aSSakari Ailus 	.get_parent = of_fwnode_get_parent,
14473708184aSSakari Ailus 	.get_next_child_node = of_fwnode_get_next_child_node,
14483708184aSSakari Ailus 	.get_named_child_node = of_fwnode_get_named_child_node,
14493e3119d3SSakari Ailus 	.get_reference_args = of_fwnode_get_reference_args,
14503b27d00eSSakari Ailus 	.graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
14513b27d00eSSakari Ailus 	.graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
14523b27d00eSSakari Ailus 	.graph_get_port_parent = of_fwnode_graph_get_port_parent,
14533b27d00eSSakari Ailus 	.graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
145468b979d0SSakari Ailus 	.iomap = of_fwnode_iomap,
145599c63707SSakari Ailus 	.irq_get = of_fwnode_irq_get,
1456a3e1d1a7SSaravana Kannan 	.add_links = of_fwnode_add_links,
14573708184aSSakari Ailus };
1458db3e50f3SSakari Ailus EXPORT_SYMBOL_GPL(of_fwnode_ops);
1459