Linux Audio

Check our new training course

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