File Index
Files and directories
Sign in
FILE

property.c

File preview with syntax highlighting.

linux-6.19.14/drivers/base/property.c C
   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
  21struct 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}
  26EXPORT_SYMBOL_GPL(__dev_fwnode);
  27
  28const 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}
  33EXPORT_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 */
  44bool device_property_present(const struct device *dev, const char *propname)
  45{
  46	return fwnode_property_present(dev_fwnode(dev), propname);
  47}
  48EXPORT_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 */
  57bool 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}
  71EXPORT_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 */
  82bool device_property_read_bool(const struct device *dev, const char *propname)
  83{
  84	return fwnode_property_read_bool(dev_fwnode(dev), propname);
  85}
  86EXPORT_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 */
  95bool 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}
 109EXPORT_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 */
 132int 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}
 137EXPORT_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 */
 160int 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}
 165EXPORT_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 */
 188int 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}
 193EXPORT_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 */
 216int 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}
 221EXPORT_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 */
 244int 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}
 249EXPORT_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 */
 266int 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}
 271EXPORT_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 */
 288int 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}
 293EXPORT_SYMBOL_GPL(device_property_match_string);
 294
 295static 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 */
 335int 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}
 341EXPORT_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 */
 364int 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}
 370EXPORT_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 */
 393int 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}
 399EXPORT_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 */
 422int 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}
 428EXPORT_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 */
 451int 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}
 468EXPORT_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 */
 485int 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}
 492EXPORT_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 */
 509int 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
 534out_free:
 535	kfree(values);
 536	return ret;
 537}
 538EXPORT_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 */
 557int 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}
 573EXPORT_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.
 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 */
 598int 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}
 619EXPORT_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 */
 635struct 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}
 646EXPORT_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 */
 654const char *fwnode_get_name(const struct fwnode_handle *fwnode)
 655{
 656	return fwnode_call_ptr_op(fwnode, get_name);
 657}
 658EXPORT_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 */
 667const 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 */
 685bool 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}
 698EXPORT_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 */
 710struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode)
 711{
 712	return fwnode_call_ptr_op(fwnode, get_parent);
 713}
 714EXPORT_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 */
 731struct 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}
 739EXPORT_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 */
 747unsigned 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}
 757EXPORT_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 */
 771struct 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}
 785EXPORT_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 */
 796struct fwnode_handle *
 797fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
 798			   struct fwnode_handle *child)
 799{
 800	struct fwnode_handle *next;
 801
 802	if (IS_ERR_OR_NULL(fwnode))
 803		return NULL;
 804
 805	/* Try to find a child in primary fwnode */
 806	next = fwnode_call_ptr_op(fwnode, get_next_child_node, child);
 807	if (next)
 808		return next;
 809
 810	/* When no more children in primary, continue with secondary */
 811	return fwnode_call_ptr_op(fwnode->secondary, get_next_child_node, child);
 812}
 813EXPORT_SYMBOL_GPL(fwnode_get_next_child_node);
 814
 815/**
 816 * fwnode_get_next_available_child_node - Return the next available child node handle for a node
 817 * @fwnode: Firmware node to find the next child node for.
 818 * @child: Handle to one of the node's child nodes or a %NULL handle.
 819 *
 820 * The caller is responsible for calling fwnode_handle_put() on the returned
 821 * fwnode pointer. Note that this function also puts a reference to @child
 822 * unconditionally.
 823 */
 824struct fwnode_handle *
 825fwnode_get_next_available_child_node(const struct fwnode_handle *fwnode,
 826				     struct fwnode_handle *child)
 827{
 828	struct fwnode_handle *next_child = child;
 829
 830	if (IS_ERR_OR_NULL(fwnode))
 831		return NULL;
 832
 833	do {
 834		next_child = fwnode_get_next_child_node(fwnode, next_child);
 835		if (!next_child)
 836			return NULL;
 837	} while (!fwnode_device_is_available(next_child));
 838
 839	return next_child;
 840}
 841EXPORT_SYMBOL_GPL(fwnode_get_next_available_child_node);
 842
 843/**
 844 * device_get_next_child_node - Return the next child node handle for a device
 845 * @dev: Device to find the next child node for.
 846 * @child: Handle to one of the device's child nodes or a %NULL handle.
 847 *
 848 * The caller is responsible for calling fwnode_handle_put() on the returned
 849 * fwnode pointer. Note that this function also puts a reference to @child
 850 * unconditionally.
 851 */
 852struct fwnode_handle *device_get_next_child_node(const struct device *dev,
 853						 struct fwnode_handle *child)
 854{
 855	return fwnode_get_next_child_node(dev_fwnode(dev), child);
 856}
 857EXPORT_SYMBOL_GPL(device_get_next_child_node);
 858
 859/**
 860 * fwnode_get_named_child_node - Return first matching named child node handle
 861 * @fwnode: Firmware node to find the named child node for.
 862 * @childname: String to match child node name against.
 863 *
 864 * The caller is responsible for calling fwnode_handle_put() on the returned
 865 * fwnode pointer.
 866 */
 867struct fwnode_handle *
 868fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
 869			    const char *childname)
 870{
 871	return fwnode_call_ptr_op(fwnode, get_named_child_node, childname);
 872}
 873EXPORT_SYMBOL_GPL(fwnode_get_named_child_node);
 874
 875/**
 876 * device_get_named_child_node - Return first matching named child node handle
 877 * @dev: Device to find the named child node for.
 878 * @childname: String to match child node name against.
 879 *
 880 * The caller is responsible for calling fwnode_handle_put() on the returned
 881 * fwnode pointer.
 882 */
 883struct fwnode_handle *device_get_named_child_node(const struct device *dev,
 884						  const char *childname)
 885{
 886	return fwnode_get_named_child_node(dev_fwnode(dev), childname);
 887}
 888EXPORT_SYMBOL_GPL(device_get_named_child_node);
 889
 890/**
 891 * fwnode_handle_get - Obtain a reference to a device node
 892 * @fwnode: Pointer to the device node to obtain the reference to.
 893 *
 894 * The caller is responsible for calling fwnode_handle_put() on the returned
 895 * fwnode pointer.
 896 *
 897 * Return: the fwnode handle.
 898 */
 899struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode)
 900{
 901	if (!fwnode_has_op(fwnode, get))
 902		return fwnode;
 903
 904	return fwnode_call_ptr_op(fwnode, get);
 905}
 906EXPORT_SYMBOL_GPL(fwnode_handle_get);
 907
 908/**
 909 * fwnode_device_is_available - check if a device is available for use
 910 * @fwnode: Pointer to the fwnode of the device.
 911 *
 912 * Return: true if device is available for use. Otherwise, returns false.
 913 *
 914 * For fwnode node types that don't implement the .device_is_available()
 915 * operation, this function returns true.
 916 */
 917bool fwnode_device_is_available(const struct fwnode_handle *fwnode)
 918{
 919	if (IS_ERR_OR_NULL(fwnode))
 920		return false;
 921
 922	if (!fwnode_has_op(fwnode, device_is_available))
 923		return true;
 924
 925	return fwnode_call_bool_op(fwnode, device_is_available);
 926}
 927EXPORT_SYMBOL_GPL(fwnode_device_is_available);
 928
 929/**
 930 * fwnode_get_child_node_count - return the number of child nodes for a given firmware node
 931 * @fwnode: Pointer to the parent firmware node
 932 *
 933 * Return: the number of child nodes for a given firmware node.
 934 */
 935unsigned int fwnode_get_child_node_count(const struct fwnode_handle *fwnode)
 936{
 937	struct fwnode_handle *child;
 938	unsigned int count = 0;
 939
 940	fwnode_for_each_child_node(fwnode, child)
 941		count++;
 942
 943	return count;
 944}
 945EXPORT_SYMBOL_GPL(fwnode_get_child_node_count);
 946
 947/**
 948 * fwnode_get_named_child_node_count - number of child nodes with given name
 949 * @fwnode: Node which child nodes are counted.
 950 * @name: String to match child node name against.
 951 *
 952 * Scan child nodes and count all the nodes with a specific name. Potential
 953 * 'number' -ending after the 'at sign' for scanned names is ignored.
 954 * E.g.::
 955 *   fwnode_get_named_child_node_count(fwnode, "channel");
 956 * would match all the nodes::
 957 *   channel { }, channel@0 {}, channel@0xabba {}...
 958 *
 959 * Return: the number of child nodes with a matching name for a given device.
 960 */
 961unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,
 962					       const char *name)
 963{
 964	struct fwnode_handle *child;
 965	unsigned int count = 0;
 966
 967	fwnode_for_each_named_child_node(fwnode, child, name)
 968		count++;
 969
 970	return count;
 971}
 972EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count);
 973
 974bool device_dma_supported(const struct device *dev)
 975{
 976	return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
 977}
 978EXPORT_SYMBOL_GPL(device_dma_supported);
 979
 980enum dev_dma_attr device_get_dma_attr(const struct device *dev)
 981{
 982	if (!fwnode_has_op(dev_fwnode(dev), device_get_dma_attr))
 983		return DEV_DMA_NOT_SUPPORTED;
 984
 985	return fwnode_call_int_op(dev_fwnode(dev), device_get_dma_attr);
 986}
 987EXPORT_SYMBOL_GPL(device_get_dma_attr);
 988
 989/**
 990 * fwnode_get_phy_mode - Get phy mode for given firmware node
 991 * @fwnode:	Pointer to the given node
 992 *
 993 * The function gets phy interface string from property 'phy-mode' or
 994 * 'phy-connection-type', and return its index in phy_modes table, or errno in
 995 * error case.
 996 */
 997int fwnode_get_phy_mode(const struct fwnode_handle *fwnode)
 998{
 999	const char *pm;
1000	int err, i;
1001
1002	err = fwnode_property_read_string(fwnode, "phy-mode", &pm);
1003	if (err < 0)
1004		err = fwnode_property_read_string(fwnode,
1005						  "phy-connection-type", &pm);
1006	if (err < 0)
1007		return err;
1008
1009	for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1010		if (!strcasecmp(pm, phy_modes(i)))
1011			return i;
1012
1013	return -ENODEV;
1014}
1015EXPORT_SYMBOL_GPL(fwnode_get_phy_mode);
1016
1017/**
1018 * device_get_phy_mode - Get phy mode for given device
1019 * @dev:	Pointer to the given device
1020 *
1021 * The function gets phy interface string from property 'phy-mode' or
1022 * 'phy-connection-type', and return its index in phy_modes table, or errno in
1023 * error case.
1024 */
1025int device_get_phy_mode(struct device *dev)
1026{
1027	return fwnode_get_phy_mode(dev_fwnode(dev));
1028}
1029EXPORT_SYMBOL_GPL(device_get_phy_mode);
1030
1031/**
1032 * fwnode_iomap - Maps the memory mapped IO for a given fwnode
1033 * @fwnode:	Pointer to the firmware node
1034 * @index:	Index of the IO range
1035 *
1036 * Return: a pointer to the mapped memory.
1037 */
1038void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index)
1039{
1040	return fwnode_call_ptr_op(fwnode, iomap, index);
1041}
1042EXPORT_SYMBOL(fwnode_iomap);
1043
1044/**
1045 * fwnode_irq_get - Get IRQ directly from a fwnode
1046 * @fwnode:	Pointer to the firmware node
1047 * @index:	Zero-based index of the IRQ
1048 *
1049 * Return: Linux IRQ number on success. Negative errno on failure.
1050 */
1051int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index)
1052{
1053	int ret;
1054
1055	ret = fwnode_call_int_op(fwnode, irq_get, index);
1056	/* We treat mapping errors as invalid case */
1057	if (ret == 0)
1058		return -EINVAL;
1059
1060	return ret;
1061}
1062EXPORT_SYMBOL(fwnode_irq_get);
1063
1064/**
1065 * fwnode_irq_get_byname - Get IRQ from a fwnode using its name
1066 * @fwnode:	Pointer to the firmware node
1067 * @name:	IRQ name
1068 *
1069 * Description:
1070 * Find a match to the string @name in the 'interrupt-names' string array
1071 * in _DSD for ACPI, or of_node for Device Tree. Then get the Linux IRQ
1072 * number of the IRQ resource corresponding to the index of the matched
1073 * string.
1074 *
1075 * Return: Linux IRQ number on success, or negative errno otherwise.
1076 */
1077int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name)
1078{
1079	int index;
1080
1081	if (!name)
1082		return -EINVAL;
1083
1084	index = fwnode_property_match_string(fwnode, "interrupt-names",  name);
1085	if (index < 0)
1086		return index;
1087
1088	return fwnode_irq_get(fwnode, index);
1089}
1090EXPORT_SYMBOL(fwnode_irq_get_byname);
1091
1092/**
1093 * fwnode_graph_get_next_endpoint - Get next endpoint firmware node
1094 * @fwnode: Pointer to the parent firmware node
1095 * @prev: Previous endpoint node or %NULL to get the first
1096 *
1097 * The caller is responsible for calling fwnode_handle_put() on the returned
1098 * fwnode pointer. Note that this function also puts a reference to @prev
1099 * unconditionally.
1100 *
1101 * Return: an endpoint firmware node pointer or %NULL if no more endpoints
1102 * are available.
1103 */
1104struct fwnode_handle *
1105fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1106			       struct fwnode_handle *prev)
1107{
1108	struct fwnode_handle *ep, *port_parent = NULL;
1109	const struct fwnode_handle *parent;
1110
1111	/*
1112	 * If this function is in a loop and the previous iteration returned
1113	 * an endpoint from fwnode->secondary, then we need to use the secondary
1114	 * as parent rather than @fwnode.
1115	 */
1116	if (prev) {
1117		port_parent = fwnode_graph_get_port_parent(prev);
1118		parent = port_parent;
1119	} else {
1120		parent = fwnode;
1121	}
1122	if (IS_ERR_OR_NULL(parent))
1123		return NULL;
1124
1125	ep = fwnode_call_ptr_op(parent, graph_get_next_endpoint, prev);
1126	if (ep)
1127		goto out_put_port_parent;
1128
1129	ep = fwnode_graph_get_next_endpoint(parent->secondary, NULL);
1130
1131out_put_port_parent:
1132	fwnode_handle_put(port_parent);
1133	return ep;
1134}
1135EXPORT_SYMBOL_GPL(fwnode_graph_get_next_endpoint);
1136
1137/**
1138 * fwnode_graph_get_port_parent - Return the device fwnode of a port endpoint
1139 * @endpoint: Endpoint firmware node of the port
1140 *
1141 * The caller is responsible for calling fwnode_handle_put() on the returned
1142 * fwnode pointer.
1143 *
1144 * Return: the firmware node of the device the @endpoint belongs to.
1145 */
1146struct fwnode_handle *
1147fwnode_graph_get_port_parent(const struct fwnode_handle *endpoint)
1148{
1149	struct fwnode_handle *port, *parent;
1150
1151	port = fwnode_get_parent(endpoint);
1152	parent = fwnode_call_ptr_op(port, graph_get_port_parent);
1153
1154	fwnode_handle_put(port);
1155
1156	return parent;
1157}
1158EXPORT_SYMBOL_GPL(fwnode_graph_get_port_parent);
1159
1160/**
1161 * fwnode_graph_get_remote_port_parent - Return fwnode of a remote device
1162 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1163 *
1164 * Extracts firmware node of a remote device the @fwnode points to.
1165 *
1166 * The caller is responsible for calling fwnode_handle_put() on the returned
1167 * fwnode pointer.
1168 */
1169struct fwnode_handle *
1170fwnode_graph_get_remote_port_parent(const struct fwnode_handle *fwnode)
1171{
1172	struct fwnode_handle *endpoint, *parent;
1173
1174	endpoint = fwnode_graph_get_remote_endpoint(fwnode);
1175	parent = fwnode_graph_get_port_parent(endpoint);
1176
1177	fwnode_handle_put(endpoint);
1178
1179	return parent;
1180}
1181EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port_parent);
1182
1183/**
1184 * fwnode_graph_get_remote_port - Return fwnode of a remote port
1185 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1186 *
1187 * Extracts firmware node of a remote port the @fwnode points to.
1188 *
1189 * The caller is responsible for calling fwnode_handle_put() on the returned
1190 * fwnode pointer.
1191 */
1192struct fwnode_handle *
1193fwnode_graph_get_remote_port(const struct fwnode_handle *fwnode)
1194{
1195	return fwnode_get_next_parent(fwnode_graph_get_remote_endpoint(fwnode));
1196}
1197EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_port);
1198
1199/**
1200 * fwnode_graph_get_remote_endpoint - Return fwnode of a remote endpoint
1201 * @fwnode: Endpoint firmware node pointing to the remote endpoint
1202 *
1203 * Extracts firmware node of a remote endpoint the @fwnode points to.
1204 *
1205 * The caller is responsible for calling fwnode_handle_put() on the returned
1206 * fwnode pointer.
1207 */
1208struct fwnode_handle *
1209fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1210{
1211	return fwnode_call_ptr_op(fwnode, graph_get_remote_endpoint);
1212}
1213EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_endpoint);
1214
1215static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
1216{
1217	struct fwnode_handle *dev_node;
1218	bool available;
1219
1220	dev_node = fwnode_graph_get_remote_port_parent(ep);
1221	available = fwnode_device_is_available(dev_node);
1222	fwnode_handle_put(dev_node);
1223
1224	return available;
1225}
1226
1227/**
1228 * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
1229 * @fwnode: parent fwnode_handle containing the graph
1230 * @port: identifier of the port node
1231 * @endpoint: identifier of the endpoint node under the port node
1232 * @flags: fwnode lookup flags
1233 *
1234 * The caller is responsible for calling fwnode_handle_put() on the returned
1235 * fwnode pointer.
1236 *
1237 * Return: the fwnode handle of the local endpoint corresponding the port and
1238 * endpoint IDs or %NULL if not found.
1239 *
1240 * If FWNODE_GRAPH_ENDPOINT_NEXT is passed in @flags and the specified endpoint
1241 * has not been found, look for the closest endpoint ID greater than the
1242 * specified one and return the endpoint that corresponds to it, if present.
1243 *
1244 * Does not return endpoints that belong to disabled devices or endpoints that
1245 * are unconnected, unless FWNODE_GRAPH_DEVICE_DISABLED is passed in @flags.
1246 */
1247struct fwnode_handle *
1248fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
1249				u32 port, u32 endpoint, unsigned long flags)
1250{
1251	struct fwnode_handle *ep, *best_ep = NULL;
1252	unsigned int best_ep_id = 0;
1253	bool endpoint_next = flags & FWNODE_GRAPH_ENDPOINT_NEXT;
1254	bool enabled_only = !(flags & FWNODE_GRAPH_DEVICE_DISABLED);
1255
1256	fwnode_graph_for_each_endpoint(fwnode, ep) {
1257		struct fwnode_endpoint fwnode_ep = { 0 };
1258		int ret;
1259
1260		if (enabled_only && !fwnode_graph_remote_available(ep))
1261			continue;
1262
1263		ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
1264		if (ret < 0)
1265			continue;
1266
1267		if (fwnode_ep.port != port)
1268			continue;
1269
1270		if (fwnode_ep.id == endpoint)
1271			return ep;
1272
1273		if (!endpoint_next)
1274			continue;
1275
1276		/*
1277		 * If the endpoint that has just been found is not the first
1278		 * matching one and the ID of the one found previously is closer
1279		 * to the requested endpoint ID, skip it.
1280		 */
1281		if (fwnode_ep.id < endpoint ||
1282		    (best_ep && best_ep_id < fwnode_ep.id))
1283			continue;
1284
1285		fwnode_handle_put(best_ep);
1286		best_ep = fwnode_handle_get(ep);
1287		best_ep_id = fwnode_ep.id;
1288	}
1289
1290	return best_ep;
1291}
1292EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
1293
1294/**
1295 * fwnode_graph_get_endpoint_count - Count endpoints on a device node
1296 * @fwnode: The node related to a device
1297 * @flags: fwnode lookup flags
1298 * Count endpoints in a device node.
1299 *
1300 * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
1301 * and endpoints connected to disabled devices are counted.
1302 */
1303unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode,
1304					     unsigned long flags)
1305{
1306	struct fwnode_handle *ep;
1307	unsigned int count = 0;
1308
1309	fwnode_graph_for_each_endpoint(fwnode, ep) {
1310		if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
1311		    fwnode_graph_remote_available(ep))
1312			count++;
1313	}
1314
1315	return count;
1316}
1317EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
1318
1319/**
1320 * fwnode_graph_parse_endpoint - parse common endpoint node properties
1321 * @fwnode: pointer to endpoint fwnode_handle
1322 * @endpoint: pointer to the fwnode endpoint data structure
1323 *
1324 * Parse @fwnode representing a graph endpoint node and store the
1325 * information in @endpoint. The caller must hold a reference to
1326 * @fwnode.
1327 */
1328int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1329				struct fwnode_endpoint *endpoint)
1330{
1331	memset(endpoint, 0, sizeof(*endpoint));
1332
1333	return fwnode_call_int_op(fwnode, graph_parse_endpoint, endpoint);
1334}
1335EXPORT_SYMBOL(fwnode_graph_parse_endpoint);
1336
1337const void *device_get_match_data(const struct device *dev)
1338{
1339	return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
1340}
1341EXPORT_SYMBOL_GPL(device_get_match_data);
1342
1343static unsigned int fwnode_graph_devcon_matches(const struct fwnode_handle *fwnode,
1344						const char *con_id, void *data,
1345						devcon_match_fn_t match,
1346						void **matches,
1347						unsigned int matches_len)
1348{
1349	struct fwnode_handle *node;
1350	struct fwnode_handle *ep;
1351	unsigned int count = 0;
1352	void *ret;
1353
1354	fwnode_graph_for_each_endpoint(fwnode, ep) {
1355		if (matches && count >= matches_len) {
1356			fwnode_handle_put(ep);
1357			break;
1358		}
1359
1360		node = fwnode_graph_get_remote_port_parent(ep);
1361		if (!fwnode_device_is_available(node)) {
1362			fwnode_handle_put(node);
1363			continue;
1364		}
1365
1366		ret = match(node, con_id, data);
1367		fwnode_handle_put(node);
1368		if (ret) {
1369			if (matches)
1370				matches[count] = ret;
1371			count++;
1372		}
1373	}
1374	return count;
1375}
1376
1377static unsigned int fwnode_devcon_matches(const struct fwnode_handle *fwnode,
1378					  const char *con_id, void *data,
1379					  devcon_match_fn_t match,
1380					  void **matches,
1381					  unsigned int matches_len)
1382{
1383	struct fwnode_handle *node;
1384	unsigned int count = 0;
1385	unsigned int i;
1386	void *ret;
1387
1388	for (i = 0; ; i++) {
1389		if (matches && count >= matches_len)
1390			break;
1391
1392		node = fwnode_find_reference(fwnode, con_id, i);
1393		if (IS_ERR(node))
1394			break;
1395
1396		ret = match(node, NULL, data);
1397		fwnode_handle_put(node);
1398		if (ret) {
1399			if (matches)
1400				matches[count] = ret;
1401			count++;
1402		}
1403	}
1404
1405	return count;
1406}
1407
1408/**
1409 * fwnode_connection_find_match - Find connection from a device node
1410 * @fwnode: Device node with the connection
1411 * @con_id: Identifier for the connection
1412 * @data: Data for the match function
1413 * @match: Function to check and convert the connection description
1414 *
1415 * Find a connection with unique identifier @con_id between @fwnode and another
1416 * device node. @match will be used to convert the connection description to
1417 * data the caller is expecting to be returned.
1418 */
1419void *fwnode_connection_find_match(const struct fwnode_handle *fwnode,
1420				   const char *con_id, void *data,
1421				   devcon_match_fn_t match)
1422{
1423	unsigned int count;
1424	void *ret;
1425
1426	if (!fwnode || !match)
1427		return NULL;
1428
1429	count = fwnode_graph_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1430	if (count)
1431		return ret;
1432
1433	count = fwnode_devcon_matches(fwnode, con_id, data, match, &ret, 1);
1434	return count ? ret : NULL;
1435}
1436EXPORT_SYMBOL_GPL(fwnode_connection_find_match);
1437
1438/**
1439 * fwnode_connection_find_matches - Find connections from a device node
1440 * @fwnode: Device node with the connection
1441 * @con_id: Identifier for the connection
1442 * @data: Data for the match function
1443 * @match: Function to check and convert the connection description
1444 * @matches: (Optional) array of pointers to fill with matches
1445 * @matches_len: Length of @matches
1446 *
1447 * Find up to @matches_len connections with unique identifier @con_id between
1448 * @fwnode and other device nodes. @match will be used to convert the
1449 * connection description to data the caller is expecting to be returned
1450 * through the @matches array.
1451 *
1452 * If @matches is %NULL @matches_len is ignored and the total number of resolved
1453 * matches is returned.
1454 *
1455 * Return: Number of matches resolved, or negative errno.
1456 */
1457int fwnode_connection_find_matches(const struct fwnode_handle *fwnode,
1458				   const char *con_id, void *data,
1459				   devcon_match_fn_t match,
1460				   void **matches, unsigned int matches_len)
1461{
1462	unsigned int count_graph;
1463	unsigned int count_ref;
1464
1465	if (!fwnode || !match)
1466		return -EINVAL;
1467
1468	count_graph = fwnode_graph_devcon_matches(fwnode, con_id, data, match,
1469						  matches, matches_len);
1470
1471	if (matches) {
1472		matches += count_graph;
1473		matches_len -= count_graph;
1474	}
1475
1476	count_ref = fwnode_devcon_matches(fwnode, con_id, data, match,
1477					  matches, matches_len);
1478
1479	return count_graph + count_ref;
1480}
1481EXPORT_SYMBOL_GPL(fwnode_connection_find_matches);
~/ Read-only file preview.