1 /* 2 * drivers/of/property.c - Procedures for accessing and interpreting 3 * Devicetree properties and graphs. 4 * 5 * Initially created by copying procedures from drivers/of/base.c. This 6 * file contains the OF property as well as the OF graph interface 7 * functions. 8 * 9 * Paul Mackerras August 1996. 10 * Copyright (C) 1996-2005 Paul Mackerras. 11 * 12 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. 13 * {engebret|bergner}@us.ibm.com 14 * 15 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net 16 * 17 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and 18 * Grant Likely. 19 * 20 * This program is free software; you can redistribute it and/or 21 * modify it under the terms of the GNU General Public License 22 * as published by the Free Software Foundation; either version 23 * 2 of the License, or (at your option) any later version. 24 */ 25 26 #define pr_fmt(fmt) "OF: " fmt 27 28 #include <linux/of.h> 29 #include <linux/of_device.h> 30 #include <linux/of_graph.h> 31 #include <linux/string.h> 32 33 #include "of_private.h" 34 35 /** 36 * of_property_count_elems_of_size - Count the number of elements in a property 37 * 38 * @np: device node from which the property value is to be read. 39 * @propname: name of the property to be searched. 40 * @elem_size: size of the individual element 41 * 42 * Search for a property in a device node and count the number of elements of 43 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the 44 * property does not exist or its length does not match a multiple of elem_size 45 * and -ENODATA if the property does not have a value. 46 */ 47 int of_property_count_elems_of_size(const struct device_node *np, 48 const char *propname, int elem_size) 49 { 50 struct property *prop = of_find_property(np, propname, NULL); 51 52 if (!prop) 53 return -EINVAL; 54 if (!prop->value) 55 return -ENODATA; 56 57 if (prop->length % elem_size != 0) { 58 pr_err("size of %s in node %pOF is not a multiple of %d\n", 59 propname, np, elem_size); 60 return -EINVAL; 61 } 62 63 return prop->length / elem_size; 64 } 65 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size); 66 67 /** 68 * of_find_property_value_of_size 69 * 70 * @np: device node from which the property value is to be read. 71 * @propname: name of the property to be searched. 72 * @min: minimum allowed length of property value 73 * @max: maximum allowed length of property value (0 means unlimited) 74 * @len: if !=NULL, actual length is written to here 75 * 76 * Search for a property in a device node and valid the requested size. 77 * Returns the property value on success, -EINVAL if the property does not 78 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the 79 * property data is too small or too large. 80 * 81 */ 82 static void *of_find_property_value_of_size(const struct device_node *np, 83 const char *propname, u32 min, u32 max, size_t *len) 84 { 85 struct property *prop = of_find_property(np, propname, NULL); 86 87 if (!prop) 88 return ERR_PTR(-EINVAL); 89 if (!prop->value) 90 return ERR_PTR(-ENODATA); 91 if (prop->length < min) 92 return ERR_PTR(-EOVERFLOW); 93 if (max && prop->length > max) 94 return ERR_PTR(-EOVERFLOW); 95 96 if (len) 97 *len = prop->length; 98 99 return prop->value; 100 } 101 102 /** 103 * of_property_read_u32_index - Find and read a u32 from a multi-value property. 104 * 105 * @np: device node from which the property value is to be read. 106 * @propname: name of the property to be searched. 107 * @index: index of the u32 in the list of values 108 * @out_value: pointer to return value, modified only if no error. 109 * 110 * Search for a property in a device node and read nth 32-bit value from 111 * it. Returns 0 on success, -EINVAL if the property does not exist, 112 * -ENODATA if property does not have a value, and -EOVERFLOW if the 113 * property data isn't large enough. 114 * 115 * The out_value is modified only if a valid u32 value can be decoded. 116 */ 117 int of_property_read_u32_index(const struct device_node *np, 118 const char *propname, 119 u32 index, u32 *out_value) 120 { 121 const u32 *val = of_find_property_value_of_size(np, propname, 122 ((index + 1) * sizeof(*out_value)), 123 0, 124 NULL); 125 126 if (IS_ERR(val)) 127 return PTR_ERR(val); 128 129 *out_value = be32_to_cpup(((__be32 *)val) + index); 130 return 0; 131 } 132 EXPORT_SYMBOL_GPL(of_property_read_u32_index); 133 134 /** 135 * of_property_read_u64_index - Find and read a u64 from a multi-value property. 136 * 137 * @np: device node from which the property value is to be read. 138 * @propname: name of the property to be searched. 139 * @index: index of the u64 in the list of values 140 * @out_value: pointer to return value, modified only if no error. 141 * 142 * Search for a property in a device node and read nth 64-bit value from 143 * it. Returns 0 on success, -EINVAL if the property does not exist, 144 * -ENODATA if property does not have a value, and -EOVERFLOW if the 145 * property data isn't large enough. 146 * 147 * The out_value is modified only if a valid u64 value can be decoded. 148 */ 149 int of_property_read_u64_index(const struct device_node *np, 150 const char *propname, 151 u32 index, u64 *out_value) 152 { 153 const u64 *val = of_find_property_value_of_size(np, propname, 154 ((index + 1) * sizeof(*out_value)), 155 0, NULL); 156 157 if (IS_ERR(val)) 158 return PTR_ERR(val); 159 160 *out_value = be64_to_cpup(((__be64 *)val) + index); 161 return 0; 162 } 163 EXPORT_SYMBOL_GPL(of_property_read_u64_index); 164 165 /** 166 * of_property_read_variable_u8_array - Find and read an array of u8 from a 167 * property, with bounds on the minimum and maximum array size. 168 * 169 * @np: device node from which the property value is to be read. 170 * @propname: name of the property to be searched. 171 * @out_values: pointer to return value, modified only if return value is 0. 172 * @sz_min: minimum number of array elements to read 173 * @sz_max: maximum number of array elements to read, if zero there is no 174 * upper limit on the number of elements in the dts entry but only 175 * sz_min will be read. 176 * 177 * Search for a property in a device node and read 8-bit value(s) from 178 * it. Returns number of elements read on success, -EINVAL if the property 179 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW 180 * if the property data is smaller than sz_min or longer than sz_max. 181 * 182 * dts entry of array should be like: 183 * property = /bits/ 8 <0x50 0x60 0x70>; 184 * 185 * The out_values is modified only if a valid u8 value can be decoded. 186 */ 187 int of_property_read_variable_u8_array(const struct device_node *np, 188 const char *propname, u8 *out_values, 189 size_t sz_min, size_t sz_max) 190 { 191 size_t sz, count; 192 const u8 *val = of_find_property_value_of_size(np, propname, 193 (sz_min * sizeof(*out_values)), 194 (sz_max * sizeof(*out_values)), 195 &sz); 196 197 if (IS_ERR(val)) 198 return PTR_ERR(val); 199 200 if (!sz_max) 201 sz = sz_min; 202 else 203 sz /= sizeof(*out_values); 204 205 count = sz; 206 while (count--) 207 *out_values++ = *val++; 208 209 return sz; 210 } 211 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array); 212 213 /** 214 * of_property_read_variable_u16_array - Find and read an array of u16 from a 215 * property, with bounds on the minimum and maximum array size. 216 * 217 * @np: device node from which the property value is to be read. 218 * @propname: name of the property to be searched. 219 * @out_values: pointer to return value, modified only if return value is 0. 220 * @sz_min: minimum number of array elements to read 221 * @sz_max: maximum number of array elements to read, if zero there is no 222 * upper limit on the number of elements in the dts entry but only 223 * sz_min will be read. 224 * 225 * Search for a property in a device node and read 16-bit value(s) from 226 * it. Returns number of elements read on success, -EINVAL if the property 227 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW 228 * if the property data is smaller than sz_min or longer than sz_max. 229 * 230 * dts entry of array should be like: 231 * property = /bits/ 16 <0x5000 0x6000 0x7000>; 232 * 233 * The out_values is modified only if a valid u16 value can be decoded. 234 */ 235 int of_property_read_variable_u16_array(const struct device_node *np, 236 const char *propname, u16 *out_values, 237 size_t sz_min, size_t sz_max) 238 { 239 size_t sz, count; 240 const __be16 *val = of_find_property_value_of_size(np, propname, 241 (sz_min * sizeof(*out_values)), 242 (sz_max * sizeof(*out_values)), 243 &sz); 244 245 if (IS_ERR(val)) 246 return PTR_ERR(val); 247 248 if (!sz_max) 249 sz = sz_min; 250 else 251 sz /= sizeof(*out_values); 252 253 count = sz; 254 while (count--) 255 *out_values++ = be16_to_cpup(val++); 256 257 return sz; 258 } 259 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array); 260 261 /** 262 * of_property_read_variable_u32_array - Find and read an array of 32 bit 263 * integers from a property, with bounds on the minimum and maximum array size. 264 * 265 * @np: device node from which the property value is to be read. 266 * @propname: name of the property to be searched. 267 * @out_values: pointer to return value, modified only if return value is 0. 268 * @sz_min: minimum number of array elements to read 269 * @sz_max: maximum number of array elements to read, if zero there is no 270 * upper limit on the number of elements in the dts entry but only 271 * sz_min will be read. 272 * 273 * Search for a property in a device node and read 32-bit value(s) from 274 * it. Returns number of elements read on success, -EINVAL if the property 275 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW 276 * if the property data is smaller than sz_min or longer than sz_max. 277 * 278 * The out_values is modified only if a valid u32 value can be decoded. 279 */ 280 int of_property_read_variable_u32_array(const struct device_node *np, 281 const char *propname, u32 *out_values, 282 size_t sz_min, size_t sz_max) 283 { 284 size_t sz, count; 285 const __be32 *val = of_find_property_value_of_size(np, propname, 286 (sz_min * sizeof(*out_values)), 287 (sz_max * sizeof(*out_values)), 288 &sz); 289 290 if (IS_ERR(val)) 291 return PTR_ERR(val); 292 293 if (!sz_max) 294 sz = sz_min; 295 else 296 sz /= sizeof(*out_values); 297 298 count = sz; 299 while (count--) 300 *out_values++ = be32_to_cpup(val++); 301 302 return sz; 303 } 304 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array); 305 306 /** 307 * of_property_read_u64 - Find and read a 64 bit integer from a property 308 * @np: device node from which the property value is to be read. 309 * @propname: name of the property to be searched. 310 * @out_value: pointer to return value, modified only if return value is 0. 311 * 312 * Search for a property in a device node and read a 64-bit value from 313 * it. Returns 0 on success, -EINVAL if the property does not exist, 314 * -ENODATA if property does not have a value, and -EOVERFLOW if the 315 * property data isn't large enough. 316 * 317 * The out_value is modified only if a valid u64 value can be decoded. 318 */ 319 int of_property_read_u64(const struct device_node *np, const char *propname, 320 u64 *out_value) 321 { 322 const __be32 *val = of_find_property_value_of_size(np, propname, 323 sizeof(*out_value), 324 0, 325 NULL); 326 327 if (IS_ERR(val)) 328 return PTR_ERR(val); 329 330 *out_value = of_read_number(val, 2); 331 return 0; 332 } 333 EXPORT_SYMBOL_GPL(of_property_read_u64); 334 335 /** 336 * of_property_read_variable_u64_array - Find and read an array of 64 bit 337 * integers from a property, with bounds on the minimum and maximum array size. 338 * 339 * @np: device node from which the property value is to be read. 340 * @propname: name of the property to be searched. 341 * @out_values: pointer to return value, modified only if return value is 0. 342 * @sz_min: minimum number of array elements to read 343 * @sz_max: maximum number of array elements to read, if zero there is no 344 * upper limit on the number of elements in the dts entry but only 345 * sz_min will be read. 346 * 347 * Search for a property in a device node and read 64-bit value(s) from 348 * it. Returns number of elements read on success, -EINVAL if the property 349 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW 350 * if the property data is smaller than sz_min or longer than sz_max. 351 * 352 * The out_values is modified only if a valid u64 value can be decoded. 353 */ 354 int of_property_read_variable_u64_array(const struct device_node *np, 355 const char *propname, u64 *out_values, 356 size_t sz_min, size_t sz_max) 357 { 358 size_t sz, count; 359 const __be32 *val = of_find_property_value_of_size(np, propname, 360 (sz_min * sizeof(*out_values)), 361 (sz_max * sizeof(*out_values)), 362 &sz); 363 364 if (IS_ERR(val)) 365 return PTR_ERR(val); 366 367 if (!sz_max) 368 sz = sz_min; 369 else 370 sz /= sizeof(*out_values); 371 372 count = sz; 373 while (count--) { 374 *out_values++ = of_read_number(val, 2); 375 val += 2; 376 } 377 378 return sz; 379 } 380 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array); 381 382 /** 383 * of_property_read_string - Find and read a string from a property 384 * @np: device node from which the property value is to be read. 385 * @propname: name of the property to be searched. 386 * @out_string: pointer to null terminated return string, modified only if 387 * return value is 0. 388 * 389 * Search for a property in a device tree node and retrieve a null 390 * terminated string value (pointer to data, not a copy). Returns 0 on 391 * success, -EINVAL if the property does not exist, -ENODATA if property 392 * does not have a value, and -EILSEQ if the string is not null-terminated 393 * within the length of the property data. 394 * 395 * The out_string pointer is modified only if a valid string can be decoded. 396 */ 397 int of_property_read_string(const struct device_node *np, const char *propname, 398 const char **out_string) 399 { 400 const struct property *prop = of_find_property(np, propname, NULL); 401 if (!prop) 402 return -EINVAL; 403 if (!prop->value) 404 return -ENODATA; 405 if (strnlen(prop->value, prop->length) >= prop->length) 406 return -EILSEQ; 407 *out_string = prop->value; 408 return 0; 409 } 410 EXPORT_SYMBOL_GPL(of_property_read_string); 411 412 /** 413 * of_property_match_string() - Find string in a list and return index 414 * @np: pointer to node containing string list property 415 * @propname: string list property name 416 * @string: pointer to string to search for in string list 417 * 418 * This function searches a string list property and returns the index 419 * of a specific string value. 420 */ 421 int of_property_match_string(const struct device_node *np, const char *propname, 422 const char *string) 423 { 424 const struct property *prop = of_find_property(np, propname, NULL); 425 size_t l; 426 int i; 427 const char *p, *end; 428 429 if (!prop) 430 return -EINVAL; 431 if (!prop->value) 432 return -ENODATA; 433 434 p = prop->value; 435 end = p + prop->length; 436 437 for (i = 0; p < end; i++, p += l) { 438 l = strnlen(p, end - p) + 1; 439 if (p + l > end) 440 return -EILSEQ; 441 pr_debug("comparing %s with %s\n", string, p); 442 if (strcmp(string, p) == 0) 443 return i; /* Found it; return index */ 444 } 445 return -ENODATA; 446 } 447 EXPORT_SYMBOL_GPL(of_property_match_string); 448 449 /** 450 * of_property_read_string_helper() - Utility helper for parsing string properties 451 * @np: device node from which the property value is to be read. 452 * @propname: name of the property to be searched. 453 * @out_strs: output array of string pointers. 454 * @sz: number of array elements to read. 455 * @skip: Number of strings to skip over at beginning of list. 456 * 457 * Don't call this function directly. It is a utility helper for the 458 * of_property_read_string*() family of functions. 459 */ 460 int of_property_read_string_helper(const struct device_node *np, 461 const char *propname, const char **out_strs, 462 size_t sz, int skip) 463 { 464 const struct property *prop = of_find_property(np, propname, NULL); 465 int l = 0, i = 0; 466 const char *p, *end; 467 468 if (!prop) 469 return -EINVAL; 470 if (!prop->value) 471 return -ENODATA; 472 p = prop->value; 473 end = p + prop->length; 474 475 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) { 476 l = strnlen(p, end - p) + 1; 477 if (p + l > end) 478 return -EILSEQ; 479 if (out_strs && i >= skip) 480 *out_strs++ = p; 481 } 482 i -= skip; 483 return i <= 0 ? -ENODATA : i; 484 } 485 EXPORT_SYMBOL_GPL(of_property_read_string_helper); 486 487 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur, 488 u32 *pu) 489 { 490 const void *curv = cur; 491 492 if (!prop) 493 return NULL; 494 495 if (!cur) { 496 curv = prop->value; 497 goto out_val; 498 } 499 500 curv += sizeof(*cur); 501 if (curv >= prop->value + prop->length) 502 return NULL; 503 504 out_val: 505 *pu = be32_to_cpup(curv); 506 return curv; 507 } 508 EXPORT_SYMBOL_GPL(of_prop_next_u32); 509 510 const char *of_prop_next_string(struct property *prop, const char *cur) 511 { 512 const void *curv = cur; 513 514 if (!prop) 515 return NULL; 516 517 if (!cur) 518 return prop->value; 519 520 curv += strlen(cur) + 1; 521 if (curv >= prop->value + prop->length) 522 return NULL; 523 524 return curv; 525 } 526 EXPORT_SYMBOL_GPL(of_prop_next_string); 527 528 /** 529 * of_graph_parse_endpoint() - parse common endpoint node properties 530 * @node: pointer to endpoint device_node 531 * @endpoint: pointer to the OF endpoint data structure 532 * 533 * The caller should hold a reference to @node. 534 */ 535 int of_graph_parse_endpoint(const struct device_node *node, 536 struct of_endpoint *endpoint) 537 { 538 struct device_node *port_node = of_get_parent(node); 539 540 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n", 541 __func__, node); 542 543 memset(endpoint, 0, sizeof(*endpoint)); 544 545 endpoint->local_node = node; 546 /* 547 * It doesn't matter whether the two calls below succeed. 548 * If they don't then the default value 0 is used. 549 */ 550 of_property_read_u32(port_node, "reg", &endpoint->port); 551 of_property_read_u32(node, "reg", &endpoint->id); 552 553 of_node_put(port_node); 554 555 return 0; 556 } 557 EXPORT_SYMBOL(of_graph_parse_endpoint); 558 559 /** 560 * of_graph_get_port_by_id() - get the port matching a given id 561 * @parent: pointer to the parent device node 562 * @id: id of the port 563 * 564 * Return: A 'port' node pointer with refcount incremented. The caller 565 * has to use of_node_put() on it when done. 566 */ 567 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id) 568 { 569 struct device_node *node, *port; 570 571 node = of_get_child_by_name(parent, "ports"); 572 if (node) 573 parent = node; 574 575 for_each_child_of_node(parent, port) { 576 u32 port_id = 0; 577 578 if (of_node_cmp(port->name, "port") != 0) 579 continue; 580 of_property_read_u32(port, "reg", &port_id); 581 if (id == port_id) 582 break; 583 } 584 585 of_node_put(node); 586 587 return port; 588 } 589 EXPORT_SYMBOL(of_graph_get_port_by_id); 590 591 /** 592 * of_graph_get_next_endpoint() - get next endpoint node 593 * @parent: pointer to the parent device node 594 * @prev: previous endpoint node, or NULL to get first 595 * 596 * Return: An 'endpoint' node pointer with refcount incremented. Refcount 597 * of the passed @prev node is decremented. 598 */ 599 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent, 600 struct device_node *prev) 601 { 602 struct device_node *endpoint; 603 struct device_node *port; 604 605 if (!parent) 606 return NULL; 607 608 /* 609 * Start by locating the port node. If no previous endpoint is specified 610 * search for the first port node, otherwise get the previous endpoint 611 * parent port node. 612 */ 613 if (!prev) { 614 struct device_node *node; 615 616 node = of_get_child_by_name(parent, "ports"); 617 if (node) 618 parent = node; 619 620 port = of_get_child_by_name(parent, "port"); 621 of_node_put(node); 622 623 if (!port) { 624 pr_err("graph: no port node found in %pOF\n", parent); 625 return NULL; 626 } 627 } else { 628 port = of_get_parent(prev); 629 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n", 630 __func__, prev)) 631 return NULL; 632 } 633 634 while (1) { 635 /* 636 * Now that we have a port node, get the next endpoint by 637 * getting the next child. If the previous endpoint is NULL this 638 * will return the first child. 639 */ 640 endpoint = of_get_next_child(port, prev); 641 if (endpoint) { 642 of_node_put(port); 643 return endpoint; 644 } 645 646 /* No more endpoints under this port, try the next one. */ 647 prev = NULL; 648 649 do { 650 port = of_get_next_child(parent, port); 651 if (!port) 652 return NULL; 653 } while (of_node_cmp(port->name, "port")); 654 } 655 } 656 EXPORT_SYMBOL(of_graph_get_next_endpoint); 657 658 /** 659 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers 660 * @parent: pointer to the parent device node 661 * @port_reg: identifier (value of reg property) of the parent port node 662 * @reg: identifier (value of reg property) of the endpoint node 663 * 664 * Return: An 'endpoint' node pointer which is identified by reg and at the same 665 * is the child of a port node identified by port_reg. reg and port_reg are 666 * ignored when they are -1. 667 */ 668 struct device_node *of_graph_get_endpoint_by_regs( 669 const struct device_node *parent, int port_reg, int reg) 670 { 671 struct of_endpoint endpoint; 672 struct device_node *node = NULL; 673 674 for_each_endpoint_of_node(parent, node) { 675 of_graph_parse_endpoint(node, &endpoint); 676 if (((port_reg == -1) || (endpoint.port == port_reg)) && 677 ((reg == -1) || (endpoint.id == reg))) 678 return node; 679 } 680 681 return NULL; 682 } 683 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs); 684 685 /** 686 * of_graph_get_remote_endpoint() - get remote endpoint node 687 * @node: pointer to a local endpoint device_node 688 * 689 * Return: Remote endpoint node associated with remote endpoint node linked 690 * to @node. Use of_node_put() on it when done. 691 */ 692 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node) 693 { 694 /* Get remote endpoint node. */ 695 return of_parse_phandle(node, "remote-endpoint", 0); 696 } 697 EXPORT_SYMBOL(of_graph_get_remote_endpoint); 698 699 /** 700 * of_graph_get_port_parent() - get port's parent node 701 * @node: pointer to a local endpoint device_node 702 * 703 * Return: device node associated with endpoint node linked 704 * to @node. Use of_node_put() on it when done. 705 */ 706 struct device_node *of_graph_get_port_parent(struct device_node *node) 707 { 708 unsigned int depth; 709 710 /* Walk 3 levels up only if there is 'ports' node. */ 711 for (depth = 3; depth && node; depth--) { 712 node = of_get_next_parent(node); 713 if (depth == 2 && of_node_cmp(node->name, "ports")) 714 break; 715 } 716 return node; 717 } 718 EXPORT_SYMBOL(of_graph_get_port_parent); 719 720 /** 721 * of_graph_get_remote_port_parent() - get remote port's parent node 722 * @node: pointer to a local endpoint device_node 723 * 724 * Return: Remote device node associated with remote endpoint node linked 725 * to @node. Use of_node_put() on it when done. 726 */ 727 struct device_node *of_graph_get_remote_port_parent( 728 const struct device_node *node) 729 { 730 struct device_node *np; 731 732 /* Get remote endpoint node. */ 733 np = of_graph_get_remote_endpoint(node); 734 735 return of_graph_get_port_parent(np); 736 } 737 EXPORT_SYMBOL(of_graph_get_remote_port_parent); 738 739 /** 740 * of_graph_get_remote_port() - get remote port node 741 * @node: pointer to a local endpoint device_node 742 * 743 * Return: Remote port node associated with remote endpoint node linked 744 * to @node. Use of_node_put() on it when done. 745 */ 746 struct device_node *of_graph_get_remote_port(const struct device_node *node) 747 { 748 struct device_node *np; 749 750 /* Get remote endpoint node. */ 751 np = of_graph_get_remote_endpoint(node); 752 if (!np) 753 return NULL; 754 return of_get_next_parent(np); 755 } 756 EXPORT_SYMBOL(of_graph_get_remote_port); 757 758 int of_graph_get_endpoint_count(const struct device_node *np) 759 { 760 struct device_node *endpoint; 761 int num = 0; 762 763 for_each_endpoint_of_node(np, endpoint) 764 num++; 765 766 return num; 767 } 768 EXPORT_SYMBOL(of_graph_get_endpoint_count); 769 770 /** 771 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint 772 * @node: pointer to parent device_node containing graph port/endpoint 773 * @port: identifier (value of reg property) of the parent port node 774 * @endpoint: identifier (value of reg property) of the endpoint node 775 * 776 * Return: Remote device node associated with remote endpoint node linked 777 * to @node. Use of_node_put() on it when done. 778 */ 779 struct device_node *of_graph_get_remote_node(const struct device_node *node, 780 u32 port, u32 endpoint) 781 { 782 struct device_node *endpoint_node, *remote; 783 784 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint); 785 if (!endpoint_node) { 786 pr_debug("no valid endpoint (%d, %d) for node %pOF\n", 787 port, endpoint, node); 788 return NULL; 789 } 790 791 remote = of_graph_get_remote_port_parent(endpoint_node); 792 of_node_put(endpoint_node); 793 if (!remote) { 794 pr_debug("no valid remote node\n"); 795 return NULL; 796 } 797 798 if (!of_device_is_available(remote)) { 799 pr_debug("not available for remote node\n"); 800 return NULL; 801 } 802 803 return remote; 804 } 805 EXPORT_SYMBOL(of_graph_get_remote_node); 806 807 static void of_fwnode_get(struct fwnode_handle *fwnode) 808 { 809 of_node_get(to_of_node(fwnode)); 810 } 811 812 static void of_fwnode_put(struct fwnode_handle *fwnode) 813 { 814 of_node_put(to_of_node(fwnode)); 815 } 816 817 static bool of_fwnode_device_is_available(struct fwnode_handle *fwnode) 818 { 819 return of_device_is_available(to_of_node(fwnode)); 820 } 821 822 static bool of_fwnode_property_present(struct fwnode_handle *fwnode, 823 const char *propname) 824 { 825 return of_property_read_bool(to_of_node(fwnode), propname); 826 } 827 828 static int of_fwnode_property_read_int_array(struct fwnode_handle *fwnode, 829 const char *propname, 830 unsigned int elem_size, void *val, 831 size_t nval) 832 { 833 struct device_node *node = to_of_node(fwnode); 834 835 if (!val) 836 return of_property_count_elems_of_size(node, propname, 837 elem_size); 838 839 switch (elem_size) { 840 case sizeof(u8): 841 return of_property_read_u8_array(node, propname, val, nval); 842 case sizeof(u16): 843 return of_property_read_u16_array(node, propname, val, nval); 844 case sizeof(u32): 845 return of_property_read_u32_array(node, propname, val, nval); 846 case sizeof(u64): 847 return of_property_read_u64_array(node, propname, val, nval); 848 } 849 850 return -ENXIO; 851 } 852 853 static int of_fwnode_property_read_string_array(struct fwnode_handle *fwnode, 854 const char *propname, 855 const char **val, size_t nval) 856 { 857 struct device_node *node = to_of_node(fwnode); 858 859 return val ? 860 of_property_read_string_array(node, propname, val, nval) : 861 of_property_count_strings(node, propname); 862 } 863 864 static struct fwnode_handle *of_fwnode_get_parent(struct fwnode_handle *fwnode) 865 { 866 return of_fwnode_handle(of_get_parent(to_of_node(fwnode))); 867 } 868 869 static struct fwnode_handle * 870 of_fwnode_get_next_child_node(struct fwnode_handle *fwnode, 871 struct fwnode_handle *child) 872 { 873 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode), 874 to_of_node(child))); 875 } 876 877 static struct fwnode_handle * 878 of_fwnode_get_named_child_node(struct fwnode_handle *fwnode, 879 const char *childname) 880 { 881 struct device_node *node = to_of_node(fwnode); 882 struct device_node *child; 883 884 for_each_available_child_of_node(node, child) 885 if (!of_node_cmp(child->name, childname)) 886 return of_fwnode_handle(child); 887 888 return NULL; 889 } 890 891 static struct fwnode_handle * 892 of_fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode, 893 struct fwnode_handle *prev) 894 { 895 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode), 896 to_of_node(prev))); 897 } 898 899 static struct fwnode_handle * 900 of_fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode) 901 { 902 return of_fwnode_handle( 903 of_graph_get_remote_endpoint(to_of_node(fwnode))); 904 } 905 906 static struct fwnode_handle * 907 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode) 908 { 909 struct device_node *np; 910 911 /* Get the parent of the port */ 912 np = of_get_next_parent(to_of_node(fwnode)); 913 if (!np) 914 return NULL; 915 916 /* Is this the "ports" node? If not, it's the port parent. */ 917 if (of_node_cmp(np->name, "ports")) 918 return of_fwnode_handle(np); 919 920 return of_fwnode_handle(of_get_next_parent(np)); 921 } 922 923 static int of_fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode, 924 struct fwnode_endpoint *endpoint) 925 { 926 struct device_node *node = to_of_node(fwnode); 927 struct device_node *port_node = of_get_parent(node); 928 929 endpoint->local_fwnode = fwnode; 930 931 of_property_read_u32(port_node, "reg", &endpoint->port); 932 of_property_read_u32(node, "reg", &endpoint->id); 933 934 of_node_put(port_node); 935 936 return 0; 937 } 938 939 const struct fwnode_operations of_fwnode_ops = { 940 .get = of_fwnode_get, 941 .put = of_fwnode_put, 942 .device_is_available = of_fwnode_device_is_available, 943 .property_present = of_fwnode_property_present, 944 .property_read_int_array = of_fwnode_property_read_int_array, 945 .property_read_string_array = of_fwnode_property_read_string_array, 946 .get_parent = of_fwnode_get_parent, 947 .get_next_child_node = of_fwnode_get_next_child_node, 948 .get_named_child_node = of_fwnode_get_named_child_node, 949 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint, 950 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint, 951 .graph_get_port_parent = of_fwnode_graph_get_port_parent, 952 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint, 953 }; 954