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