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