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 %s is not a multiple of %d\n", 59 propname, np->full_name, 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 %s has no parent node\n", 541 __func__, node->full_name); 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 %s\n", 625 parent->full_name); 626 return NULL; 627 } 628 } else { 629 port = of_get_parent(prev); 630 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n", 631 __func__, prev->full_name)) 632 return NULL; 633 } 634 635 while (1) { 636 /* 637 * Now that we have a port node, get the next endpoint by 638 * getting the next child. If the previous endpoint is NULL this 639 * will return the first child. 640 */ 641 endpoint = of_get_next_child(port, prev); 642 if (endpoint) { 643 of_node_put(port); 644 return endpoint; 645 } 646 647 /* No more endpoints under this port, try the next one. */ 648 prev = NULL; 649 650 do { 651 port = of_get_next_child(parent, port); 652 if (!port) 653 return NULL; 654 } while (of_node_cmp(port->name, "port")); 655 } 656 } 657 EXPORT_SYMBOL(of_graph_get_next_endpoint); 658 659 /** 660 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers 661 * @parent: pointer to the parent device node 662 * @port_reg: identifier (value of reg property) of the parent port node 663 * @reg: identifier (value of reg property) of the endpoint node 664 * 665 * Return: An 'endpoint' node pointer which is identified by reg and at the same 666 * is the child of a port node identified by port_reg. reg and port_reg are 667 * ignored when they are -1. 668 */ 669 struct device_node *of_graph_get_endpoint_by_regs( 670 const struct device_node *parent, int port_reg, int reg) 671 { 672 struct of_endpoint endpoint; 673 struct device_node *node = NULL; 674 675 for_each_endpoint_of_node(parent, node) { 676 of_graph_parse_endpoint(node, &endpoint); 677 if (((port_reg == -1) || (endpoint.port == port_reg)) && 678 ((reg == -1) || (endpoint.id == reg))) 679 return node; 680 } 681 682 return NULL; 683 } 684 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs); 685 686 /** 687 * of_graph_get_remote_endpoint() - get remote endpoint node 688 * @node: pointer to a local endpoint device_node 689 * 690 * Return: Remote endpoint node associated with remote endpoint node linked 691 * to @node. Use of_node_put() on it when done. 692 */ 693 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node) 694 { 695 /* Get remote endpoint node. */ 696 return of_parse_phandle(node, "remote-endpoint", 0); 697 } 698 EXPORT_SYMBOL(of_graph_get_remote_endpoint); 699 700 /** 701 * of_graph_get_port_parent() - get port's parent node 702 * @node: pointer to a local endpoint device_node 703 * 704 * Return: device node associated with endpoint node linked 705 * to @node. Use of_node_put() on it when done. 706 */ 707 struct device_node *of_graph_get_port_parent(struct device_node *node) 708 { 709 unsigned int depth; 710 711 if (!node) 712 return NULL; 713 714 /* 715 * Preserve usecount for passed in node as of_get_next_parent() 716 * will do of_node_put() on it. 717 */ 718 of_node_get(node); 719 720 /* Walk 3 levels up only if there is 'ports' node. */ 721 for (depth = 3; depth && node; depth--) { 722 node = of_get_next_parent(node); 723 if (depth == 2 && of_node_cmp(node->name, "ports")) 724 break; 725 } 726 return node; 727 } 728 EXPORT_SYMBOL(of_graph_get_port_parent); 729 730 /** 731 * of_graph_get_remote_port_parent() - get remote port's parent node 732 * @node: pointer to a local endpoint device_node 733 * 734 * Return: Remote device node associated with remote endpoint node linked 735 * to @node. Use of_node_put() on it when done. 736 */ 737 struct device_node *of_graph_get_remote_port_parent( 738 const struct device_node *node) 739 { 740 struct device_node *np, *pp; 741 742 /* Get remote endpoint node. */ 743 np = of_graph_get_remote_endpoint(node); 744 745 pp = of_graph_get_port_parent(np); 746 747 of_node_put(np); 748 749 return pp; 750 } 751 EXPORT_SYMBOL(of_graph_get_remote_port_parent); 752 753 /** 754 * of_graph_get_remote_port() - get remote port node 755 * @node: pointer to a local endpoint device_node 756 * 757 * Return: Remote port node associated with remote endpoint node linked 758 * to @node. Use of_node_put() on it when done. 759 */ 760 struct device_node *of_graph_get_remote_port(const struct device_node *node) 761 { 762 struct device_node *np; 763 764 /* Get remote endpoint node. */ 765 np = of_graph_get_remote_endpoint(node); 766 if (!np) 767 return NULL; 768 return of_get_next_parent(np); 769 } 770 EXPORT_SYMBOL(of_graph_get_remote_port); 771 772 int of_graph_get_endpoint_count(const struct device_node *np) 773 { 774 struct device_node *endpoint; 775 int num = 0; 776 777 for_each_endpoint_of_node(np, endpoint) 778 num++; 779 780 return num; 781 } 782 EXPORT_SYMBOL(of_graph_get_endpoint_count); 783 784 /** 785 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint 786 * @node: pointer to parent device_node containing graph port/endpoint 787 * @port: identifier (value of reg property) of the parent port node 788 * @endpoint: identifier (value of reg property) of the endpoint node 789 * 790 * Return: Remote device node associated with remote endpoint node linked 791 * to @node. Use of_node_put() on it when done. 792 */ 793 struct device_node *of_graph_get_remote_node(const struct device_node *node, 794 u32 port, u32 endpoint) 795 { 796 struct device_node *endpoint_node, *remote; 797 798 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint); 799 if (!endpoint_node) { 800 pr_debug("no valid endpoint (%d, %d) for node %s\n", 801 port, endpoint, node->full_name); 802 return NULL; 803 } 804 805 remote = of_graph_get_remote_port_parent(endpoint_node); 806 of_node_put(endpoint_node); 807 if (!remote) { 808 pr_debug("no valid remote node\n"); 809 return NULL; 810 } 811 812 if (!of_device_is_available(remote)) { 813 pr_debug("not available for remote node\n"); 814 return NULL; 815 } 816 817 return remote; 818 } 819 EXPORT_SYMBOL(of_graph_get_remote_node); 820 821 static void of_fwnode_get(struct fwnode_handle *fwnode) 822 { 823 of_node_get(to_of_node(fwnode)); 824 } 825 826 static void of_fwnode_put(struct fwnode_handle *fwnode) 827 { 828 of_node_put(to_of_node(fwnode)); 829 } 830 831 static bool of_fwnode_device_is_available(struct fwnode_handle *fwnode) 832 { 833 return of_device_is_available(to_of_node(fwnode)); 834 } 835 836 static bool of_fwnode_property_present(struct fwnode_handle *fwnode, 837 const char *propname) 838 { 839 return of_property_read_bool(to_of_node(fwnode), propname); 840 } 841 842 static int of_fwnode_property_read_int_array(struct fwnode_handle *fwnode, 843 const char *propname, 844 unsigned int elem_size, void *val, 845 size_t nval) 846 { 847 struct device_node *node = to_of_node(fwnode); 848 849 if (!val) 850 return of_property_count_elems_of_size(node, propname, 851 elem_size); 852 853 switch (elem_size) { 854 case sizeof(u8): 855 return of_property_read_u8_array(node, propname, val, nval); 856 case sizeof(u16): 857 return of_property_read_u16_array(node, propname, val, nval); 858 case sizeof(u32): 859 return of_property_read_u32_array(node, propname, val, nval); 860 case sizeof(u64): 861 return of_property_read_u64_array(node, propname, val, nval); 862 } 863 864 return -ENXIO; 865 } 866 867 static int of_fwnode_property_read_string_array(struct fwnode_handle *fwnode, 868 const char *propname, 869 const char **val, size_t nval) 870 { 871 struct device_node *node = to_of_node(fwnode); 872 873 return val ? 874 of_property_read_string_array(node, propname, val, nval) : 875 of_property_count_strings(node, propname); 876 } 877 878 static struct fwnode_handle *of_fwnode_get_parent(struct fwnode_handle *fwnode) 879 { 880 return of_fwnode_handle(of_get_parent(to_of_node(fwnode))); 881 } 882 883 static struct fwnode_handle * 884 of_fwnode_get_next_child_node(struct fwnode_handle *fwnode, 885 struct fwnode_handle *child) 886 { 887 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode), 888 to_of_node(child))); 889 } 890 891 static struct fwnode_handle * 892 of_fwnode_get_named_child_node(struct fwnode_handle *fwnode, 893 const char *childname) 894 { 895 struct device_node *node = to_of_node(fwnode); 896 struct device_node *child; 897 898 for_each_available_child_of_node(node, child) 899 if (!of_node_cmp(child->name, childname)) 900 return of_fwnode_handle(child); 901 902 return NULL; 903 } 904 905 static struct fwnode_handle * 906 of_fwnode_graph_get_next_endpoint(struct fwnode_handle *fwnode, 907 struct fwnode_handle *prev) 908 { 909 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode), 910 to_of_node(prev))); 911 } 912 913 static struct fwnode_handle * 914 of_fwnode_graph_get_remote_endpoint(struct fwnode_handle *fwnode) 915 { 916 return of_fwnode_handle(of_parse_phandle(to_of_node(fwnode), 917 "remote-endpoint", 0)); 918 } 919 920 static struct fwnode_handle * 921 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode) 922 { 923 struct device_node *np; 924 925 /* Get the parent of the port */ 926 np = of_get_next_parent(to_of_node(fwnode)); 927 if (!np) 928 return NULL; 929 930 /* Is this the "ports" node? If not, it's the port parent. */ 931 if (of_node_cmp(np->name, "ports")) 932 return of_fwnode_handle(np); 933 934 return of_fwnode_handle(of_get_next_parent(np)); 935 } 936 937 static int of_fwnode_graph_parse_endpoint(struct fwnode_handle *fwnode, 938 struct fwnode_endpoint *endpoint) 939 { 940 struct device_node *node = to_of_node(fwnode); 941 struct device_node *port_node = of_get_parent(node); 942 943 endpoint->local_fwnode = fwnode; 944 945 of_property_read_u32(port_node, "reg", &endpoint->port); 946 of_property_read_u32(node, "reg", &endpoint->id); 947 948 of_node_put(port_node); 949 950 return 0; 951 } 952 953 const struct fwnode_operations of_fwnode_ops = { 954 .get = of_fwnode_get, 955 .put = of_fwnode_put, 956 .device_is_available = of_fwnode_device_is_available, 957 .property_present = of_fwnode_property_present, 958 .property_read_int_array = of_fwnode_property_read_int_array, 959 .property_read_string_array = of_fwnode_property_read_string_array, 960 .get_parent = of_fwnode_get_parent, 961 .get_next_child_node = of_fwnode_get_next_child_node, 962 .get_named_child_node = of_fwnode_get_named_child_node, 963 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint, 964 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint, 965 .graph_get_port_parent = of_fwnode_graph_get_port_parent, 966 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint, 967 }; 968