1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * property.c - Unified device property interface. 4 * 5 * Copyright (C) 2014, Intel Corporation 6 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 7 * Mika Westerberg <mika.westerberg@linux.intel.com> 8 */ 9 10 #include <linux/device.h> 11 #include <linux/err.h> 12 #include <linux/export.h> 13 #include <linux/kconfig.h> 14 #include <linux/of.h> 15 #include <linux/property.h> 16 #include <linux/phy.h> 17 #include <linux/slab.h> 18 #include <linux/string.h> 19 #include <linux/types.h> 20 21 struct fwnode_handle *__dev_fwnode(struct device *dev) 22 { 23 return IS_ENABLED(CONFIG_OF) && dev->of_node ? 24 of_fwnode_handle(dev->of_node) : dev->fwnode; 25 } 26 EXPORT_SYMBOL_GPL(__dev_fwnode); 27 28 const struct fwnode_handle *__dev_fwnode_const(const struct device *dev) 29 { 30 return IS_ENABLED(CONFIG_OF) && dev->of_node ? 31 of_fwnode_handle(dev->of_node) : dev->fwnode; 32 } 33 EXPORT_SYMBOL_GPL(__dev_fwnode_const); 34 35 /** 36 * device_property_present - check if a property of a device is present 37 * @dev: Device whose property is being checked 38 * @propname: Name of the property 39 * 40 * Check if property @propname is present in the device firmware description. 41 * 42 * Return: true if property @propname is present. Otherwise, returns false. 43 */ 44 bool device_property_present(const struct device *dev, const char *propname) 45 { 46 return fwnode_property_present(dev_fwnode(dev), propname); 47 } 48 EXPORT_SYMBOL_GPL(device_property_present); 49 50 /** 51 * fwnode_property_present - check if a property of a firmware node is present 52 * @fwnode: Firmware node whose property to check 53 * @propname: Name of the property 54 * 55 * Return: true if property @propname is present. Otherwise, returns false. 56 */ 57 bool fwnode_property_present(const struct fwnode_handle *fwnode, 58 const char *propname) 59 { 60 bool ret; 61 62 if (IS_ERR_OR_NULL(fwnode)) 63 return false; 64 65 ret = fwnode_call_bool_op(fwnode, property_present, propname); 66 if (ret) 67 return ret; 68 69 return fwnode_call_bool_op(fwnode->secondary, property_present, propname); 70 } 71 EXPORT_SYMBOL_GPL(fwnode_property_present); 72 73 /** 74 * device_property_read_bool - Return the value for a boolean property of a device 75 * @dev: Device whose property is being checked 76 * @propname: Name of the property 77 * 78 * Return if property @propname is true or false in the device firmware description. 79 * 80 * Return: true if property @propname is present. Otherwise, returns false. 81 */ 82 bool device_property_read_bool(const struct device *dev, const char *propname) 83 { 84 return fwnode_property_read_bool(dev_fwnode(dev), propname); 85 } 86 EXPORT_SYMBOL_GPL(device_property_read_bool); 87 88 /** 89 * fwnode_property_read_bool - Return the value for a boolean property of a firmware node 90 * @fwnode: Firmware node whose property to check 91 * @propname: Name of the property 92 * 93 * Return if property @propname is true or false in the firmware description. 94 */ 95 bool fwnode_property_read_bool(const struct fwnode_handle *fwnode, 96 const char *propname) 97 { 98 bool ret; 99 100 if (IS_ERR_OR_NULL(fwnode)) 101 return false; 102 103 ret = fwnode_call_bool_op(fwnode, property_read_bool, propname); 104 if (ret) 105 return ret; 106 107 return fwnode_call_bool_op(fwnode->secondary, property_read_bool, propname); 108 } 109 EXPORT_SYMBOL_GPL(fwnode_property_read_bool); 110 111 /** 112 * device_property_read_u8_array - return a u8 array property of a device 113 * @dev: Device to get the property of 114 * @propname: Name of the property 115 * @val: The values are stored here or %NULL to return the number of values 116 * @nval: Size of the @val array 117 * 118 * Function reads an array of u8 properties with @propname from the device 119 * firmware description and stores them to @val if found. 120 * 121 * It's recommended to call device_property_count_u8() instead of calling 122 * this function with @val equals %NULL and @nval equals 0. 123 * 124 * Return: number of values if @val was %NULL, 125 * %0 if the property was found (success), 126 * %-EINVAL if given arguments are not valid, 127 * %-ENODATA if the property does not have a value, 128 * %-EPROTO if the property is not an array of numbers, 129 * %-EOVERFLOW if the size of the property is not as expected. 130 * %-ENXIO if no suitable firmware interface is present. 131 */ 132 int device_property_read_u8_array(const struct device *dev, const char *propname, 133 u8 *val, size_t nval) 134 { 135 return fwnode_property_read_u8_array(dev_fwnode(dev), propname, val, nval); 136 } 137 EXPORT_SYMBOL_GPL(device_property_read_u8_array); 138 139 /** 140 * device_property_read_u16_array - return a u16 array property of a device 141 * @dev: Device to get the property of 142 * @propname: Name of the property 143 * @val: The values are stored here or %NULL to return the number of values 144 * @nval: Size of the @val array 145 * 146 * Function reads an array of u16 properties with @propname from the device 147 * firmware description and stores them to @val if found. 148 * 149 * It's recommended to call device_property_count_u16() instead of calling 150 * this function with @val equals %NULL and @nval equals 0. 151 * 152 * Return: number of values if @val was %NULL, 153 * %0 if the property was found (success), 154 * %-EINVAL if given arguments are not valid, 155 * %-ENODATA if the property does not have a value, 156 * %-EPROTO if the property is not an array of numbers, 157 * %-EOVERFLOW if the size of the property is not as expected. 158 * %-ENXIO if no suitable firmware interface is present. 159 */ 160 int device_property_read_u16_array(const struct device *dev, const char *propname, 161 u16 *val, size_t nval) 162 { 163 return fwnode_property_read_u16_array(dev_fwnode(dev), propname, val, nval); 164 } 165 EXPORT_SYMBOL_GPL(device_property_read_u16_array); 166 167 /** 168 * device_property_read_u32_array - return a u32 array property of a device 169 * @dev: Device to get the property of 170 * @propname: Name of the property 171 * @val: The values are stored here or %NULL to return the number of values 172 * @nval: Size of the @val array 173 * 174 * Function reads an array of u32 properties with @propname from the device 175 * firmware description and stores them to @val if found. 176 * 177 * It's recommended to call device_property_count_u32() instead of calling 178 * this function with @val equals %NULL and @nval equals 0. 179 * 180 * Return: number of values if @val was %NULL, 181 * %0 if the property was found (success), 182 * %-EINVAL if given arguments are not valid, 183 * %-ENODATA if the property does not have a value, 184 * %-EPROTO if the property is not an array of numbers, 185 * %-EOVERFLOW if the size of the property is not as expected. 186 * %-ENXIO if no suitable firmware interface is present. 187 */ 188 int device_property_read_u32_array(const struct device *dev, const char *propname, 189 u32 *val, size_t nval) 190 { 191 return fwnode_property_read_u32_array(dev_fwnode(dev), propname, val, nval); 192 } 193 EXPORT_SYMBOL_GPL(device_property_read_u32_array); 194 195 /** 196 * device_property_read_u64_array - return a u64 array property of a device 197 * @dev: Device to get the property of 198 * @propname: Name of the property 199 * @val: The values are stored here or %NULL to return the number of values 200 * @nval: Size of the @val array 201 * 202 * Function reads an array of u64 properties with @propname from the device 203 * firmware description and stores them to @val if found. 204 * 205 * It's recommended to call device_property_count_u64() instead of calling 206 * this function with @val equals %NULL and @nval equals 0. 207 * 208 * Return: number of values if @val was %NULL, 209 * %0 if the property was found (success), 210 * %-EINVAL if given arguments are not valid, 211 * %-ENODATA if the property does not have a value, 212 * %-EPROTO if the property is not an array of numbers, 213 * %-EOVERFLOW if the size of the property is not as expected. 214 * %-ENXIO if no suitable firmware interface is present. 215 */ 216 int device_property_read_u64_array(const struct device *dev, const char *propname, 217 u64 *val, size_t nval) 218 { 219 return fwnode_property_read_u64_array(dev_fwnode(dev), propname, val, nval); 220 } 221 EXPORT_SYMBOL_GPL(device_property_read_u64_array); 222 223 /** 224 * device_property_read_string_array - return a string array property of device 225 * @dev: Device to get the property of 226 * @propname: Name of the property 227 * @val: The values are stored here or %NULL to return the number of values 228 * @nval: Size of the @val array 229 * 230 * Function reads an array of string properties with @propname from the device 231 * firmware description and stores them to @val if found. 232 * 233 * It's recommended to call device_property_string_array_count() instead of calling 234 * this function with @val equals %NULL and @nval equals 0. 235 * 236 * Return: number of values read on success if @val is non-NULL, 237 * number of values available on success if @val is NULL, 238 * %-EINVAL if given arguments are not valid, 239 * %-ENODATA if the property does not have a value, 240 * %-EPROTO or %-EILSEQ if the property is not an array of strings, 241 * %-EOVERFLOW if the size of the property is not as expected. 242 * %-ENXIO if no suitable firmware interface is present. 243 */ 244 int device_property_read_string_array(const struct device *dev, const char *propname, 245 const char **val, size_t nval) 246 { 247 return fwnode_property_read_string_array(dev_fwnode(dev), propname, val, nval); 248 } 249 EXPORT_SYMBOL_GPL(device_property_read_string_array); 250 251 /** 252 * device_property_read_string - return a string property of a device 253 * @dev: Device to get the property of 254 * @propname: Name of the property 255 * @val: The value is stored here 256 * 257 * Function reads property @propname from the device firmware description and 258 * stores the value into @val if found. The value is checked to be a string. 259 * 260 * Return: %0 if the property was found (success), 261 * %-EINVAL if given arguments are not valid, 262 * %-ENODATA if the property does not have a value, 263 * %-EPROTO or %-EILSEQ if the property type is not a string. 264 * %-ENXIO if no suitable firmware interface is present. 265 */ 266 int device_property_read_string(const struct device *dev, const char *propname, 267 const char **val) 268 { 269 return fwnode_property_read_string(dev_fwnode(dev), propname, val); 270 } 271 EXPORT_SYMBOL_GPL(device_property_read_string); 272 273 /** 274 * device_property_match_string - find a string in an array and return index 275 * @dev: Device to get the property of 276 * @propname: Name of the property holding the array 277 * @string: String to look for 278 * 279 * Find a given string in a string array and if it is found return the 280 * index back. 281 * 282 * Return: index, starting from %0, if the property was found (success), 283 * %-EINVAL if given arguments are not valid, 284 * %-ENODATA if the property does not have a value, 285 * %-EPROTO if the property is not an array of strings, 286 * %-ENXIO if no suitable firmware interface is present. 287 */ 288 int device_property_match_string(const struct device *dev, const char *propname, 289 const char *string) 290 { 291 return fwnode_property_match_string(dev_fwnode(dev), propname, string); 292 } 293 EXPORT_SYMBOL_GPL(device_property_match_string); 294 295 static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode, 296 const char *propname, 297 unsigned int elem_size, void *val, 298 size_t nval) 299 { 300 int ret; 301 302 if (IS_ERR_OR_NULL(fwnode)) 303 return -EINVAL; 304 305 ret = fwnode_call_int_op(fwnode, property_read_int_array, propname, 306 elem_size, val, nval); 307 if (ret != -EINVAL) 308 return ret; 309 310 return fwnode_call_int_op(fwnode->secondary, property_read_int_array, propname, 311 elem_size, val, nval); 312 } 313 314 /** 315 * fwnode_property_read_u8_array - return a u8 array property of firmware node 316 * @fwnode: Firmware node to get the property of 317 * @propname: Name of the property 318 * @val: The values are stored here or %NULL to return the number of values 319 * @nval: Size of the @val array 320 * 321 * Read an array of u8 properties with @propname from @fwnode and stores them to 322 * @val if found. 323 * 324 * It's recommended to call fwnode_property_count_u8() instead of calling 325 * this function with @val equals %NULL and @nval equals 0. 326 * 327 * Return: number of values if @val was %NULL, 328 * %0 if the property was found (success), 329 * %-EINVAL if given arguments are not valid, 330 * %-ENODATA if the property does not have a value, 331 * %-EPROTO if the property is not an array of numbers, 332 * %-EOVERFLOW if the size of the property is not as expected, 333 * %-ENXIO if no suitable firmware interface is present. 334 */ 335 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode, 336 const char *propname, u8 *val, size_t nval) 337 { 338 return fwnode_property_read_int_array(fwnode, propname, sizeof(u8), 339 val, nval); 340 } 341 EXPORT_SYMBOL_GPL(fwnode_property_read_u8_array); 342 343 /** 344 * fwnode_property_read_u16_array - return a u16 array property of firmware node 345 * @fwnode: Firmware node to get the property of 346 * @propname: Name of the property 347 * @val: The values are stored here or %NULL to return the number of values 348 * @nval: Size of the @val array 349 * 350 * Read an array of u16 properties with @propname from @fwnode and store them to 351 * @val if found. 352 * 353 * It's recommended to call fwnode_property_count_u16() instead of calling 354 * this function with @val equals %NULL and @nval equals 0. 355 * 356 * Return: number of values if @val was %NULL, 357 * %0 if the property was found (success), 358 * %-EINVAL if given arguments are not valid, 359 * %-ENODATA if the property does not have a value, 360 * %-EPROTO if the property is not an array of numbers, 361 * %-EOVERFLOW if the size of the property is not as expected, 362 * %-ENXIO if no suitable firmware interface is present. 363 */ 364 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode, 365 const char *propname, u16 *val, size_t nval) 366 { 367 return fwnode_property_read_int_array(fwnode, propname, sizeof(u16), 368 val, nval); 369 } 370 EXPORT_SYMBOL_GPL(fwnode_property_read_u16_array); 371 372 /** 373 * fwnode_property_read_u32_array - return a u32 array property of firmware node 374 * @fwnode: Firmware node to get the property of 375 * @propname: Name of the property 376 * @val: The values are stored here or %NULL to return the number of values 377 * @nval: Size of the @val array 378 * 379 * Read an array of u32 properties with @propname from @fwnode store them to 380 * @val if found. 381 * 382 * It's recommended to call fwnode_property_count_u32() instead of calling 383 * this function with @val equals %NULL and @nval equals 0. 384 * 385 * Return: number of values if @val was %NULL, 386 * %0 if the property was found (success), 387 * %-EINVAL if given arguments are not valid, 388 * %-ENODATA if the property does not have a value, 389 * %-EPROTO if the property is not an array of numbers, 390 * %-EOVERFLOW if the size of the property is not as expected, 391 * %-ENXIO if no suitable firmware interface is present. 392 */ 393 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode, 394 const char *propname, u32 *val, size_t nval) 395 { 396 return fwnode_property_read_int_array(fwnode, propname, sizeof(u32), 397 val, nval); 398 } 399 EXPORT_SYMBOL_GPL(fwnode_property_read_u32_array); 400 401 /** 402 * fwnode_property_read_u64_array - return a u64 array property firmware node 403 * @fwnode: Firmware node to get the property of 404 * @propname: Name of the property 405 * @val: The values are stored here or %NULL to return the number of values 406 * @nval: Size of the @val array 407 * 408 * Read an array of u64 properties with @propname from @fwnode and store them to 409 * @val if found. 410 * 411 * It's recommended to call fwnode_property_count_u64() instead of calling 412 * this function with @val equals %NULL and @nval equals 0. 413 * 414 * Return: number of values if @val was %NULL, 415 * %0 if the property was found (success), 416 * %-EINVAL if given arguments are not valid, 417 * %-ENODATA if the property does not have a value, 418 * %-EPROTO if the property is not an array of numbers, 419 * %-EOVERFLOW if the size of the property is not as expected, 420 * %-ENXIO if no suitable firmware interface is present. 421 */ 422 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode, 423 const char *propname, u64 *val, size_t nval) 424 { 425 return fwnode_property_read_int_array(fwnode, propname, sizeof(u64), 426 val, nval); 427 } 428 EXPORT_SYMBOL_GPL(fwnode_property_read_u64_array); 429 430 /** 431 * fwnode_property_read_string_array - return string array property of a node 432 * @fwnode: Firmware node to get the property of 433 * @propname: Name of the property 434 * @val: The values are stored here or %NULL to return the number of values 435 * @nval: Size of the @val array 436 * 437 * Read an string list property @propname from the given firmware node and store 438 * them to @val if found. 439 * 440 * It's recommended to call fwnode_property_string_array_count() instead of calling 441 * this function with @val equals %NULL and @nval equals 0. 442 * 443 * Return: number of values read on success if @val is non-NULL, 444 * number of values available on success if @val is NULL, 445 * %-EINVAL if given arguments are not valid, 446 * %-ENODATA if the property does not have a value, 447 * %-EPROTO or %-EILSEQ if the property is not an array of strings, 448 * %-EOVERFLOW if the size of the property is not as expected, 449 * %-ENXIO if no suitable firmware interface is present. 450 */ 451 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode, 452 const char *propname, const char **val, 453 size_t nval) 454 { 455 int ret; 456 457 if (IS_ERR_OR_NULL(fwnode)) 458 return -EINVAL; 459 460 ret = fwnode_call_int_op(fwnode, property_read_string_array, propname, 461 val, nval); 462 if (ret != -EINVAL) 463 return ret; 464 465 return fwnode_call_int_op(fwnode->secondary, property_read_string_array, propname, 466 val, nval); 467 } 468 EXPORT_SYMBOL_GPL(fwnode_property_read_string_array); 469 470 /** 471 * fwnode_property_read_string - return a string property of a firmware node 472 * @fwnode: Firmware node to get the property of 473 * @propname: Name of the property 474 * @val: The value is stored here 475 * 476 * Read property @propname from the given firmware node and store the value into 477 * @val if found. The value is checked to be a string. 478 * 479 * Return: %0 if the property was found (success), 480 * %-EINVAL if given arguments are not valid, 481 * %-ENODATA if the property does not have a value, 482 * %-EPROTO or %-EILSEQ if the property is not a string, 483 * %-ENXIO if no suitable firmware interface is present. 484 */ 485 int fwnode_property_read_string(const struct fwnode_handle *fwnode, 486 const char *propname, const char **val) 487 { 488 int ret = fwnode_property_read_string_array(fwnode, propname, val, 1); 489 490 return ret < 0 ? ret : 0; 491 } 492 EXPORT_SYMBOL_GPL(fwnode_property_read_string); 493 494 /** 495 * fwnode_property_match_string - find a string in an array and return index 496 * @fwnode: Firmware node to get the property of 497 * @propname: Name of the property holding the array 498 * @string: String to look for 499 * 500 * Find a given string in a string array and if it is found return the 501 * index back. 502 * 503 * Return: index, starting from %0, if the property was found (success), 504 * %-EINVAL if given arguments are not valid, 505 * %-ENODATA if the property does not have a value, 506 * %-EPROTO if the property is not an array of strings, 507 * %-ENXIO if no suitable firmware interface is present. 508 */ 509 int fwnode_property_match_string(const struct fwnode_handle *fwnode, 510 const char *propname, const char *string) 511 { 512 const char **values; 513 int nval, ret; 514 515 nval = fwnode_property_string_array_count(fwnode, propname); 516 if (nval < 0) 517 return nval; 518 519 if (nval == 0) 520 return -ENODATA; 521 522 values = kcalloc(nval, sizeof(*values), GFP_KERNEL); 523 if (!values) 524 return -ENOMEM; 525 526 ret = fwnode_property_read_string_array(fwnode, propname, values, nval); 527 if (ret < 0) 528 goto out_free; 529 530 ret = match_string(values, nval, string); 531 if (ret < 0) 532 ret = -ENODATA; 533 534 out_free: 535 kfree(values); 536 return ret; 537 } 538 EXPORT_SYMBOL_GPL(fwnode_property_match_string); 539 540 /** 541 * fwnode_property_match_property_string - find a property string value in an array and return index 542 * @fwnode: Firmware node to get the property of 543 * @propname: Name of the property holding the string value 544 * @array: String array to search in 545 * @n: Size of the @array 546 * 547 * Find a property string value in a given @array and if it is found return 548 * the index back. 549 * 550 * Return: index, starting from %0, if the string value was found in the @array (success), 551 * %-ENOENT when the string value was not found in the @array, 552 * %-EINVAL if given arguments are not valid, 553 * %-ENODATA if the property does not have a value, 554 * %-EPROTO or %-EILSEQ if the property is not a string, 555 * %-ENXIO if no suitable firmware interface is present. 556 */ 557 int fwnode_property_match_property_string(const struct fwnode_handle *fwnode, 558 const char *propname, const char * const *array, size_t n) 559 { 560 const char *string; 561 int ret; 562 563 ret = fwnode_property_read_string(fwnode, propname, &string); 564 if (ret) 565 return ret; 566 567 ret = match_string(array, n, string); 568 if (ret < 0) 569 ret = -ENOENT; 570 571 return ret; 572 } 573 EXPORT_SYMBOL_GPL(fwnode_property_match_property_string); 574 575 /** 576 * fwnode_property_get_reference_args() - Find a reference with arguments 577 * @fwnode: Firmware node where to look for the reference 578 * @prop: The name of the property 579 * @nargs_prop: The name of the property telling the number of 580 * arguments in the referred node. NULL if @nargs is known, 581 * otherwise @nargs is ignored. Only relevant on OF. 582 * @nargs: Number of arguments. Ignored if @nargs_prop is non-NULL. 583 * @index: Index of the reference, from zero onwards. 584 * @args: Result structure with reference and integer arguments. 585 * May be NULL. 586 * 587 * Obtain a reference based on a named property in an fwnode, with 588 * integer arguments. 589 * 590 * The caller is responsible for calling fwnode_handle_put() on the returned 591 * @args->fwnode pointer. 592 * 593 * Return: %0 on success 594 * %-ENOENT when the index is out of bounds, the index has an empty 595 * reference or the property was not found 596 * %-EINVAL on parse error 597 */ 598 int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode, 599 const char *prop, const char *nargs_prop, 600 unsigned int nargs, unsigned int index, 601 struct fwnode_reference_args *args) 602 { 603 int ret; 604 605 if (IS_ERR_OR_NULL(fwnode)) 606 return -ENOENT; 607 608 ret = fwnode_call_int_op(fwnode, get_reference_args, prop, nargs_prop, 609 nargs, index, args); 610 if (ret == 0) 611 return ret; 612 613 if (IS_ERR_OR_NULL(fwnode->secondary)) 614 return ret; 615 616 return fwnode_call_int_op(fwnode->secondary, get_reference_args, prop, nargs_prop, 617 nargs, index, args); 618 } 619 EXPORT_SYMBOL_GPL(fwnode_property_get_reference_args); 620 621 /** 622 * fwnode_find_reference - Find named reference to a fwnode_handle 623 * @fwnode: Firmware node where to look for the reference 624 * @name: The name of the reference 625 * @index: Index of the reference 626 * 627 * @index can be used when the named reference holds a table of references. 628 * 629 * The caller is responsible for calling fwnode_handle_put() on the returned 630 * fwnode pointer. 631 * 632 * Return: a pointer to the reference fwnode, when found. Otherwise, 633 * returns an error pointer. 634 */ 635 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode, 636 const char *name, 637 unsigned int index) 638 { 639 struct fwnode_reference_args args; 640 int ret; 641 642 ret = fwnode_property_get_reference_args(fwnode, name, NULL, 0, index, 643 &args); 644 return ret ? ERR_PTR(ret) : args.fwnode; 645 } 646 EXPORT_SYMBOL_GPL(fwnode_find_reference); 647 648 /** 649 * fwnode_get_name - Return the name of a node 650 * @fwnode: The firmware node 651 * 652 * Return: a pointer to the node name, or %NULL. 653 */ 654 const char *fwnode_get_name(const struct fwnode_handle *fwnode) 655 { 656 return fwnode_call_ptr_op(fwnode, get_name); 657 } 658 EXPORT_SYMBOL_GPL(fwnode_get_name); 659 660 /** 661 * fwnode_get_name_prefix - Return the prefix of node for printing purposes 662 * @fwnode: The firmware node 663 * 664 * Return: the prefix of a node, intended to be printed right before the node. 665 * The prefix works also as a separator between the nodes. 666 */ 667 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode) 668 { 669 return fwnode_call_ptr_op(fwnode, get_name_prefix); 670 } 671 672 /** 673 * fwnode_name_eq - Return true if node name is equal 674 * @fwnode: The firmware node 675 * @name: The name to which to compare the node name 676 * 677 * Compare the name provided as an argument to the name of the node, stopping 678 * the comparison at either NUL or '@' character, whichever comes first. This 679 * function is generally used for comparing node names while ignoring the 680 * possible unit address of the node. 681 * 682 * Return: true if the node name matches with the name provided in the @name 683 * argument, false otherwise. 684 */ 685 bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name) 686 { 687 const char *node_name; 688 ptrdiff_t len; 689 690 node_name = fwnode_get_name(fwnode); 691 if (!node_name) 692 return false; 693 694 len = strchrnul(node_name, '@') - node_name; 695 696 return str_has_prefix(node_name, name) == len; 697 } 698 EXPORT_SYMBOL_GPL(fwnode_name_eq); 699 700 /** 701 * fwnode_get_parent - Return parent firwmare node 702 * @fwnode: Firmware whose parent is retrieved 703 * 704 * The caller is responsible for calling fwnode_handle_put() on the returned 705 * fwnode pointer. 706 * 707 * Return: parent firmware node of the given node if possible or %NULL if no 708 * parent was available. 709 */ 710 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode) 711 { 712 return fwnode_call_ptr_op(fwnode, get_parent); 713 } 714 EXPORT_SYMBOL_GPL(fwnode_get_parent); 715 716 /** 717 * fwnode_get_next_parent - Iterate to the node's parent 718 * @fwnode: Firmware whose parent is retrieved 719 * 720 * This is like fwnode_get_parent() except that it drops the refcount 721 * on the passed node, making it suitable for iterating through a 722 * node's parents. 723 * 724 * The caller is responsible for calling fwnode_handle_put() on the returned 725 * fwnode pointer. Note that this function also puts a reference to @fwnode 726 * unconditionally. 727 * 728 * Return: parent firmware node of the given node if possible or %NULL if no 729 * parent was available. 730 */ 731 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode) 732 { 733 struct fwnode_handle *parent = fwnode_get_parent(fwnode); 734 735 fwnode_handle_put(fwnode); 736 737 return parent; 738 } 739 EXPORT_SYMBOL_GPL(fwnode_get_next_parent); 740 741 /** 742 * fwnode_count_parents - Return the number of parents a node has 743 * @fwnode: The node the parents of which are to be counted 744 * 745 * Return: the number of parents a node has. 746 */ 747 unsigned int fwnode_count_parents(const struct fwnode_handle *fwnode) 748 { 749 struct fwnode_handle *parent; 750 unsigned int count = 0; 751 752 fwnode_for_each_parent_node(fwnode, parent) 753 count++; 754 755 return count; 756 } 757 EXPORT_SYMBOL_GPL(fwnode_count_parents); 758 759 /** 760 * fwnode_get_nth_parent - Return an nth parent of a node 761 * @fwnode: The node the parent of which is requested 762 * @depth: Distance of the parent from the node 763 * 764 * The caller is responsible for calling fwnode_handle_put() on the returned 765 * fwnode pointer. 766 * 767 * Return: the nth parent of a node. If there is no parent at the requested 768 * @depth, %NULL is returned. If @depth is 0, the functionality is equivalent to 769 * fwnode_handle_get(). For @depth == 1, it is fwnode_get_parent() and so on. 770 */ 771 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwnode, 772 unsigned int depth) 773 { 774 struct fwnode_handle *parent; 775 776 if (depth == 0) 777 return fwnode_handle_get(fwnode); 778 779 fwnode_for_each_parent_node(fwnode, parent) { 780 if (--depth == 0) 781 return parent; 782 } 783 return NULL; 784 } 785 EXPORT_SYMBOL_GPL(fwnode_get_nth_parent); 786 787 /** 788 * fwnode_get_next_child_node - Return the next child node handle for a node 789 * @fwnode: Firmware node to find the next child node for. 790 * @child: Handle to one of the node's child nodes or a %NULL handle. 791 * 792 * The caller is responsible for calling fwnode_handle_put() on the returned 793 * fwnode pointer. Note that this function also puts a reference to @child 794 * unconditionally. 795 */ 796 struct fwnode_handle * 797 fwnode_get_next_child_node(const struct fwnode_handle *fwnode, 798 struct fwnode_handle *child) 799 { 800 return fwnode_call_ptr_op(fwnode, get_next_child_node, child); 801 } 802 EXPORT_SYMBOL_GPL(fwnode_get_next_child_node); 803 804 /** 805 * fwnode_get_next_available_child_node - Return the next available child node handle for a node 806 * @fwnode: Firmware node to find the next child node for. 807 * @child: Handle to one of the node's child nodes or a %NULL handle. 808 * 809 * The caller is responsible for calling fwnode_handle_put() on the returned 810 * fwnode pointer. Note that this function also puts a reference to @child 811 * unconditionally. 812 */ 813 struct fwnode_handle * 814 fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode, 815 struct fwnode_handle *child) 816 { 817 struct fwnode_handle *next_child = child; 818 819 if (IS_ERR_OR_NULL(fwnode)) 820 return NULL; 821 822 do { 823 next_child = fwnode_get_next_child_node(fwnode, next_child); 824 if (!next_child) 825 return NULL; 826 } while (!fwnode_device_is_available(next_child)); 827 828 return next_child; 829 } 830 EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node); 831 832 /** 833 * device_get_next_child_node - Return the next child node handle for a device 834 * @dev: Device to find the next child node for. 835 * @child: Handle to one of the device's child nodes or a %NULL handle. 836 * 837 * The caller is responsible for calling fwnode_handle_put() on the returned 838 * fwnode pointer. Note that this function also puts a reference to @child 839 * unconditionally. 840 */ 841 struct fwnode_handle *device_get_next_child_node(const struct device *dev, 842 struct fwnode_handle *child) 843 { 844 const struct fwnode_handle *fwnode = dev_fwnode(dev); 845 struct fwnode_handle *next; 846 847 if (IS_ERR_OR_NULL(fwnode)) 848 return NULL; 849 850 /* Try to find a child in primary fwnode */ 851 next = fwnode_get_next_child_node(fwnode, child); 852 if (next) 853 return next; 854 855 /* When no more children in primary, continue with secondary */ 856 return fwnode_get_next_child_node(fwnode->secondary, child); 857 } 858 EXPORT_SYMBOL_GPL(device_get_next_child_node); 859 860 /** 861 * fwnode_get_named_child_node - Return first matching named child node handle 862 * @fwnode: Firmware node to find the named child node for. 863 * @childname: String to match child node name against. 864 * 865 * The caller is responsible for calling fwnode_handle_put() on the returned 866 * fwnode pointer. 867 */ 868 struct fwnode_handle * 869 fwnode_get_named_child_node(const struct fwnode_handle *fwnode, 870 const char *childname) 871 { 872 return fwnode_call_ptr_op(fwnode, get_named_child_node, childname); 873 } 874 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node); 875 876 /** 877 * device_get_named_child_node - Return first matching named child node handle 878 * @dev: Device to find the named child node for. 879 * @childname: String to match child node name against. 880 * 881 * The caller is responsible for calling fwnode_handle_put() on the returned 882 * fwnode pointer. 883 */ 884 struct fwnode_handle *device_get_named_child_node(const struct device *dev, 885 const char *childname) 886 { 887 return fwnode_get_named_child_node(dev_fwnode(dev), childname); 888 } 889 EXPORT_SYMBOL_GPL(device_get_named_child_node); 890 891 /** 892 * fwnode_handle_get - Obtain a reference to a device node 893 * @fwnode: Pointer to the device node to obtain the reference to. 894 * 895 * The caller is responsible for calling fwnode_handle_put() on the returned 896 * fwnode pointer. 897 * 898 * Return: the fwnode handle. 899 */ 900 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode) 901 { 902 if (!fwnode_has_op(fwnode, get)) 903 return fwnode; 904 905 return fwnode_call_ptr_op(fwnode, get); 906 } 907 EXPORT_SYMBOL_GPL(fwnode_handle_get); 908 909 /** 910 * fwnode_device_is_available - check if a device is available for use 911 * @fwnode: Pointer to the fwnode of the device. 912 * 913 * Return: true if device is available for use. Otherwise, returns false. 914 * 915 * For fwnode node types that don't implement the .device_is_available() 916 * operation, this function returns true. 917 */ 918 bool fwnode_device_is_available(const struct fwnode_handle *fwnode) 919 { 920 if (IS_ERR_OR_NULL(fwnode)) 921 return false; 922 923 if (!fwnode_has_op(fwnode, device_is_available)) 924 return true; 925 926 return fwnode_call_bool_op(fwnode, device_is_available); 927 } 928 EXPORT_SYMBOL_GPL(fwnode_device_is_available); 929 930 /** 931 * fwnode_get_child_node_count - return the number of child nodes for a given firmware node 932 * @fwnode: Pointer to the parent firmware node 933 * 934 * Return: the number of child nodes for a given firmware node. 935 */ 936 unsigned int fwnode_get_child_node_count(const struct fwnode_handle *fwnode) 937 { 938 struct fwnode_handle *child; 939 unsigned int count = 0; 940 941 fwnode_for_each_child_node(fwnode, child) 942 count++; 943 944 return count; 945 } 946 EXPORT_SYMBOL_GPL(fwnode_get_child_node_count); 947 948 /** 949 * fwnode_get_named_child_node_count - number of child nodes with given name 950 * @fwnode: Node which child nodes are counted. 951 * @name: String to match child node name against. 952 * 953 * Scan child nodes and count all the nodes with a specific name. Potential 954 * 'number' -ending after the 'at sign' for scanned names is ignored. 955 * E.g.:: 956 * fwnode_get_named_child_node_count(fwnode, "channel"); 957 * would match all the nodes:: 958 * channel { }, channel@0 {}, channel@0xabba {}... 959 * 960 * Return: the number of child nodes with a matching name for a given device. 961 */ 962 unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode, 963 const char *name) 964 { 965 struct fwnode_handle *child; 966 unsigned int count = 0; 967 968 fwnode_for_each_named_child_node(fwnode, child, name) 969 count++; 970 971 return count; 972 } 973 EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count); 974 975 bool device_dma_supported(const struct device *dev) 976 { 977 return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported); 978 } 979 EXPORT_SYMBOL_GPL(device_dma_supported); 980 981 enum dev_dma_attr device_get_dma_attr(const struct device *dev) 982 { 983 if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr)) 984 return DEV_DMA_NOT_SUPPORTED; 985 986 return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr); 987 } 988 EXPORT_SYMBOL_GPL(device_get_dma_attr); 989 990 /** 991 * fwnode_get_phy_mode - Get phy mode for given firmware node 992 * @fwnode: Pointer to the given node 993 * 994 * The function gets phy interface string from property 'phy-mode' or 995 * 'phy-connection-type', and return its index in phy_modes table, or errno in 996 * error case. 997 */ 998 int fwnode_get_phy_mode(const struct fwnode_handle *fwnode) 999 { 1000 const char *pm; 1001 int err, i; 1002 1003 err = fwnode_property_read_string(fwnode, "phy-mode", &pm); 1004 if (err < 0) 1005 err = fwnode_property_read_string(fwnode, 1006 "phy-connection-type", &pm); 1007 if (err < 0) 1008 return err; 1009 1010 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) 1011 if (!strcasecmp(pm, phy_modes(i))) 1012 return i; 1013 1014 return -ENODEV; 1015 } 1016 EXPORT_SYMBOL_GPL(fwnode_get_phy_mode); 1017 1018 /** 1019 * device_get_phy_mode - Get phy mode for given device 1020 * @dev: Pointer to the given device 1021 * 1022 * The function gets phy interface string from property 'phy-mode' or 1023 * 'phy-connection-type', and return its index in phy_modes table, or errno in 1024 * error case. 1025 */ 1026 int device_get_phy_mode(struct device *dev) 1027 { 1028 return fwnode_get_phy_mode(dev_fwnode(dev)); 1029 } 1030 EXPORT_SYMBOL_GPL(device_get_phy_mode); 1031 1032 /** 1033 * fwnode_iomap - Maps the memory mapped IO for a given fwnode 1034 * @fwnode: Pointer to the firmware node 1035 * @index: Index of the IO range 1036 * 1037 * Return: a pointer to the mapped memory. 1038 */ 1039 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index) 1040 { 1041 return fwnode_call_ptr_op(fwnode, iomap, index); 1042 } 1043 EXPORT_SYMBOL(fwnode_iomap); 1044 1045 /** 1046 * fwnode_irq_get - Get IRQ directly from a fwnode 1047 * @fwnode: Pointer to the firmware node 1048 * @index: Zero-based index of the IRQ 1049 * 1050 * Return: Linux IRQ number on success. Negative errno on failure. 1051 */ 1052 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index) 1053 { 1054 int ret; 1055 1056 ret = fwnode_call_int_op(fwnode, irq_get, index); 1057 /* We treat mapping errors as invalid case */ 1058 if (ret == 0) 1059 return -EINVAL; 1060 1061 return ret; 1062 } 1063 EXPORT_SYMBOL(fwnode_irq_get); 1064 1065 /** 1066 * fwnode_irq_get_byname - Get IRQ from a fwnode using its name 1067 * @fwnode: Pointer to the firmware node 1068 * @name: IRQ name 1069 * 1070 * Description: 1071 * Find a match to the string @name in the 'interrupt-names' string array 1072 * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ 1073 * number of the IRQ resource corresponding to the index of the matched 1074 * string. 1075 * 1076 * Return: Linux IRQ number on success, or negative errno otherwise. 1077 */ 1078 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name) 1079 { 1080 int index; 1081 1082 if (!name) 1083 return -EINVAL; 1084 1085 index = fwnode_property_match_string(fwnode, "interrupt-names", name); 1086 if (index < 0) 1087 return index; 1088 1089 return fwnode_irq_get(fwnode, index); 1090 } 1091 EXPORT_SYMBOL(fwnode_irq_get_byname); 1092 1093 /** 1094 * fwnode_graph_get_next_endpoint - Get next endpoint firmware node 1095 * @fwnode: Pointer to the parent firmware node 1096 * @prev: Previous endpoint node or %NULL to get the first 1097 * 1098 * The caller is responsible for calling fwnode_handle_put() on the returned 1099 * fwnode pointer. Note that this function also puts a reference to @prev 1100 * unconditionally. 1101 * 1102 * Return: an endpoint firmware node pointer or %NULL if no more endpoints 1103 * are available. 1104 */ 1105 struct fwnode_handle * 1106 fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode, 1107 struct fwnode_handle *prev) 1108 { 1109 struct fwnode_handle *ep, *port_parent = NULL; 1110 const struct fwnode_handle *parent; 1111 1112 /* 1113 * If this function is in a loop and the previous iteration returned 1114 * an endpoint from fwnode->secondary, then we need to use the secondary 1115 * as parent rather than @fwnode. 1116 */ 1117 if (prev) { 1118 port_parent = fwnode_graph_get_port_parent(prev); 1119 parent = port_parent; 1120 } else { 1121 parent = fwnode; 1122 } 1123 if (IS_ERR_OR_NULL(parent)) 1124 return NULL; 1125 1126 ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev); 1127 if (ep) 1128 goto out_put_port_parent; 1129 1130 ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL); 1131 1132 out_put_port_parent: 1133 fwnode_handle_put(port_parent); 1134 return ep; 1135 } 1136 EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint); 1137 1138 /** 1139 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint 1140 * @endpoint: Endpoint firmware node of the port 1141 * 1142 * The caller is responsible for calling fwnode_handle_put() on the returned 1143 * fwnode pointer. 1144 * 1145 * Return: the firmware node of the device the @endpoint belongs to. 1146 */ 1147 struct fwnode_handle * 1148 fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint) 1149 { 1150 struct fwnode_handle *port, *parent; 1151 1152 port = fwnode_get_parent(endpoint); 1153 parent = fwnode_call_ptr_op(port, graph_get_port_parent); 1154 1155 fwnode_handle_put(port); 1156 1157 return parent; 1158 } 1159 EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent); 1160 1161 /** 1162 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device 1163 * @fwnode: Endpoint firmware node pointing to the remote endpoint 1164 * 1165 * Extracts firmware node of a remote device the @fwnode points to. 1166 * 1167 * The caller is responsible for calling fwnode_handle_put() on the returned 1168 * fwnode pointer. 1169 */ 1170 struct fwnode_handle * 1171 fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode) 1172 { 1173 struct fwnode_handle *endpoint, *parent; 1174 1175 endpoint = fwnode_graph_get_remote_endpoint(fwnode); 1176 parent = fwnode_graph_get_port_parent(endpoint); 1177 1178 fwnode_handle_put(endpoint); 1179 1180 return parent; 1181 } 1182 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent); 1183 1184 /** 1185 * fwnode_graph_get_remote_port - Return fwnode of a remote port 1186 * @fwnode: Endpoint firmware node pointing to the remote endpoint 1187 * 1188 * Extracts firmware node of a remote port the @fwnode points to. 1189 * 1190 * The caller is responsible for calling fwnode_handle_put() on the returned 1191 * fwnode pointer. 1192 */ 1193 struct fwnode_handle * 1194 fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode) 1195 { 1196 return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode)); 1197 } 1198 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port); 1199 1200 /** 1201 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint 1202 * @fwnode: Endpoint firmware node pointing to the remote endpoint 1203 * 1204 * Extracts firmware node of a remote endpoint the @fwnode points to. 1205 * 1206 * The caller is responsible for calling fwnode_handle_put() on the returned 1207 * fwnode pointer. 1208 */ 1209 struct fwnode_handle * 1210 fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode) 1211 { 1212 return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint); 1213 } 1214 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint); 1215 1216 static bool fwnode_graph_remote_available(struct fwnode_handle *ep) 1217 { 1218 struct fwnode_handle *dev_node; 1219 bool available; 1220 1221 dev_node = fwnode_graph_get_remote_port_parent(ep); 1222 available = fwnode_device_is_available(dev_node); 1223 fwnode_handle_put(dev_node); 1224 1225 return available; 1226 } 1227 1228 /** 1229 * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers 1230 * @fwnode: parent fwnode_handle containing the graph 1231 * @port: identifier of the port node 1232 * @endpoint: identifier of the endpoint node under the port node 1233 * @flags: fwnode lookup flags 1234 * 1235 * The caller is responsible for calling fwnode_handle_put() on the returned 1236 * fwnode pointer. 1237 * 1238 * Return: the fwnode handle of the local endpoint corresponding the port and 1239 * endpoint IDs or %NULL if not found. 1240 * 1241 * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint 1242 * has not been found, look for the closest endpoint ID greater than the 1243 * specified one and return the endpoint that corresponds to it, if present. 1244 * 1245 * Does not return endpoints that belong to disabled devices or endpoints that 1246 * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags. 1247 */ 1248 struct fwnode_handle * 1249 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode, 1250 u32 port, u32 endpoint, unsigned long flags) 1251 { 1252 struct fwnode_handle *ep, *best_ep = NULL; 1253 unsigned int best_ep_id = 0; 1254 bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT; 1255 bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED); 1256 1257 fwnode_graph_for_each_endpoint(fwnode, ep) { 1258 struct fwnode_endpoint fwnode_ep = { 0 }; 1259 int ret; 1260 1261 if (enabled_only && !fwnode_graph_remote_available(ep)) 1262 continue; 1263 1264 ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep); 1265 if (ret < 0) 1266 continue; 1267 1268 if (fwnode_ep.port != port) 1269 continue; 1270 1271 if (fwnode_ep.id == endpoint) 1272 return ep; 1273 1274 if (!endpoint_next) 1275 continue; 1276 1277 /* 1278 * If the endpoint that has just been found is not the first 1279 * matching one and the ID of the one found previously is closer 1280 * to the requested endpoint ID, skip it. 1281 */ 1282 if (fwnode_ep.id < endpoint || 1283 (best_ep && best_ep_id < fwnode_ep.id)) 1284 continue; 1285 1286 fwnode_handle_put(best_ep); 1287 best_ep = fwnode_handle_get(ep); 1288 best_ep_id = fwnode_ep.id; 1289 } 1290 1291 return best_ep; 1292 } 1293 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id); 1294 1295 /** 1296 * fwnode_graph_get_endpoint_count - Count endpoints on a device node 1297 * @fwnode: The node related to a device 1298 * @flags: fwnode lookup flags 1299 * Count endpoints in a device node. 1300 * 1301 * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints 1302 * and endpoints connected to disabled devices are counted. 1303 */ 1304 unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode, 1305 unsigned long flags) 1306 { 1307 struct fwnode_handle *ep; 1308 unsigned int count = 0; 1309 1310 fwnode_graph_for_each_endpoint(fwnode, ep) { 1311 if (flags & FWNODE_GRAPH_DEVICE_DISABLED || 1312 fwnode_graph_remote_available(ep)) 1313 count++; 1314 } 1315 1316 return count; 1317 } 1318 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count); 1319 1320 /** 1321 * fwnode_graph_parse_endpoint - parse common endpoint node properties 1322 * @fwnode: pointer to endpoint fwnode_handle 1323 * @endpoint: pointer to the fwnode endpoint data structure 1324 * 1325 * Parse @fwnode representing a graph endpoint node and store the 1326 * information in @endpoint. The caller must hold a reference to 1327 * @fwnode. 1328 */ 1329 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode, 1330 struct fwnode_endpoint *endpoint) 1331 { 1332 memset(endpoint, 0, sizeof(*endpoint)); 1333 1334 return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint); 1335 } 1336 EXPORT_SYMBOL(fwnode_graph_parse_endpoint); 1337 1338 const void *device_get_match_data(const struct device *dev) 1339 { 1340 return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev); 1341 } 1342 EXPORT_SYMBOL_GPL(device_get_match_data); 1343 1344 static unsigned int fwnode_graph_devcon_matches(const struct fwnode_handle *fwnode, 1345 const char *con_id, void *data, 1346 devcon_match_fn_t match, 1347 void **matches, 1348 unsigned int matches_len) 1349 { 1350 struct fwnode_handle *node; 1351 struct fwnode_handle *ep; 1352 unsigned int count = 0; 1353 void *ret; 1354 1355 fwnode_graph_for_each_endpoint(fwnode, ep) { 1356 if (matches && count >= matches_len) { 1357 fwnode_handle_put(ep); 1358 break; 1359 } 1360 1361 node = fwnode_graph_get_remote_port_parent(ep); 1362 if (!fwnode_device_is_available(node)) { 1363 fwnode_handle_put(node); 1364 continue; 1365 } 1366 1367 ret = match(node, con_id, data); 1368 fwnode_handle_put(node); 1369 if (ret) { 1370 if (matches) 1371 matches[count] = ret; 1372 count++; 1373 } 1374 } 1375 return count; 1376 } 1377 1378 static unsigned int fwnode_devcon_matches(const struct fwnode_handle *fwnode, 1379 const char *con_id, void *data, 1380 devcon_match_fn_t match, 1381 void **matches, 1382 unsigned int matches_len) 1383 { 1384 struct fwnode_handle *node; 1385 unsigned int count = 0; 1386 unsigned int i; 1387 void *ret; 1388 1389 for (i = 0; ; i++) { 1390 if (matches && count >= matches_len) 1391 break; 1392 1393 node = fwnode_find_reference(fwnode, con_id, i); 1394 if (IS_ERR(node)) 1395 break; 1396 1397 ret = match(node, NULL, data); 1398 fwnode_handle_put(node); 1399 if (ret) { 1400 if (matches) 1401 matches[count] = ret; 1402 count++; 1403 } 1404 } 1405 1406 return count; 1407 } 1408 1409 /** 1410 * fwnode_connection_find_match - Find connection from a device node 1411 * @fwnode: Device node with the connection 1412 * @con_id: Identifier for the connection 1413 * @data: Data for the match function 1414 * @match: Function to check and convert the connection description 1415 * 1416 * Find a connection with unique identifier @con_id between @fwnode and another 1417 * device node. @match will be used to convert the connection description to 1418 * data the caller is expecting to be returned. 1419 */ 1420 void *fwnode_connection_find_match(const struct fwnode_handle *fwnode, 1421 const char *con_id, void *data, 1422 devcon_match_fn_t match) 1423 { 1424 unsigned int count; 1425 void *ret; 1426 1427 if (!fwnode || !match) 1428 return NULL; 1429 1430 count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1); 1431 if (count) 1432 return ret; 1433 1434 count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1); 1435 return count ? ret : NULL; 1436 } 1437 EXPORT_SYMBOL_GPL(fwnode_connection_find_match); 1438 1439 /** 1440 * fwnode_connection_find_matches - Find connections from a device node 1441 * @fwnode: Device node with the connection 1442 * @con_id: Identifier for the connection 1443 * @data: Data for the match function 1444 * @match: Function to check and convert the connection description 1445 * @matches: (Optional) array of pointers to fill with matches 1446 * @matches_len: Length of @matches 1447 * 1448 * Find up to @matches_len connections with unique identifier @con_id between 1449 * @fwnode and other device nodes. @match will be used to convert the 1450 * connection description to data the caller is expecting to be returned 1451 * through the @matches array. 1452 * 1453 * If @matches is %NULL @matches_len is ignored and the total number of resolved 1454 * matches is returned. 1455 * 1456 * Return: Number of matches resolved, or negative errno. 1457 */ 1458 int fwnode_connection_find_matches(const struct fwnode_handle *fwnode, 1459 const char *con_id, void *data, 1460 devcon_match_fn_t match, 1461 void **matches, unsigned int matches_len) 1462 { 1463 unsigned int count_graph; 1464 unsigned int count_ref; 1465 1466 if (!fwnode || !match) 1467 return -EINVAL; 1468 1469 count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match, 1470 matches, matches_len); 1471 1472 if (matches) { 1473 matches += count_graph; 1474 matches_len -= count_graph; 1475 } 1476 1477 count_ref = fwnode_devcon_matches(fwnode, con_id, data, match, 1478 matches, matches_len); 1479 1480 return count_graph + count_ref; 1481 } 1482 EXPORT_SYMBOL_GPL(fwnode_connection_find_matches); 1483