Linux Audio

Check our new training course

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