Linux Audio

Check our new training course

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