1 /* 2 * SPDX-License-Identifier: Apache-2.0 3 * Copyright (c) 2020 Nordic Semiconductor 4 * Copyright (c) 2020, Linaro Ltd. 5 * 6 * Not a generated file. Feel free to modify. 7 */ 8 9 /** 10 * @file 11 * @brief Devicetree main header 12 * 13 * API for accessing the current application's devicetree macros. 14 */ 15 16 #ifndef DEVICETREE_H 17 #define DEVICETREE_H 18 19 #include <devicetree_unfixed.h> 20 #include <devicetree_fixups.h> 21 22 #include <sys/util.h> 23 24 /** 25 * @brief devicetree.h API 26 * @defgroup devicetree Devicetree 27 * @{ 28 * @} 29 */ 30 31 /* 32 * Property suffixes 33 * ----------------- 34 * 35 * These are the optional parts that come after the _P_<property> 36 * part in DT_N_<path-id>_P_<property-id> macros, or the "prop-suf" 37 * nonterminal in the DT guide's macros.bnf file. 38 * 39 * Before adding new ones, check this list to avoid conflicts. If any 40 * are missing from this list, please add them. It should be complete. 41 * 42 * _ENUM_IDX: property's value as an index into bindings enum 43 * _ENUM_TOKEN: property's value as a token into bindings enum (string 44 * enum values are identifiers) [deprecated, use _STRING_TOKEN] 45 * _ENUM_UPPER_TOKEN: like _ENUM_TOKEN, but uppercased [deprecated, use 46 * _STRING_UPPER_TOKEN] 47 * _EXISTS: property is defined 48 * _FOREACH_PROP_ELEM: helper for "iterating" over values in the property 49 * _FOREACH_PROP_ELEM_VARGS: foreach functions with variable number of arguments 50 * _IDX_<i>: logical index into property 51 * _IDX_<i>_EXISTS: logical index into property is defined 52 * _IDX_<i>_PH: phandle array's phandle by index (or phandle, phandles) 53 * _IDX_<i>_VAL_<val>: phandle array's specifier value by index 54 * _IDX_<i>_VAL_<val>_EXISTS: cell value exists, by index 55 * _LEN: property logical length 56 * _NAME_<name>_PH: phandle array's phandle by name 57 * _NAME_<name>_VAL_<val>: phandle array's property specifier by name 58 * _NAME_<name>_VAL_<val>_EXISTS: cell value exists, by name 59 * _STRING_TOKEN: string property's value as a token 60 * _STRING_UPPER_TOKEN: like _STRING_TOKEN, but uppercased 61 */ 62 63 /** 64 * @defgroup devicetree-generic-id Node identifiers and helpers 65 * @ingroup devicetree 66 * @{ 67 */ 68 69 /** 70 * @brief Name for an invalid node identifier 71 * 72 * This supports cases where factored macros can be invoked from paths where 73 * devicetree data may or may not be available. It is a preprocessor identifier 74 * that does not match any valid devicetree node identifier. 75 */ 76 #define DT_INVALID_NODE _ 77 78 /** 79 * @brief Node identifier for the root node in the devicetree 80 */ 81 #define DT_ROOT DT_N 82 83 /** 84 * @brief Get a node identifier for a devicetree path 85 * 86 * (This macro returns a node identifier from path components. To get 87 * a path string from a node identifier, use DT_NODE_PATH() instead.) 88 * 89 * The arguments to this macro are the names of non-root nodes in the 90 * tree required to reach the desired node, starting from the root. 91 * Non-alphanumeric characters in each name must be converted to 92 * underscores to form valid C tokens, and letters must be lowercased. 93 * 94 * Example devicetree fragment: 95 * 96 * / { 97 * soc { 98 * serial1: serial@40001000 { 99 * status = "okay"; 100 * current-speed = <115200>; 101 * ... 102 * }; 103 * }; 104 * }; 105 * 106 * You can use DT_PATH(soc, serial_40001000) to get a node identifier 107 * for the serial@40001000 node. Node labels like "serial1" cannot be 108 * used as DT_PATH() arguments; use DT_NODELABEL() for those instead. 109 * 110 * Example usage with DT_PROP() to get the current-speed property: 111 * 112 * DT_PROP(DT_PATH(soc, serial_40001000), current_speed) // 115200 113 * 114 * (The current-speed property is also in "lowercase-and-underscores" 115 * form when used with this API.) 116 * 117 * When determining arguments to DT_PATH(): 118 * 119 * - the first argument corresponds to a child node of the root ("soc" above) 120 * - a second argument corresponds to a child of the first argument 121 * ("serial_40001000" above, from the node name "serial@40001000" 122 * after lowercasing and changing "@" to "_") 123 * - and so on for deeper nodes in the desired node's path 124 * 125 * @param ... lowercase-and-underscores node names along the node's path, 126 * with each name given as a separate argument 127 * @return node identifier for the node with that path 128 */ 129 #define DT_PATH(...) DT_PATH_INTERNAL(__VA_ARGS__) 130 131 /** 132 * @brief Get a node identifier for a node label 133 * 134 * Convert non-alphanumeric characters in the node label to 135 * underscores to form valid C tokens, and lowercase all letters. Note 136 * that node labels are not the same thing as label properties. 137 * 138 * Example devicetree fragment: 139 * 140 * serial1: serial@40001000 { 141 * label = "UART_0"; 142 * status = "okay"; 143 * current-speed = <115200>; 144 * ... 145 * }; 146 * 147 * The only node label in this example is "serial1". 148 * 149 * The string "UART_0" is *not* a node label; it's the value of a 150 * property named label. 151 * 152 * You can use DT_NODELABEL(serial1) to get a node identifier for the 153 * serial@40001000 node. Example usage with DT_PROP() to get the 154 * current-speed property: 155 * 156 * DT_PROP(DT_NODELABEL(serial1), current_speed) // 115200 157 * 158 * Another example devicetree fragment: 159 * 160 * cpu@0 { 161 * L2_0: l2-cache { 162 * cache-level = <2>; 163 * ... 164 * }; 165 * }; 166 * 167 * Example usage to get the cache-level property: 168 * 169 * DT_PROP(DT_NODELABEL(l2_0), cache_level) // 2 170 * 171 * Notice how "L2_0" in the devicetree is lowercased to "l2_0" in the 172 * DT_NODELABEL() argument. 173 * 174 * @param label lowercase-and-underscores node label name 175 * @return node identifier for the node with that label 176 */ 177 #define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label) 178 179 /** 180 * @brief Get a node identifier from /aliases 181 * 182 * This macro's argument is a property of the /aliases node. It 183 * returns a node identifier for the node which is aliased. Convert 184 * non-alphanumeric characters in the alias property to underscores to 185 * form valid C tokens, and lowercase all letters. 186 * 187 * Example devicetree fragment: 188 * 189 * / { 190 * aliases { 191 * my-serial = &serial1; 192 * }; 193 * 194 * soc { 195 * serial1: serial@40001000 { 196 * status = "okay"; 197 * current-speed = <115200>; 198 * ... 199 * }; 200 * }; 201 * }; 202 * 203 * You can use DT_ALIAS(my_serial) to get a node identifier for the 204 * serial@40001000 node. Notice how my-serial in the devicetree 205 * becomes my_serial in the DT_ALIAS() argument. Example usage with 206 * DT_PROP() to get the current-speed property: 207 * 208 * DT_PROP(DT_ALIAS(my_serial), current_speed) // 115200 209 * 210 * @param alias lowercase-and-underscores alias name. 211 * @return node identifier for the node with that alias 212 */ 213 #define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias) 214 215 /** 216 * @brief Get a node identifier for an instance of a compatible 217 * 218 * All nodes with a particular compatible property value are assigned 219 * instance numbers, which are zero-based indexes specific to that 220 * compatible. You can get a node identifier for these nodes by 221 * passing DT_INST() an instance number, "inst", along with the 222 * lowercase-and-underscores version of the compatible, "compat". 223 * 224 * Instance numbers have the following properties: 225 * 226 * - for each compatible, instance numbers start at 0 and are contiguous 227 * - exactly one instance number is assigned for each node with a compatible, 228 * **including disabled nodes** 229 * - enabled nodes (status property is "okay" or missing) are assigned the 230 * instance numbers starting from 0, and disabled nodes have instance 231 * numbers which are greater than those of any enabled node 232 * 233 * No other guarantees are made. In particular: 234 * 235 * - instance numbers **in no way reflect** any numbering scheme that 236 * might exist in SoC documentation, node labels or unit addresses, 237 * or properties of the /aliases node (use DT_NODELABEL() or DT_ALIAS() 238 * for those) 239 * - there **is no general guarantee** that the same node will have 240 * the same instance number between builds, even if you are building 241 * the same application again in the same build directory 242 * 243 * Example devicetree fragment: 244 * 245 * serial1: serial@40001000 { 246 * compatible = "vnd,soc-serial"; 247 * status = "disabled"; 248 * current-speed = <9600>; 249 * ... 250 * }; 251 * 252 * serial2: serial@40002000 { 253 * compatible = "vnd,soc-serial"; 254 * status = "okay"; 255 * current-speed = <57600>; 256 * ... 257 * }; 258 * 259 * serial3: serial@40003000 { 260 * compatible = "vnd,soc-serial"; 261 * current-speed = <115200>; 262 * ... 263 * }; 264 * 265 * Assuming no other nodes in the devicetree have compatible 266 * "vnd,soc-serial", that compatible has nodes with instance numbers 267 * 0, 1, and 2. 268 * 269 * The nodes serial@40002000 and serial@40003000 are both enabled, so 270 * their instance numbers are 0 and 1, but no guarantees are made 271 * regarding which node has which instance number. 272 * 273 * Since serial@40001000 is the only disabled node, it has instance 274 * number 2, since disabled nodes are assigned the largest instance 275 * numbers. Therefore: 276 * 277 * // Could be 57600 or 115200. There is no way to be sure: 278 * // either serial@40002000 or serial@40003000 could 279 * // have instance number 0, so this could be the current-speed 280 * // property of either of those nodes. 281 * DT_PROP(DT_INST(0, vnd_soc_serial), current_speed) 282 * 283 * // Could be 57600 or 115200, for the same reason. 284 * // If the above expression expands to 57600, then 285 * // this expands to 115200, and vice-versa. 286 * DT_PROP(DT_INST(1, vnd_soc_serial), current_speed) 287 * 288 * // 9600, because there is only one disabled node, and 289 * // disabled nodes are "at the the end" of the instance 290 * // number "list". 291 * DT_PROP(DT_INST(2, vnd_soc_serial), current_speed) 292 * 293 * Notice how "vnd,soc-serial" in the devicetree becomes vnd_soc_serial 294 * (without quotes) in the DT_INST() arguments. (As usual, current-speed 295 * in the devicetree becomes current_speed as well.) 296 * 297 * Nodes whose "compatible" property has multiple values are assigned 298 * independent instance numbers for each compatible. 299 * 300 * @param inst instance number for compatible "compat" 301 * @param compat lowercase-and-underscores compatible, without quotes 302 * @return node identifier for the node with that instance number and 303 * compatible 304 */ 305 #define DT_INST(inst, compat) UTIL_CAT(DT_N_INST, DT_DASH(inst, compat)) 306 307 /** 308 * @brief Get a node identifier for a parent node 309 * 310 * Example devicetree fragment: 311 * 312 * parent: parent-node { 313 * child: child-node { 314 * ... 315 * }; 316 * }; 317 * 318 * The following are equivalent ways to get the same node identifier: 319 * 320 * DT_NODELABEL(parent) 321 * DT_PARENT(DT_NODELABEL(child)) 322 * 323 * @param node_id node identifier 324 * @return a node identifier for the node's parent 325 */ 326 #define DT_PARENT(node_id) UTIL_CAT(node_id, _PARENT) 327 328 /** 329 * @brief Get a node identifier for a grandparent node 330 * 331 * Example devicetree fragment: 332 * 333 * gparent: grandparent-node { 334 * parent: parent-node { 335 * child: child-node { ... } 336 * }; 337 * }; 338 * 339 * The following are equivalent ways to get the same node identifier: 340 * 341 * DT_GPARENT(DT_NODELABEL(child)) 342 * DT_PARENT(DT_PARENT(DT_NODELABEL(child)) 343 * 344 * @param node_id node identifier 345 * @return a node identifier for the node's parent's parent 346 */ 347 #define DT_GPARENT(node_id) DT_PARENT(DT_PARENT(node_id)) 348 349 /** 350 * @brief Get a node identifier for a child node 351 * 352 * Example devicetree fragment: 353 * 354 * / { 355 * soc-label: soc { 356 * serial1: serial@40001000 { 357 * status = "okay"; 358 * current-speed = <115200>; 359 * ... 360 * }; 361 * }; 362 * }; 363 * 364 * Example usage with @ref DT_PROP() to get the status of the 365 * serial@40001000 node: 366 * 367 * #define SOC_NODE DT_NODELABEL(soc_label) 368 * DT_PROP(DT_CHILD(SOC_NODE, serial_40001000), status) // "okay" 369 * 370 * Node labels like "serial1" cannot be used as the "child" argument 371 * to this macro. Use DT_NODELABEL() for that instead. 372 * 373 * You can also use DT_FOREACH_CHILD() to iterate over node 374 * identifiers for all of a node's children. 375 * 376 * @param node_id node identifier 377 * @param child lowercase-and-underscores child node name 378 * @return node identifier for the node with the name referred to by 'child' 379 */ 380 #define DT_CHILD(node_id, child) UTIL_CAT(node_id, DT_S_PREFIX(child)) 381 382 /** 383 * @brief Get a node identifier for a status "okay" node with a compatible 384 * 385 * Use this if you want to get an arbitrary enabled node with a given 386 * compatible, and you do not care which one you get. If any enabled 387 * nodes with the given compatible exist, a node identifier for one 388 * of them is returned. Otherwise, @p DT_INVALID_NODE is returned. 389 * 390 * Example devicetree fragment: 391 * 392 * node-a { 393 * compatible = "vnd,device"; 394 * status = "okay"; 395 * }; 396 * 397 * node-b { 398 * compatible = "vnd,device"; 399 * status = "okay"; 400 * }; 401 * 402 * node-c { 403 * compatible = "vnd,device"; 404 * status = "disabled"; 405 * }; 406 * 407 * Example usage: 408 * 409 * DT_COMPAT_GET_ANY_STATUS_OKAY(vnd_device) 410 * 411 * This expands to a node identifier for either @p node-a or @p 412 * node-b. It will not expand to a node identifier for @p node-c, 413 * because that node does not have status "okay". 414 * 415 * @param compat lowercase-and-underscores compatible, without quotes 416 * @return node identifier for a node with that compatible, or DT_INVALID_NODE 417 */ 418 #define DT_COMPAT_GET_ANY_STATUS_OKAY(compat) \ 419 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \ 420 (DT_INST(0, compat)), \ 421 (DT_INVALID_NODE)) 422 423 /** 424 * @brief Get a devicetree node's full path as a string literal 425 * 426 * This returns the path to a node from a node identifier. To get a 427 * node identifier from path components instead, use DT_PATH(). 428 * 429 * Example devicetree fragment: 430 * 431 * / { 432 * soc { 433 * node: my-node@12345678 { ... }; 434 * }; 435 * }; 436 * 437 * Example usage: 438 * 439 * DT_NODE_PATH(DT_NODELABEL(node)) // "/soc/my-node@12345678" 440 * DT_NODE_PATH(DT_PATH(soc)) // "/soc" 441 * DT_NODE_PATH(DT_ROOT) // "/" 442 * 443 * @param node_id node identifier 444 * @return the node's full path in the devicetree 445 */ 446 #define DT_NODE_PATH(node_id) DT_CAT(node_id, _PATH) 447 448 /** 449 * @brief Get a devicetree node's name with unit-address as a string literal 450 * 451 * This returns the node name and unit-address from a node identifier. 452 * 453 * Example devicetree fragment: 454 * 455 * / { 456 * soc { 457 * node: my-node@12345678 { ... }; 458 * }; 459 * }; 460 * 461 * Example usage: 462 * 463 * DT_NODE_FULL_NAME(DT_NODELABEL(node)) // "my-node@12345678" 464 * 465 * @param node_id node identifier 466 * @return the node's name with unit-address as a string in the devicetree 467 */ 468 #define DT_NODE_FULL_NAME(node_id) DT_CAT(node_id, _FULL_NAME) 469 470 /** 471 * @brief Do node_id1 and node_id2 refer to the same node? 472 * 473 * Both "node_id1" and "node_id2" must be node identifiers for nodes 474 * that exist in the devicetree (if unsure, you can check with 475 * DT_NODE_EXISTS()). 476 * 477 * The expansion evaluates to 0 or 1, but may not be a literal integer 478 * 0 or 1. 479 * 480 * @param node_id1 first node identifer 481 * @param node_id2 second node identifier 482 * @return an expression that evaluates to 1 if the node identifiers 483 * refer to the same node, and evaluates to 0 otherwise 484 */ 485 #define DT_SAME_NODE(node_id1, node_id2) \ 486 (DT_DEP_ORD(node_id1) == (DT_DEP_ORD(node_id2))) 487 488 /* Implementation note: distinct nodes have distinct node identifiers. 489 * See include/devicetree/ordinals.h. */ 490 491 /** 492 * @} 493 */ 494 495 /** 496 * @defgroup devicetree-generic-prop Property accessors 497 * @ingroup devicetree 498 * @{ 499 */ 500 501 /** 502 * @brief Get a devicetree property value 503 * 504 * For properties whose bindings have the following types, this macro 505 * expands to: 506 * 507 * - string: a string literal 508 * - boolean: 0 if the property is false, or 1 if it is true 509 * - int: the property's value as an integer literal 510 * - array, uint8-array, string-array: an initializer expression in braces, 511 * whose elements are integer or string literals (like {0, 1, 2}, 512 * {"hello", "world"}, etc.) 513 * - phandle: a node identifier for the node with that phandle 514 * 515 * A property's type is usually defined by its binding. In some 516 * special cases, it has an assumed type defined by the devicetree 517 * specification even when no binding is available: "compatible" has 518 * type string-array, "status" and "label" have type string, and 519 * "interrupt-controller" has type boolean. 520 * 521 * For other properties or properties with unknown type due to a 522 * missing binding, behavior is undefined. 523 * 524 * For usage examples, see @ref DT_PATH(), @ref DT_ALIAS(), @ref 525 * DT_NODELABEL(), and @ref DT_INST() above. 526 * 527 * @param node_id node identifier 528 * @param prop lowercase-and-underscores property name 529 * @return a representation of the property's value 530 */ 531 #define DT_PROP(node_id, prop) DT_CAT(node_id, _P_##prop) 532 533 /** 534 * @brief Get a property's logical length 535 * 536 * Here, "length" is a number of elements, which may differ from the 537 * property's size in bytes. 538 * 539 * The return value depends on the property's type: 540 * 541 * - for types array, string-array, and uint8-array, this expands 542 * to the number of elements in the array 543 * - for type phandles, this expands to the number of phandles 544 * - for type phandle-array, this expands to the number of 545 * phandle and specifier blocks in the property 546 * 547 * These properties are handled as special cases: 548 * 549 * - reg property: use DT_NUM_REGS(node_id) instead 550 * - interrupts property: use DT_NUM_IRQS(node_id) instead 551 * 552 * It is an error to use this macro with the reg or interrupts properties. 553 * 554 * For other properties, behavior is undefined. 555 * 556 * @param node_id node identifier 557 * @param prop a lowercase-and-underscores property with a logical length 558 * @return the property's length 559 */ 560 #define DT_PROP_LEN(node_id, prop) DT_PROP(node_id, prop##_LEN) 561 562 /** 563 * @brief Like DT_PROP_LEN(), but with a fallback to default_value 564 * 565 * If the property is defined (as determined by DT_NODE_HAS_PROP()), 566 * this expands to DT_PROP_LEN(node_id, prop). The default_value 567 * parameter is not expanded in this case. 568 * 569 * Otherwise, this expands to default_value. 570 * 571 * @param node_id node identifier 572 * @param prop a lowercase-and-underscores property with a logical length 573 * @param default_value a fallback value to expand to 574 * @return the property's length or the given default value 575 */ 576 #define DT_PROP_LEN_OR(node_id, prop, default_value) \ 577 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \ 578 (DT_PROP_LEN(node_id, prop)), (default_value)) 579 580 /** 581 * @brief Is index "idx" valid for an array type property? 582 * 583 * If this returns 1, then DT_PROP_BY_IDX(node_id, prop, idx) or 584 * DT_PHA_BY_IDX(node_id, prop, idx, ...) are valid at index "idx". 585 * If it returns 0, it is an error to use those macros with that index. 586 * 587 * These properties are handled as special cases: 588 * 589 * - reg property: use DT_REG_HAS_IDX(node_id, idx) instead 590 * - interrupts property: use DT_IRQ_HAS_IDX(node_id, idx) instead 591 * 592 * It is an error to use this macro with the reg or interrupts properties. 593 * 594 * @param node_id node identifier 595 * @param prop a lowercase-and-underscores property with a logical length 596 * @param idx index to check 597 * @return An expression which evaluates to 1 if "idx" is a valid index 598 * into the given property, and 0 otherwise. 599 */ 600 #define DT_PROP_HAS_IDX(node_id, prop, idx) \ 601 IS_ENABLED(DT_CAT6(node_id, _P_, prop, _IDX_, idx, _EXISTS)) 602 603 /** 604 * @brief Get the value at index "idx" in an array type property 605 * 606 * It might help to read the argument order as being similar to 607 * "node->property[index]". 608 * 609 * When the property's binding has type array, string-array, 610 * uint8-array, or phandles, this expands to the idx-th array element 611 * as an integer, string literal, or node identifier respectively. 612 * 613 * These properties are handled as special cases: 614 * 615 * - reg property: use DT_REG_ADDR_BY_IDX() or DT_REG_SIZE_BY_IDX() instead 616 * - interrupts property: use DT_IRQ_BY_IDX() instead 617 * 618 * For non-array properties, behavior is undefined. 619 * 620 * @param node_id node identifier 621 * @param prop lowercase-and-underscores property name 622 * @param idx the index to get 623 * @return a representation of the idx-th element of the property 624 */ 625 #define DT_PROP_BY_IDX(node_id, prop, idx) DT_PROP(node_id, prop##_IDX_##idx) 626 627 /** 628 * @brief Like DT_PROP(), but with a fallback to default_value 629 * 630 * If the value exists, this expands to DT_PROP(node_id, prop). 631 * The default_value parameter is not expanded in this case. 632 * 633 * Otherwise, this expands to default_value. 634 * 635 * @param node_id node identifier 636 * @param prop lowercase-and-underscores property name 637 * @param default_value a fallback value to expand to 638 * @return the property's value or default_value 639 */ 640 #define DT_PROP_OR(node_id, prop, default_value) \ 641 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \ 642 (DT_PROP(node_id, prop)), (default_value)) 643 644 /** 645 * @brief Equivalent to DT_PROP(node_id, label) 646 * 647 * This is a convenience for the Zephyr device API, which uses label 648 * properties as device_get_binding() arguments. 649 * @param node_id node identifier 650 * @return node's label property value 651 */ 652 #define DT_LABEL(node_id) DT_PROP(node_id, label) 653 654 /** 655 * @brief Get a property value's index into its enumeration values 656 * 657 * The return values start at zero. 658 * 659 * Example devicetree fragment: 660 * 661 * usb1: usb@12340000 { 662 * maximum-speed = "full-speed"; 663 * }; 664 * usb2: usb@12341000 { 665 * maximum-speed = "super-speed"; 666 * }; 667 * 668 * Example bindings fragment: 669 * 670 * properties: 671 * maximum-speed: 672 * type: string 673 * enum: 674 * - "low-speed" 675 * - "full-speed" 676 * - "high-speed" 677 * - "super-speed" 678 * 679 * Example usage: 680 * 681 * DT_ENUM_IDX(DT_NODELABEL(usb1), maximum_speed) // 1 682 * DT_ENUM_IDX(DT_NODELABEL(usb2), maximum_speed) // 3 683 * 684 * @param node_id node identifier 685 * @param prop lowercase-and-underscores property name 686 * @return zero-based index of the property's value in its enum: list 687 */ 688 #define DT_ENUM_IDX(node_id, prop) DT_PROP(node_id, prop##_ENUM_IDX) 689 690 /** 691 * @brief Like DT_ENUM_IDX(), but with a fallback to a default enum index 692 * 693 * If the value exists, this expands to its zero based index value thanks to 694 * DT_ENUM_IDX(node_id, prop). 695 * 696 * Otherwise, this expands to provided default index enum value. 697 * 698 * @param node_id node identifier 699 * @param prop lowercase-and-underscores property name 700 * @param default_idx_value a fallback index value to expand to 701 * @return zero-based index of the property's value in its enum if present, 702 * default_idx_value ohterwise 703 */ 704 #define DT_ENUM_IDX_OR(node_id, prop, default_idx_value) \ 705 COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \ 706 (DT_ENUM_IDX(node_id, prop)), (default_idx_value)) 707 708 /** 709 * @brief Get a string property's value as a token. 710 * 711 * This removes "the quotes" from string-valued properties, and converts 712 * non-alphanumeric characters to underscores. That can be useful, for example, 713 * when programmatically using the value to form a C variable or code. 714 * 715 * DT_STRING_TOKEN() can only be used for properties with string type. 716 * 717 * It is an error to use DT_STRING_TOKEN() in other circumstances. 718 * 719 * Example devicetree fragment: 720 * 721 * n1: node-1 { 722 * prop = "foo"; 723 * }; 724 * n2: node-2 { 725 * prop = "FOO"; 726 * } 727 * n3: node-3 { 728 * prop = "123 foo"; 729 * }; 730 * 731 * Example bindings fragment: 732 * 733 * properties: 734 * prop: 735 * type: string 736 * 737 * Example usage: 738 * 739 * DT_STRING_TOKEN(DT_NODELABEL(n1), prop) // foo 740 * DT_STRING_TOKEN(DT_NODELABEL(n2), prop) // FOO 741 * DT_STRING_TOKEN(DT_NODELABEL(n3), prop) // 123_foo 742 * 743 * Notice how: 744 * 745 * - Unlike C identifiers, the property values may begin with a 746 * number. It's the user's responsibility not to use such values as 747 * the name of a C identifier. 748 * 749 * - The uppercased "FOO" in the DTS remains @p FOO as a token. It is 750 * *not* converted to @p foo. 751 * 752 * - The whitespace in the DTS "123 foo" string is converted to @p 753 * 123_foo as a token. 754 * 755 * @param node_id node identifier 756 * @param prop lowercase-and-underscores property string name 757 * @return the value of @p prop as a token, i.e. without any quotes 758 * and with special characters converted to underscores 759 */ 760 #define DT_STRING_TOKEN(node_id, prop) \ 761 DT_CAT4(node_id, _P_, prop, _STRING_TOKEN) 762 763 /** 764 * @brief Like DT_STRING_TOKEN(), but uppercased. 765 * 766 * This removes "the quotes and capitalize" from string-valued properties, and 767 * converts non-alphanumeric characters to underscores. That can be useful, for 768 * example, when programmatically using the value to form a C variable or code. 769 * 770 * DT_STRING_UPPER_TOKEN() can only be used for properties with string type. 771 * 772 * It is an error to use DT_STRING_UPPER_TOKEN() in other circumstances. 773 * 774 * Example devicetree fragment: 775 * 776 * n1: node-1 { 777 * prop = "foo"; 778 * }; 779 * n2: node-2 { 780 * prop = "123 foo"; 781 * }; 782 * 783 * Example bindings fragment: 784 * 785 * properties: 786 * prop: 787 * type: string 788 * 789 * Example usage: 790 * 791 * DT_STRING_UPPER_TOKEN(DT_NODELABEL(n1), prop) // FOO 792 * DT_STRING_UPPER_TOKEN(DT_NODELABEL(n2), prop) // 123_FOO 793 * 794 * Notice how: 795 * 796 * - Unlike C identifiers, the property values may begin with a 797 * number. It's the user's responsibility not to use such values as 798 * the name of a C identifier. 799 * 800 * - The lowercased "foo" in the DTS becomes @p FOO as a token, i.e. 801 * it is uppercased. 802 * 803 * - The whitespace in the DTS "123 foo" string is converted to @p 804 * 123_FOO as a token, i.e. it is uppercased and whitespace becomes 805 * an underscore. 806 * 807 * @param node_id node identifier 808 * @param prop lowercase-and-underscores property string name 809 * @return the value of @p prop as a token, i.e. without any quotes 810 * and with special characters converted to underscores 811 */ 812 #define DT_STRING_UPPER_TOKEN(node_id, prop) \ 813 DT_CAT4(node_id, _P_, prop, _STRING_UPPER_TOKEN) 814 815 /** 816 * @brief Get an enumeration property's value as a token. 817 * 818 * This allows you to "remove the quotes" from some string-valued 819 * properties. That can be useful, for example, when pasting the 820 * values onto some other token to form an enum in C using the @p ## 821 * preprocessor operator. 822 * 823 * DT_ENUM_TOKEN() can only be used for properties with string type 824 * whose binding has an "enum:". The values in the binding's "enum:" 825 * list must be unique after converting non-alphanumeric characters to 826 * underscores. 827 * 828 * It is an error to use DT_ENUM_TOKEN() in other circumstances. 829 * 830 * Example devicetree fragment: 831 * 832 * n1: node-1 { 833 * prop = "foo"; 834 * }; 835 * n2: node-2 { 836 * prop = "FOO"; 837 * } 838 * n3: node-3 { 839 * prop = "123 foo"; 840 * }; 841 * 842 * Example bindings fragment: 843 * 844 * properties: 845 * prop: 846 * type: string 847 * enum: 848 * - "foo" 849 * - "FOO" 850 * - "123 foo" 851 * 852 * Example usage: 853 * 854 * DT_ENUM_TOKEN(DT_NODELABEL(n1), prop) // foo 855 * DT_ENUM_TOKEN(DT_NODELABEL(n2), prop) // FOO 856 * DT_ENUM_TOKEN(DT_NODELABEL(n3), prop) // 123_foo 857 * 858 * Notice how: 859 * 860 * - Unlike C identifiers, the property values may begin with a 861 * number. It's the user's responsibility not to use such values as 862 * the name of a C identifier. 863 * 864 * - The uppercased "FOO" in the DTS remains @p FOO as a token. It is 865 *not* converted to @p foo. 866 * 867 * - The whitespace in the DTS "123 foo" string is converted to @p 868 * 123_foo as a token. 869 * 870 * @param node_id node identifier 871 * @param prop lowercase-and-underscores property name with suitable 872 * enumeration of values in its binding 873 * @return the value of @p prop as a token, i.e. without any quotes 874 * and with special characters converted to underscores 875 */ 876 #define DT_ENUM_TOKEN(node_id, prop) \ 877 __DEPRECATED_MACRO \ 878 DT_CAT4(node_id, _P_, prop, _ENUM_TOKEN) 879 880 /** 881 * @brief Like DT_ENUM_TOKEN(), but uppercased 882 * 883 * This allows you to "remove the quotes and capitalize" some string-valued 884 * properties. 885 * 886 * DT_ENUM_UPPER_TOKEN() can only be used for properties with string type 887 * whose binding has an "enum:". The values in the binding's "enum:" 888 * list must be unique after converting non-alphanumeric characters to 889 * underscores and capitalizating any letters. 890 * 891 * It is an error to use DT_ENUM_UPPER_TOKEN() in other circumstances. 892 * 893 * Example devicetree fragment: 894 * 895 * n1: node-1 { 896 * prop = "foo"; 897 * }; 898 * n2: node-2 { 899 * prop = "123 foo"; 900 * }; 901 * 902 * Example bindings fragment: 903 * 904 * properties: 905 * prop: 906 * type: string 907 * enum: 908 * - "foo" 909 * - "123 foo" 910 * 911 * Example usage: 912 * 913 * DT_ENUM_TOKEN((DT_NODELABEL(n1), prop) // FOO 914 * DT_ENUM_TOKEN((DT_NODELABEL(n2), prop) // 123_FOO 915 * 916 * Notice how: 917 * 918 * - Unlike C identifiers, the property values may begin with a 919 * number. It's the user's responsibility not to use such values as 920 * the name of a C identifier. 921 * 922 * - The lowercased "foo" in the DTS becomes @p FOO as a token, i.e. 923 * it is uppercased. 924 * 925 * - The whitespace in the DTS "123 foo" string is converted to @p 926 * 123_FOO as a token, i.e. it is uppercased and whitespace becomes 927 * an underscore. 928 * 929 * @param node_id node identifier 930 * @param prop lowercase-and-underscores property name with suitable 931 * enumeration of values in its binding 932 * @return the value of @p prop as a capitalized token, i.e. upper case, 933 * without any quotes, and with special characters converted to 934 * underscores 935 */ 936 #define DT_ENUM_UPPER_TOKEN(node_id, prop) \ 937 __DEPRECATED_MACRO \ 938 DT_CAT4(node_id, _P_, prop, _ENUM_UPPER_TOKEN) 939 940 /* 941 * phandle properties 942 * 943 * These are special-cased to manage the impedance mismatch between 944 * phandles, which are just uint32_t node properties that only make sense 945 * within the tree itself, and C values. 946 */ 947 948 /** 949 * @brief Get a property value from a phandle in a property. 950 * 951 * This is a shorthand for: 952 * 953 * DT_PROP(DT_PHANDLE_BY_IDX(node_id, phs, idx), prop) 954 * 955 * That is, "prop" is a property of the phandle's node, not a 956 * property of "node_id". 957 * 958 * Example devicetree fragment: 959 * 960 * n1: node-1 { 961 * foo = <&n2 &n3>; 962 * }; 963 * 964 * n2: node-2 { 965 * bar = <42>; 966 * }; 967 * 968 * n3: node-3 { 969 * baz = <43>; 970 * }; 971 * 972 * Example usage: 973 * 974 * #define N1 DT_NODELABEL(n1) 975 * 976 * DT_PROP_BY_PHANDLE_IDX(N1, foo, 0, bar) // 42 977 * DT_PROP_BY_PHANDLE_IDX(N1, foo, 1, baz) // 43 978 * 979 * @param node_id node identifier 980 * @param phs lowercase-and-underscores property with type "phandle", 981 * "phandles", or "phandle-array" 982 * @param idx logical index into "phs", which must be zero if "phs" 983 * has type "phandle" 984 * @param prop lowercase-and-underscores property of the phandle's node 985 * @return the property's value 986 */ 987 #define DT_PROP_BY_PHANDLE_IDX(node_id, phs, idx, prop) \ 988 DT_PROP(DT_PHANDLE_BY_IDX(node_id, phs, idx), prop) 989 990 /** 991 * @brief Like DT_PROP_BY_PHANDLE_IDX(), but with a fallback to 992 * default_value. 993 * 994 * If the value exists, this expands to DT_PROP_BY_PHANDLE_IDX(node_id, phs, 995 * idx, prop). The default_value parameter is not expanded in this 996 * case. 997 * 998 * Otherwise, this expands to default_value. 999 * 1000 * @param node_id node identifier 1001 * @param phs lowercase-and-underscores property with type "phandle", 1002 * "phandles", or "phandle-array" 1003 * @param idx logical index into "phs", which must be zero if "phs" 1004 * has type "phandle" 1005 * @param prop lowercase-and-underscores property of the phandle's node 1006 * @param default_value a fallback value to expand to 1007 * @return the property's value 1008 */ 1009 #define DT_PROP_BY_PHANDLE_IDX_OR(node_id, phs, idx, prop, default_value) \ 1010 DT_PROP_OR(DT_PHANDLE_BY_IDX(node_id, phs, idx), prop, default_value) 1011 1012 /** 1013 * @brief Get a property value from a phandle's node 1014 * 1015 * This is equivalent to DT_PROP_BY_PHANDLE_IDX(node_id, ph, 0, prop). 1016 * 1017 * @param node_id node identifier 1018 * @param ph lowercase-and-underscores property of "node_id" 1019 * with type "phandle" 1020 * @param prop lowercase-and-underscores property of the phandle's node 1021 * @return the property's value 1022 */ 1023 #define DT_PROP_BY_PHANDLE(node_id, ph, prop) \ 1024 DT_PROP_BY_PHANDLE_IDX(node_id, ph, 0, prop) 1025 1026 /** 1027 * @brief Get a phandle-array specifier cell value at an index 1028 * 1029 * It might help to read the argument order as being similar to 1030 * "node->phandle_array[index].cell". That is, the cell value is in 1031 * the "pha" property of "node_id", inside the specifier at index 1032 * "idx". 1033 * 1034 * Example devicetree fragment: 1035 * 1036 * gpio0: gpio@... { 1037 * #gpio-cells = <2>; 1038 * }; 1039 * 1040 * gpio1: gpio@... { 1041 * #gpio-cells = <2>; 1042 * }; 1043 * 1044 * led: led_0 { 1045 * gpios = <&gpio0 17 0x1>, <&gpio1 5 0x3>; 1046 * }; 1047 * 1048 * Bindings fragment for the gpio0 and gpio1 nodes: 1049 * 1050 * gpio-cells: 1051 * - pin 1052 * - flags 1053 * 1054 * Above, "gpios" has two elements: 1055 * 1056 * - index 0 has specifier <17 0x1>, so its "pin" cell is 17, and its 1057 * "flags" cell is 0x1 1058 * - index 1 has specifier <5 0x3>, so "pin" is 5 and "flags" is 0x3 1059 * 1060 * Example usage: 1061 * 1062 * #define LED DT_NODELABEL(led) 1063 * 1064 * DT_PHA_BY_IDX(LED, gpios, 0, pin) // 17 1065 * DT_PHA_BY_IDX(LED, gpios, 1, flags) // 0x3 1066 * 1067 * @param node_id node identifier 1068 * @param pha lowercase-and-underscores property with type "phandle-array" 1069 * @param idx logical index into "pha" 1070 * @param cell lowercase-and-underscores cell name within the specifier 1071 * at "pha" index "idx" 1072 * @return the cell's value 1073 */ 1074 #define DT_PHA_BY_IDX(node_id, pha, idx, cell) \ 1075 DT_PROP(node_id, pha##_IDX_##idx##_VAL_##cell) 1076 1077 /** 1078 * @brief Like DT_PHA_BY_IDX(), but with a fallback to default_value. 1079 * 1080 * If the value exists, this expands to DT_PHA_BY_IDX(node_id, pha, 1081 * idx, cell). The default_value parameter is not expanded in this 1082 * case. 1083 * 1084 * Otherwise, this expands to default_value. 1085 * 1086 * @param node_id node identifier 1087 * @param pha lowercase-and-underscores property with type "phandle-array" 1088 * @param idx logical index into "pha" 1089 * @param cell lowercase-and-underscores cell name within the specifier 1090 * at "pha" index "idx" 1091 * @param default_value a fallback value to expand to 1092 * @return the cell's value or "default_value" 1093 */ 1094 #define DT_PHA_BY_IDX_OR(node_id, pha, idx, cell, default_value) \ 1095 DT_PROP_OR(node_id, pha##_IDX_##idx##_VAL_##cell, default_value) 1096 /* Implementation note: the _IDX_##idx##_VAL_##cell##_EXISTS 1097 * macros are defined, so it's safe to use DT_PROP_OR() here, because 1098 * that uses an IS_ENABLED() on the _EXISTS macro. 1099 */ 1100 1101 /** 1102 * @brief Equivalent to DT_PHA_BY_IDX(node_id, pha, 0, cell) 1103 * @param node_id node identifier 1104 * @param pha lowercase-and-underscores property with type "phandle-array" 1105 * @param cell lowercase-and-underscores cell name 1106 * @return the cell's value 1107 */ 1108 #define DT_PHA(node_id, pha, cell) DT_PHA_BY_IDX(node_id, pha, 0, cell) 1109 1110 /** 1111 * @brief Like DT_PHA(), but with a fallback to default_value 1112 * 1113 * If the value exists, this expands to DT_PHA(node_id, pha, cell). 1114 * The default_value parameter is not expanded in this case. 1115 * 1116 * Otherwise, this expands to default_value. 1117 * 1118 * @param node_id node identifier 1119 * @param pha lowercase-and-underscores property with type "phandle-array" 1120 * @param cell lowercase-and-underscores cell name 1121 * @param default_value a fallback value to expand to 1122 * @return the cell's value or default_value 1123 */ 1124 #define DT_PHA_OR(node_id, pha, cell, default_value) \ 1125 DT_PHA_BY_IDX_OR(node_id, pha, 0, cell, default_value) 1126 1127 /** 1128 * @brief Get a value within a phandle-array specifier by name 1129 * 1130 * This is like DT_PHA_BY_IDX(), except it treats "pha" as a structure 1131 * where each array element has a name. 1132 * 1133 * It might help to read the argument order as being similar to 1134 * "node->phandle_struct.name.cell". That is, the cell value is in the 1135 * "pha" property of "node_id", treated as a data structure where 1136 * each array element has a name. 1137 * 1138 * Example devicetree fragment: 1139 * 1140 * n: node { 1141 * io-channels = <&adc1 10>, <&adc2 20>; 1142 * io-channel-names = "SENSOR", "BANDGAP"; 1143 * }; 1144 * 1145 * Bindings fragment for the "adc1" and "adc2" nodes: 1146 * 1147 * io-channel-cells: 1148 * - input 1149 * 1150 * Example usage: 1151 * 1152 * DT_PHA_BY_NAME(DT_NODELABEL(n), io_channels, sensor, input) // 10 1153 * DT_PHA_BY_NAME(DT_NODELABEL(n), io_channels, bandgap, input) // 20 1154 * 1155 * @param node_id node identifier 1156 * @param pha lowercase-and-underscores property with type "phandle-array" 1157 * @param name lowercase-and-underscores name of a specifier in "pha" 1158 * @param cell lowercase-and-underscores cell name in the named specifier 1159 * @return the cell's value 1160 */ 1161 #define DT_PHA_BY_NAME(node_id, pha, name, cell) \ 1162 DT_PROP(node_id, pha##_NAME_##name##_VAL_##cell) 1163 1164 /** 1165 * @brief Like DT_PHA_BY_NAME(), but with a fallback to default_value 1166 * 1167 * If the value exists, this expands to DT_PHA_BY_NAME(node_id, pha, 1168 * name, cell). The default_value parameter is not expanded in this case. 1169 * 1170 * Otherwise, this expands to default_value. 1171 * 1172 * @param node_id node identifier 1173 * @param pha lowercase-and-underscores property with type "phandle-array" 1174 * @param name lowercase-and-underscores name of a specifier in "pha" 1175 * @param cell lowercase-and-underscores cell name in the named specifier 1176 * @param default_value a fallback value to expand to 1177 * @return the cell's value or default_value 1178 */ 1179 #define DT_PHA_BY_NAME_OR(node_id, pha, name, cell, default_value) \ 1180 DT_PROP_OR(node_id, pha##_NAME_##name##_VAL_##cell, default_value) 1181 /* Implementation note: the _NAME_##name##_VAL_##cell##_EXISTS 1182 * macros are defined, so it's safe to use DT_PROP_OR() here, because 1183 * that uses an IS_ENABLED() on the _EXISTS macro. 1184 */ 1185 1186 /** 1187 * @brief Get a phandle's node identifier from a phandle array by name 1188 * 1189 * It might help to read the argument order as being similar to 1190 * "node->phandle_struct.name.phandle". That is, the phandle array is 1191 * treated as a structure with named elements. The return value is 1192 * the node identifier for a phandle inside the structure. 1193 * 1194 * Example devicetree fragment: 1195 * 1196 * adc1: adc@... { 1197 * label = "ADC_1"; 1198 * }; 1199 * 1200 * adc2: adc@... { 1201 * label = "ADC_2"; 1202 * }; 1203 * 1204 * n: node { 1205 * io-channels = <&adc1 10>, <&adc2 20>; 1206 * io-channel-names = "SENSOR", "BANDGAP"; 1207 * }; 1208 * 1209 * Above, "io-channels" has two elements: 1210 * 1211 * - the element named "SENSOR" has phandle &adc1 1212 * - the element named "BANDGAP" has phandle &adc2 1213 * 1214 * Example usage: 1215 * 1216 * #define NODE DT_NODELABEL(n) 1217 * 1218 * DT_LABEL(DT_PHANDLE_BY_NAME(NODE, io_channels, sensor)) // "ADC_1" 1219 * DT_LABEL(DT_PHANDLE_BY_NAME(NODE, io_channels, bandgap)) // "ADC_2" 1220 * 1221 * Notice how devicetree properties and names are lowercased, and 1222 * non-alphanumeric characters are converted to underscores. 1223 * 1224 * @param node_id node identifier 1225 * @param pha lowercase-and-underscores property with type "phandle-array" 1226 * @param name lowercase-and-underscores name of an element in "pha" 1227 * @return a node identifier for the node with that phandle 1228 */ 1229 #define DT_PHANDLE_BY_NAME(node_id, pha, name) \ 1230 DT_PROP(node_id, pha##_NAME_##name##_PH) 1231 1232 /** 1233 * @brief Get a node identifier for a phandle in a property. 1234 * 1235 * When a node's value at a logical index contains a phandle, this 1236 * macro returns a node identifier for the node with that phandle. 1237 * 1238 * Therefore, if "prop" has type "phandle", "idx" must be zero. (A 1239 * "phandle" type is treated as a "phandles" with a fixed length of 1240 * 1). 1241 * 1242 * Example devicetree fragment: 1243 * 1244 * n1: node-1 { 1245 * foo = <&n2 &n3>; 1246 * }; 1247 * 1248 * n2: node-2 { ... }; 1249 * n3: node-3 { ... }; 1250 * 1251 * Above, "foo" has type phandles and has two elements: 1252 * 1253 * - index 0 has phandle &n2, which is node-2's phandle 1254 * - index 1 has phandle &n3, which is node-3's phandle 1255 * 1256 * Example usage: 1257 * 1258 * #define N1 DT_NODELABEL(n1) 1259 * 1260 * DT_PHANDLE_BY_IDX(N1, foo, 0) // node identifier for node-2 1261 * DT_PHANDLE_BY_IDX(N1, foo, 1) // node identifier for node-3 1262 * 1263 * Behavior is analogous for phandle-arrays. 1264 * 1265 * @param node_id node identifier 1266 * @param prop lowercase-and-underscores property name in "node_id" 1267 * with type "phandle", "phandles" or "phandle-array" 1268 * @param idx index into "prop" 1269 * @return node identifier for the node with the phandle at that index 1270 */ 1271 #define DT_PHANDLE_BY_IDX(node_id, prop, idx) \ 1272 DT_CAT6(node_id, _P_, prop, _IDX_, idx, _PH) 1273 /* 1274 * Implementation note: using DT_CAT6 above defers concatenation until 1275 * after expansion of each parameter. This is important when 'idx' is 1276 * expandable to a number, but it isn't one "yet". 1277 */ 1278 1279 /** 1280 * @brief Get a node identifier for a phandle property's value 1281 * 1282 * This is equivalent to DT_PHANDLE_BY_IDX(node_id, prop, 0). Its primary 1283 * benefit is readability when "prop" has type "phandle". 1284 * 1285 * @param node_id node identifier 1286 * @param prop lowercase-and-underscores property of "node_id" 1287 * with type "phandle" 1288 * @return a node identifier for the node pointed to by "ph" 1289 */ 1290 #define DT_PHANDLE(node_id, prop) DT_PHANDLE_BY_IDX(node_id, prop, 0) 1291 1292 /** 1293 * @} 1294 */ 1295 1296 /** 1297 * @defgroup devicetree-reg-prop reg property 1298 * @ingroup devicetree 1299 * @{ 1300 */ 1301 1302 /** 1303 * @brief Get the number of register blocks in the reg property 1304 * 1305 * Use this instead of DT_PROP_LEN(node_id, reg). 1306 * @param node_id node identifier 1307 * @return Number of register blocks in the node's "reg" property. 1308 */ 1309 #define DT_NUM_REGS(node_id) DT_CAT(node_id, _REG_NUM) 1310 1311 /** 1312 * @brief Is "idx" a valid register block index? 1313 * 1314 * If this returns 1, then DT_REG_ADDR_BY_IDX(node_id, idx) or 1315 * DT_REG_SIZE_BY_IDX(node_id, idx) are valid. 1316 * If it returns 0, it is an error to use those macros with index "idx". 1317 * @param node_id node identifier 1318 * @param idx index to check 1319 * @return 1 if "idx" is a valid register block index, 1320 * 0 otherwise. 1321 */ 1322 #define DT_REG_HAS_IDX(node_id, idx) \ 1323 IS_ENABLED(DT_CAT(node_id, _REG_IDX_##idx##_EXISTS)) 1324 1325 /** 1326 * @brief Get the base address of the register block at index "idx" 1327 * @param node_id node identifier 1328 * @param idx index of the register whose address to return 1329 * @return address of the idx-th register block 1330 */ 1331 #define DT_REG_ADDR_BY_IDX(node_id, idx) \ 1332 DT_CAT(node_id, _REG_IDX_##idx##_VAL_ADDRESS) 1333 1334 /** 1335 * @brief Get the size of the register block at index "idx" 1336 * 1337 * This is the size of an individual register block, not the total 1338 * number of register blocks in the property; use DT_NUM_REGS() for 1339 * that. 1340 * 1341 * @param node_id node identifier 1342 * @param idx index of the register whose size to return 1343 * @return size of the idx-th register block 1344 */ 1345 #define DT_REG_SIZE_BY_IDX(node_id, idx) \ 1346 DT_CAT(node_id, _REG_IDX_##idx##_VAL_SIZE) 1347 1348 /** 1349 * @brief Get a node's (only) register block address 1350 * 1351 * Equivalent to DT_REG_ADDR_BY_IDX(node_id, 0). 1352 * @param node_id node identifier 1353 * @return node's register block address 1354 */ 1355 #define DT_REG_ADDR(node_id) DT_REG_ADDR_BY_IDX(node_id, 0) 1356 1357 /** 1358 * @brief Get a node's (only) register block size 1359 * 1360 * Equivalent to DT_REG_SIZE_BY_IDX(node_id, 0). 1361 * @param node_id node identifier 1362 * @return node's only register block's size 1363 */ 1364 #define DT_REG_SIZE(node_id) DT_REG_SIZE_BY_IDX(node_id, 0) 1365 1366 /** 1367 * @brief Get a register block's base address by name 1368 * @param node_id node identifier 1369 * @param name lowercase-and-underscores register specifier name 1370 * @return address of the register block specified by name 1371 */ 1372 #define DT_REG_ADDR_BY_NAME(node_id, name) \ 1373 DT_CAT(node_id, _REG_NAME_##name##_VAL_ADDRESS) 1374 1375 /** 1376 * @brief Get a register block's size by name 1377 * @param node_id node identifier 1378 * @param name lowercase-and-underscores register specifier name 1379 * @return size of the register block specified by name 1380 */ 1381 #define DT_REG_SIZE_BY_NAME(node_id, name) \ 1382 DT_CAT(node_id, _REG_NAME_##name##_VAL_SIZE) 1383 1384 /** 1385 * @} 1386 */ 1387 1388 /** 1389 * @defgroup devicetree-interrupts-prop interrupts property 1390 * @ingroup devicetree 1391 * @{ 1392 */ 1393 1394 /** 1395 * @brief Get the number of interrupt sources for the node 1396 * 1397 * Use this instead of DT_PROP_LEN(node_id, interrupts). 1398 * 1399 * @param node_id node identifier 1400 * @return Number of interrupt specifiers in the node's "interrupts" property. 1401 */ 1402 #define DT_NUM_IRQS(node_id) DT_CAT(node_id, _IRQ_NUM) 1403 1404 /** 1405 * @brief Is "idx" a valid interrupt index? 1406 * 1407 * If this returns 1, then DT_IRQ_BY_IDX(node_id, idx) is valid. 1408 * If it returns 0, it is an error to use that macro with this index. 1409 * @param node_id node identifier 1410 * @param idx index to check 1411 * @return 1 if the idx is valid for the interrupt property 1412 * 0 otherwise. 1413 */ 1414 #define DT_IRQ_HAS_IDX(node_id, idx) \ 1415 IS_ENABLED(DT_CAT(node_id, _IRQ_IDX_##idx##_EXISTS)) 1416 1417 /** 1418 * @brief Does an interrupts property have a named cell specifier at an index? 1419 * If this returns 1, then DT_IRQ_BY_IDX(node_id, idx, cell) is valid. 1420 * If it returns 0, it is an error to use that macro. 1421 * @param node_id node identifier 1422 * @param idx index to check 1423 * @param cell named cell value whose existence to check 1424 * @return 1 if the named cell exists in the interrupt specifier at index idx 1425 * 0 otherwise. 1426 */ 1427 #define DT_IRQ_HAS_CELL_AT_IDX(node_id, idx, cell) \ 1428 IS_ENABLED(DT_CAT(node_id, _IRQ_IDX_##idx##_VAL_##cell##_EXISTS)) 1429 1430 /** 1431 * @brief Equivalent to DT_IRQ_HAS_CELL_AT_IDX(node_id, 0, cell) 1432 * @param node_id node identifier 1433 * @param cell named cell value whose existence to check 1434 * @return 1 if the named cell exists in the interrupt specifier at index 0 1435 * 0 otherwise. 1436 */ 1437 #define DT_IRQ_HAS_CELL(node_id, cell) DT_IRQ_HAS_CELL_AT_IDX(node_id, 0, cell) 1438 1439 /** 1440 * @brief Does an interrupts property have a named specifier value at an index? 1441 * If this returns 1, then DT_IRQ_BY_NAME(node_id, name, cell) is valid. 1442 * If it returns 0, it is an error to use that macro. 1443 * @param node_id node identifier 1444 * @param name lowercase-and-underscores interrupt specifier name 1445 * @return 1 if "name" is a valid named specifier 1446 * 0 otherwise. 1447 */ 1448 #define DT_IRQ_HAS_NAME(node_id, name) \ 1449 IS_ENABLED(DT_CAT(node_id, _IRQ_NAME_##name##_VAL_irq_EXISTS)) 1450 1451 /** 1452 * @brief Get a value within an interrupt specifier at an index 1453 * 1454 * It might help to read the argument order as being similar to 1455 * "node->interrupts[index].cell". 1456 * 1457 * This can be used to get information about an individual interrupt 1458 * when a device generates more than one. 1459 * 1460 * Example devicetree fragment: 1461 * 1462 * my-serial: serial@... { 1463 * interrupts = < 33 0 >, < 34 1 >; 1464 * }; 1465 * 1466 * Assuming the node's interrupt domain has "#interrupt-cells = <2>;" and 1467 * the individual cells in each interrupt specifier are named "irq" and 1468 * "priority" by the node's binding, here are some examples: 1469 * 1470 * #define SERIAL DT_NODELABEL(my_serial) 1471 * 1472 * Example usage Value 1473 * ------------- ----- 1474 * DT_IRQ_BY_IDX(SERIAL, 0, irq) 33 1475 * DT_IRQ_BY_IDX(SERIAL, 0, priority) 0 1476 * DT_IRQ_BY_IDX(SERIAL, 1, irq, 34 1477 * DT_IRQ_BY_IDX(SERIAL, 1, priority) 1 1478 * 1479 * @param node_id node identifier 1480 * @param idx logical index into the interrupt specifier array 1481 * @param cell cell name specifier 1482 * @return the named value at the specifier given by the index 1483 */ 1484 #define DT_IRQ_BY_IDX(node_id, idx, cell) \ 1485 DT_CAT(node_id, _IRQ_IDX_##idx##_VAL_##cell) 1486 1487 /** 1488 * @brief Get a value within an interrupt specifier by name 1489 * 1490 * It might help to read the argument order as being similar to 1491 * "node->interrupts.name.cell". 1492 * 1493 * This can be used to get information about an individual interrupt 1494 * when a device generates more than one, if the bindings give each 1495 * interrupt specifier a name. 1496 * 1497 * @param node_id node identifier 1498 * @param name lowercase-and-underscores interrupt specifier name 1499 * @param cell cell name specifier 1500 * @return the named value at the specifier given by the index 1501 */ 1502 #define DT_IRQ_BY_NAME(node_id, name, cell) \ 1503 DT_CAT(node_id, _IRQ_NAME_##name##_VAL_##cell) 1504 1505 /** 1506 * @brief Get an interrupt specifier's value 1507 * Equivalent to DT_IRQ_BY_IDX(node_id, 0, cell). 1508 * @param node_id node identifier 1509 * @param cell cell name specifier 1510 * @return the named value at that index 1511 */ 1512 #define DT_IRQ(node_id, cell) DT_IRQ_BY_IDX(node_id, 0, cell) 1513 1514 /** 1515 * @brief Get a node's (only) irq number 1516 * 1517 * Equivalent to DT_IRQ(node_id, irq). This is provided as a convenience 1518 * for the common case where a node generates exactly one interrupt, 1519 * and the IRQ number is in a cell named "irq". 1520 * 1521 * @param node_id node identifier 1522 * @return the interrupt number for the node's only interrupt 1523 */ 1524 #define DT_IRQN(node_id) DT_IRQ(node_id, irq) 1525 1526 /** 1527 * @} 1528 */ 1529 1530 /** 1531 * @defgroup devicetree-generic-chosen Chosen nodes 1532 * @ingroup devicetree 1533 * @{ 1534 */ 1535 1536 /** 1537 * @brief Get a node identifier for a /chosen node property 1538 * 1539 * This is only valid to call if DT_HAS_CHOSEN(prop) is 1. 1540 * @param prop lowercase-and-underscores property name for 1541 * the /chosen node 1542 * @return a node identifier for the chosen node property 1543 */ 1544 #define DT_CHOSEN(prop) DT_CAT(DT_CHOSEN_, prop) 1545 1546 /** 1547 * @brief Test if the devicetree has a /chosen node 1548 * @param prop lowercase-and-underscores devicetree property 1549 * @return 1 if the chosen property exists and refers to a node, 1550 * 0 otherwise 1551 */ 1552 #define DT_HAS_CHOSEN(prop) IS_ENABLED(DT_CHOSEN_##prop##_EXISTS) 1553 1554 /** 1555 * @} 1556 */ 1557 1558 /** 1559 * @defgroup devicetree-generic-foreach "For-each" macros 1560 * @ingroup devicetree 1561 * @{ 1562 */ 1563 1564 /** 1565 * @brief Invokes "fn" for each child of "node_id" 1566 * 1567 * The macro "fn" must take one parameter, which will be the node 1568 * identifier of a child node of "node_id". 1569 * 1570 * Example devicetree fragment: 1571 * 1572 * n: node { 1573 * child-1 { 1574 * label = "foo"; 1575 * }; 1576 * child-2 { 1577 * label = "bar"; 1578 * }; 1579 * }; 1580 * 1581 * Example usage: 1582 * 1583 * #define LABEL_AND_COMMA(node_id) DT_LABEL(node_id), 1584 * 1585 * const char *child_labels[] = { 1586 * DT_FOREACH_CHILD(DT_NODELABEL(n), LABEL_AND_COMMA) 1587 * }; 1588 * 1589 * This expands to: 1590 * 1591 * const char *child_labels[] = { 1592 * "foo", "bar", 1593 * }; 1594 * 1595 * @param node_id node identifier 1596 * @param fn macro to invoke 1597 */ 1598 #define DT_FOREACH_CHILD(node_id, fn) \ 1599 DT_CAT(node_id, _FOREACH_CHILD)(fn) 1600 1601 /** 1602 * @brief Invokes "fn" for each child of "node_id" with multiple arguments 1603 * 1604 * The macro "fn" takes multiple arguments. The first should be the node 1605 * identifier for the child node. The remaining are passed-in by the caller. 1606 * 1607 * @param node_id node identifier 1608 * @param fn macro to invoke 1609 * @param ... variable number of arguments to pass to fn 1610 * 1611 * @see DT_FOREACH_CHILD 1612 */ 1613 #define DT_FOREACH_CHILD_VARGS(node_id, fn, ...) \ 1614 DT_CAT(node_id, _FOREACH_CHILD_VARGS)(fn, __VA_ARGS__) 1615 1616 /** 1617 * @brief Call "fn" on the child nodes with status "okay" 1618 * 1619 * The macro "fn" should take one argument, which is the node 1620 * identifier for the child node. 1621 * 1622 * As usual, both a missing status and an "ok" status are 1623 * treated as "okay". 1624 * 1625 * @param node_id node identifier 1626 * @param fn macro to invoke 1627 */ 1628 #define DT_FOREACH_CHILD_STATUS_OKAY(node_id, fn) \ 1629 DT_CAT(node_id, _FOREACH_CHILD_STATUS_OKAY)(fn) 1630 1631 /** 1632 * @brief Call "fn" on the child nodes with status "okay" with multiple 1633 * arguments 1634 * 1635 * The macro "fn" takes multiple arguments. The first should be the node 1636 * identifier for the child node. The remaining are passed-in by the caller. 1637 * 1638 * As usual, both a missing status and an "ok" status are 1639 * treated as "okay". 1640 * 1641 * @param node_id node identifier 1642 * @param fn macro to invoke 1643 * @param ... variable number of arguments to pass to fn 1644 * 1645 * @see DT_FOREACH_CHILD_STATUS_OKAY 1646 */ 1647 #define DT_FOREACH_CHILD_STATUS_OKAY_VARGS(node_id, fn, ...) \ 1648 DT_CAT(node_id, _FOREACH_CHILD_STATUS_OKAY_VARGS)(fn, __VA_ARGS__) 1649 1650 /** 1651 * @brief Invokes "fn" for each element in the value of property "prop". 1652 * 1653 * The macro "fn" must take three parameters: fn(node_id, prop, idx). 1654 * "node_id" and "prop" are the same as what is passed to 1655 * DT_FOREACH_PROP_ELEM, and "idx" is the current index into the array. 1656 * The "idx" values are integer literals starting from 0. 1657 * 1658 * Example devicetree fragment: 1659 * 1660 * n: node { 1661 * my-ints = <1 2 3>; 1662 * }; 1663 * 1664 * Example usage: 1665 * 1666 * #define TIMES_TWO(node_id, prop, idx) \ 1667 * (2 * DT_PROP_BY_IDX(node_id, prop, idx)), 1668 * 1669 * int array[] = { 1670 * DT_FOREACH_PROP_ELEM(DT_NODELABEL(n), my_ints, TIMES_TWO) 1671 * }; 1672 * 1673 * This expands to: 1674 * 1675 * int array[] = { 1676 * (2 * 1), (2 * 2), (2 * 3), 1677 * }; 1678 * 1679 * In general, this macro expands to: 1680 * 1681 * fn(node_id, prop, 0) fn(node_id, prop, 1) [...] fn(node_id, prop, n-1) 1682 * 1683 * where "n" is the number of elements in "prop", as it would be 1684 * returned by <tt>DT_PROP_LEN(node_id, prop)</tt>. 1685 * 1686 * The "prop" argument must refer to a property with type string, 1687 * array, uint8-array, string-array, phandles, or phandle-array. It is 1688 * an error to use this macro with properties of other types. 1689 * 1690 * @param node_id node identifier 1691 * @param prop lowercase-and-underscores property name 1692 * @param fn macro to invoke 1693 */ 1694 #define DT_FOREACH_PROP_ELEM(node_id, prop, fn) \ 1695 DT_CAT4(node_id, _P_, prop, _FOREACH_PROP_ELEM)(fn) 1696 1697 /** 1698 * @brief Invokes "fn" for each element in the value of property "prop" with 1699 * multiple arguments. 1700 * 1701 * The macro "fn" must take multiple parameters: fn(node_id, prop, idx, ...). 1702 * "node_id" and "prop" are the same as what is passed to 1703 * DT_FOREACH_PROP_ELEM, and "idx" is the current index into the array. 1704 * The "idx" values are integer literals starting from 0. The remaining 1705 * arguments are passed-in by the caller. 1706 * 1707 * @param node_id node identifier 1708 * @param prop lowercase-and-underscores property name 1709 * @param fn macro to invoke 1710 * @param ... variable number of arguments to pass to fn 1711 * 1712 * @see DT_FOREACH_PROP_ELEM 1713 */ 1714 #define DT_FOREACH_PROP_ELEM_VARGS(node_id, prop, fn, ...) \ 1715 DT_CAT4(node_id, _P_, prop, _FOREACH_PROP_ELEM_VARGS)(fn, __VA_ARGS__) 1716 1717 /** 1718 * @brief Call "fn" on all nodes with compatible DT_DRV_COMPAT 1719 * and status "okay" 1720 * 1721 * This macro expands to: 1722 * 1723 * fn(node_id_1) fn(node_id_2) ... fn(node_id_n) 1724 * 1725 * where each "node_id_<i>" is a node identifier for some node with 1726 * compatible "compat" and status "okay". Whitespace is added between 1727 * expansions as shown above. 1728 * 1729 * Example devicetree fragment: 1730 * 1731 * / { 1732 * a { 1733 * compatible = "foo"; 1734 * status = "okay"; 1735 * }; 1736 * b { 1737 * compatible = "foo"; 1738 * status = "disabled"; 1739 * }; 1740 * c { 1741 * compatible = "foo"; 1742 * }; 1743 * }; 1744 * 1745 * Example usage: 1746 * 1747 * DT_FOREACH_STATUS_OKAY(foo, DT_NODE_PATH) 1748 * 1749 * This expands to one of the following: 1750 * 1751 * "/a" "/c" 1752 * "/c" "/a" 1753 * 1754 * "One of the following" is because no guarantees are made about the 1755 * order that node identifiers are passed to "fn" in the expansion. 1756 * 1757 * (The "/c" string literal is present because a missing status 1758 * property is always treated as if the status were set to "okay".) 1759 * 1760 * Note also that "fn" is responsible for adding commas, semicolons, 1761 * or other terminators as needed. 1762 * 1763 * @param compat lowercase-and-underscores devicetree compatible 1764 * @param fn Macro to call for each enabled node. Must accept a 1765 * node_id as its only parameter. 1766 */ 1767 #define DT_FOREACH_STATUS_OKAY(compat, fn) \ 1768 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \ 1769 (UTIL_CAT(DT_FOREACH_OKAY_, compat)(fn)), \ 1770 ()) 1771 1772 /** 1773 * @brief Invokes "fn" for each status "okay" node of a compatible 1774 * with multiple arguments. 1775 * 1776 * This is like DT_FOREACH_STATUS_OKAY() except you can also pass 1777 * additional arguments to "fn". 1778 * 1779 * Example devicetree fragment: 1780 * 1781 * / { 1782 * a { 1783 * compatible = "foo"; 1784 * val = <3>; 1785 * }; 1786 * b { 1787 * compatible = "foo"; 1788 * val = <4>; 1789 * }; 1790 * }; 1791 * 1792 * Example usage: 1793 * 1794 * #define MY_FN(node_id, operator) DT_PROP(node_id, val) operator 1795 * x = DT_FOREACH_STATUS_OKAY_VARGS(foo, MY_FN, +) 0; 1796 * 1797 * This expands to one of the following: 1798 * 1799 * x = 3 + 4 + 0; 1800 * x = 4 + 3 + 0; 1801 * 1802 * i.e. it sets x to 7. As with DT_FOREACH_STATUS_OKAY(), there are no 1803 * guarantees about the order nodes appear in the expansion. 1804 * 1805 * @param compat lowercase-and-underscores devicetree compatible 1806 * @param fn Macro to call for each enabled node. Must accept a 1807 * node_id as its only parameter. 1808 * @param ... Additional arguments to pass to "fn" 1809 */ 1810 #define DT_FOREACH_STATUS_OKAY_VARGS(compat, fn, ...) \ 1811 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \ 1812 (UTIL_CAT(DT_FOREACH_OKAY_VARGS_, \ 1813 compat)(fn, __VA_ARGS__)), \ 1814 ()) 1815 1816 /** 1817 * @} 1818 */ 1819 1820 /** 1821 * @defgroup devicetree-generic-exist Existence checks 1822 * @ingroup devicetree 1823 * @{ 1824 */ 1825 1826 /** 1827 * @brief Does a node identifier refer to a node? 1828 * 1829 * Tests whether a node identifier refers to a node which exists, i.e. 1830 * is defined in the devicetree. 1831 * 1832 * It doesn't matter whether or not the node has a matching binding, 1833 * or what the node's status value is. This is purely a check of 1834 * whether the node exists at all. 1835 * 1836 * @param node_id a node identifier 1837 * @return 1 if the node identifier refers to a node, 1838 * 0 otherwise. 1839 */ 1840 #define DT_NODE_EXISTS(node_id) IS_ENABLED(DT_CAT(node_id, _EXISTS)) 1841 1842 /** 1843 * @brief Does a node identifier refer to a node with a status? 1844 * 1845 * Example uses: 1846 * 1847 * DT_NODE_HAS_STATUS(DT_PATH(soc, i2c_12340000), okay) 1848 * DT_NODE_HAS_STATUS(DT_PATH(soc, i2c_12340000), disabled) 1849 * 1850 * Tests whether a node identifier refers to a node which: 1851 * 1852 * - exists in the devicetree, and 1853 * - has a status property matching the second argument 1854 * (except that either a missing status or an "ok" status 1855 * in the devicetree is treated as if it were "okay" instead) 1856 * 1857 * @param node_id a node identifier 1858 * @param status a status as one of the tokens okay or disabled, not a string 1859 * @return 1 if the node has the given status, 0 otherwise. 1860 */ 1861 #define DT_NODE_HAS_STATUS(node_id, status) \ 1862 DT_NODE_HAS_STATUS_INTERNAL(node_id, status) 1863 1864 /** 1865 * @brief Does the devicetree have a status "okay" node with a compatible? 1866 * 1867 * Test for whether the devicetree has any nodes with status "okay" 1868 * and the given compatible. That is, this returns 1 if and only if 1869 * there is at least one "node_id" for which both of these 1870 * expressions return 1: 1871 * 1872 * DT_NODE_HAS_STATUS(node_id, okay) 1873 * DT_NODE_HAS_COMPAT(node_id, compat) 1874 * 1875 * As usual, both a missing status and an "ok" status are treated as 1876 * "okay". 1877 * 1878 * @param compat lowercase-and-underscores compatible, without quotes 1879 * @return 1 if both of the above conditions are met, 0 otherwise 1880 */ 1881 #define DT_HAS_COMPAT_STATUS_OKAY(compat) \ 1882 IS_ENABLED(DT_CAT(DT_COMPAT_HAS_OKAY_, compat)) 1883 1884 /** 1885 * @brief Get the number of instances of a given compatible with 1886 * status "okay" 1887 * @param compat lowercase-and-underscores compatible, without quotes 1888 * @return Number of instances with status "okay" 1889 */ 1890 #define DT_NUM_INST_STATUS_OKAY(compat) \ 1891 UTIL_AND(DT_HAS_COMPAT_STATUS_OKAY(compat), \ 1892 UTIL_CAT(DT_N_INST, DT_DASH(compat, NUM_OKAY))) 1893 1894 /** 1895 * @brief Does a devicetree node match a compatible? 1896 * 1897 * Example devicetree fragment: 1898 * 1899 * n: node { 1900 * compatible = "vnd,specific-device", "generic-device"; 1901 * } 1902 * 1903 * Example usages which evaluate to 1: 1904 * 1905 * DT_NODE_HAS_COMPAT(DT_NODELABEL(n), vnd_specific_device) 1906 * DT_NODE_HAS_COMPAT(DT_NODELABEL(n), generic_device) 1907 * 1908 * This macro only uses the value of the compatible property. Whether 1909 * or not a particular compatible has a matching binding has no effect 1910 * on its value, nor does the node's status. 1911 * 1912 * @param node_id node identifier 1913 * @param compat lowercase-and-underscores compatible, without quotes 1914 * @return 1 if the node's compatible property contains compat, 1915 * 0 otherwise. 1916 */ 1917 #define DT_NODE_HAS_COMPAT(node_id, compat) \ 1918 IS_ENABLED(DT_CAT(node_id, _COMPAT_MATCHES_##compat)) 1919 1920 /** 1921 * @brief Does a devicetree node have a compatible and status? 1922 * 1923 * This is equivalent to: 1924 * 1925 * (DT_NODE_HAS_COMPAT(node_id, compat) && 1926 * DT_NODE_HAS_STATUS(node_id, status)) 1927 * 1928 * @param node_id node identifier 1929 * @param compat lowercase-and-underscores compatible, without quotes 1930 * @param status okay or disabled as a token, not a string 1931 */ 1932 #define DT_NODE_HAS_COMPAT_STATUS(node_id, compat, status) \ 1933 DT_NODE_HAS_COMPAT(node_id, compat) && DT_NODE_HAS_STATUS(node_id, status) 1934 1935 /** 1936 * @brief Does a devicetree node have a property? 1937 * 1938 * Tests whether a devicetree node has a property defined. 1939 * 1940 * This tests whether the property is defined at all, not whether a 1941 * boolean property is true or false. To get a boolean property's 1942 * truth value, use DT_PROP(node_id, prop) instead. 1943 * 1944 * @param node_id node identifier 1945 * @param prop lowercase-and-underscores property name 1946 * @return 1 if the node has the property, 0 otherwise. 1947 */ 1948 #define DT_NODE_HAS_PROP(node_id, prop) \ 1949 IS_ENABLED(DT_CAT(node_id, _P_##prop##_EXISTS)) 1950 1951 1952 /** 1953 * @brief Does a phandle array have a named cell specifier at an index? 1954 * 1955 * If this returns 1, then the phandle-array property "pha" has a cell 1956 * named "cell" at index "idx", and therefore DT_PHA_BY_IDX(node_id, 1957 * pha, idx, cell) is valid. If it returns 0, it's an error to use 1958 * DT_PHA_BY_IDX() with the same arguments. 1959 * 1960 * @param node_id node identifier 1961 * @param pha lowercase-and-underscores property with type "phandle-array" 1962 * @param idx index to check within "pha" 1963 * @param cell lowercase-and-underscores cell name whose existence to check 1964 * at index "idx" 1965 * @return 1 if the named cell exists in the specifier at index idx, 1966 * 0 otherwise. 1967 */ 1968 #define DT_PHA_HAS_CELL_AT_IDX(node_id, pha, idx, cell) \ 1969 IS_ENABLED(DT_PROP(node_id, \ 1970 pha##_IDX_##idx##_VAL_##cell##_EXISTS)) 1971 1972 /** 1973 * @brief Equivalent to DT_PHA_HAS_CELL_AT_IDX(node_id, pha, 0, cell) 1974 * @param node_id node identifier 1975 * @param pha lowercase-and-underscores property with type "phandle-array" 1976 * @param cell lowercase-and-underscores cell name whose existence to check 1977 * at index "idx" 1978 * @return 1 if the named cell exists in the specifier at index 0, 1979 * 0 otherwise. 1980 */ 1981 #define DT_PHA_HAS_CELL(node_id, pha, cell) \ 1982 DT_PHA_HAS_CELL_AT_IDX(node_id, pha, 0, cell) 1983 1984 /** 1985 * @} 1986 */ 1987 1988 /** 1989 * @defgroup devicetree-generic-bus Bus helpers 1990 * @ingroup devicetree 1991 * @{ 1992 */ 1993 1994 /** 1995 * @brief Node's bus controller 1996 * 1997 * Get the node identifier of the node's bus controller. This can be 1998 * used with @ref DT_PROP() to get properties of the bus controller. 1999 * 2000 * It is an error to use this with nodes which do not have bus 2001 * controllers. 2002 * 2003 * Example devicetree fragment: 2004 * 2005 * i2c@deadbeef { 2006 * label = "I2C_CTLR"; 2007 * status = "okay"; 2008 * clock-frequency = < 100000 >; 2009 * 2010 * i2c_device: accelerometer@12 { 2011 * ... 2012 * }; 2013 * }; 2014 * 2015 * Example usage: 2016 * 2017 * DT_PROP(DT_BUS(DT_NODELABEL(i2c_device)), clock_frequency) // 100000 2018 * 2019 * @param node_id node identifier 2020 * @return a node identifier for the node's bus controller 2021 */ 2022 #define DT_BUS(node_id) DT_CAT(node_id, _BUS) 2023 2024 /** 2025 * @brief Node's bus controller's label property 2026 * @param node_id node identifier 2027 * @return the label property of the node's bus controller DT_BUS(node) 2028 */ 2029 #define DT_BUS_LABEL(node_id) DT_PROP(DT_BUS(node_id), label) 2030 2031 /** 2032 * @brief Is a node on a bus of a given type? 2033 * 2034 * Example devicetree overlay: 2035 * 2036 * &i2c0 { 2037 * temp: temperature-sensor@76 { 2038 * compatible = "vnd,some-sensor"; 2039 * reg = <0x76>; 2040 * }; 2041 * }; 2042 * 2043 * Example usage, assuming "i2c0" is an I2C bus controller node, and 2044 * therefore "temp" is on an I2C bus: 2045 * 2046 * DT_ON_BUS(DT_NODELABEL(temp), i2c) // 1 2047 * DT_ON_BUS(DT_NODELABEL(temp), spi) // 0 2048 * 2049 * @param node_id node identifier 2050 * @param bus lowercase-and-underscores bus type as a C token (i.e. 2051 * without quotes) 2052 * @return 1 if the node is on a bus of the given type, 2053 * 0 otherwise 2054 */ 2055 #define DT_ON_BUS(node_id, bus) IS_ENABLED(DT_CAT(node_id, _BUS_##bus)) 2056 2057 /** 2058 * @} 2059 */ 2060 2061 /** 2062 * @defgroup devicetree-inst Instance-based devicetree APIs 2063 * @ingroup devicetree 2064 * @{ 2065 */ 2066 2067 /** 2068 * @brief Node identifier for an instance of a DT_DRV_COMPAT compatible 2069 * @param inst instance number 2070 * @return a node identifier for the node with DT_DRV_COMPAT compatible and 2071 * instance number "inst" 2072 */ 2073 #define DT_DRV_INST(inst) DT_INST(inst, DT_DRV_COMPAT) 2074 2075 /** 2076 * @brief Call "fn" on all child nodes of DT_DRV_INST(inst). 2077 * 2078 * The macro "fn" should take one argument, which is the node 2079 * identifier for the child node. 2080 * 2081 * @param inst instance number 2082 * @param fn macro to invoke on each child node identifier 2083 * 2084 * @see DT_FOREACH_CHILD 2085 */ 2086 #define DT_INST_FOREACH_CHILD(inst, fn) \ 2087 DT_FOREACH_CHILD(DT_DRV_INST(inst), fn) 2088 2089 /** 2090 * @brief Call "fn" on all child nodes of DT_DRV_INST(inst). 2091 * 2092 * The macro "fn" takes multiple arguments. The first should be the node 2093 * identifier for the child node. The remaining are passed-in by the caller. 2094 * 2095 * @param inst instance number 2096 * @param fn macro to invoke on each child node identifier 2097 * @param ... variable number of arguments to pass to fn 2098 * 2099 * @see DT_FOREACH_CHILD 2100 */ 2101 #define DT_INST_FOREACH_CHILD_VARGS(inst, fn, ...) \ 2102 DT_FOREACH_CHILD_VARGS(DT_DRV_INST(inst), fn, __VA_ARGS__) 2103 2104 /** 2105 * @brief Get a DT_DRV_COMPAT instance property 2106 * @param inst instance number 2107 * @param prop lowercase-and-underscores property name 2108 * @return a representation of the property's value 2109 */ 2110 #define DT_INST_PROP(inst, prop) DT_PROP(DT_DRV_INST(inst), prop) 2111 2112 /** 2113 * @brief Get a DT_DRV_COMPAT property length 2114 * @param inst instance number 2115 * @param prop lowercase-and-underscores property name 2116 * @return logical length of the property 2117 */ 2118 #define DT_INST_PROP_LEN(inst, prop) DT_PROP_LEN(DT_DRV_INST(inst), prop) 2119 2120 /** 2121 * @brief Is index "idx" valid for an array type property 2122 * on a DT_DRV_COMPAT instance? 2123 * @param inst instance number 2124 * @param prop lowercase-and-underscores property name 2125 * @param idx index to check 2126 * @return 1 if "idx" is a valid index into the given property, 2127 * 0 otherwise. 2128 */ 2129 #define DT_INST_PROP_HAS_IDX(inst, prop, idx) \ 2130 DT_PROP_HAS_IDX(DT_DRV_INST(inst), prop, idx) 2131 2132 /** 2133 * @brief Get a DT_DRV_COMPAT element value in an array property 2134 * @param inst instance number 2135 * @param prop lowercase-and-underscores property name 2136 * @param idx the index to get 2137 * @return a representation of the idx-th element of the property 2138 */ 2139 #define DT_INST_PROP_BY_IDX(inst, prop, idx) \ 2140 DT_PROP_BY_IDX(DT_DRV_INST(inst), prop, idx) 2141 2142 /** 2143 * @brief Like DT_INST_PROP(), but with a fallback to default_value 2144 * @param inst instance number 2145 * @param prop lowercase-and-underscores property name 2146 * @param default_value a fallback value to expand to 2147 * @return DT_INST_PROP(inst, prop) or default_value 2148 */ 2149 #define DT_INST_PROP_OR(inst, prop, default_value) \ 2150 DT_PROP_OR(DT_DRV_INST(inst), prop, default_value) 2151 2152 /** 2153 * @brief Get a DT_DRV_COMPAT instance's "label" property 2154 * @param inst instance number 2155 * @return instance's label property value 2156 */ 2157 #define DT_INST_LABEL(inst) DT_INST_PROP(inst, label) 2158 2159 /** 2160 * @brief Get a DT_DRV_COMPAT instance's property value from a phandle's node 2161 * @param inst instance number 2162 * @param ph lowercase-and-underscores property of "inst" 2163 * with type "phandle" 2164 * @param prop lowercase-and-underscores property of the phandle's node 2165 * @return the value of "prop" as described in the DT_PROP() documentation 2166 */ 2167 #define DT_INST_PROP_BY_PHANDLE(inst, ph, prop) \ 2168 DT_INST_PROP_BY_PHANDLE_IDX(inst, ph, 0, prop) 2169 2170 /** 2171 * @brief Get a DT_DRV_COMPAT instance's property value from a phandle in a 2172 * property. 2173 * @param inst instance number 2174 * @param phs lowercase-and-underscores property with type "phandle", 2175 * "phandles", or "phandle-array" 2176 * @param idx logical index into "phs", which must be zero if "phs" 2177 * has type "phandle" 2178 * @param prop lowercase-and-underscores property of the phandle's node 2179 * @return the value of "prop" as described in the DT_PROP() documentation 2180 */ 2181 #define DT_INST_PROP_BY_PHANDLE_IDX(inst, phs, idx, prop) \ 2182 DT_PROP_BY_PHANDLE_IDX(DT_DRV_INST(inst), phs, idx, prop) 2183 2184 /** 2185 * @brief Get a DT_DRV_COMPAT instance's phandle-array specifier value at an index 2186 * @param inst instance number 2187 * @param pha lowercase-and-underscores property with type "phandle-array" 2188 * @param idx logical index into the property "pha" 2189 * @param cell binding's cell name within the specifier at index "idx" 2190 * @return the value of the cell inside the specifier at index "idx" 2191 */ 2192 #define DT_INST_PHA_BY_IDX(inst, pha, idx, cell) \ 2193 DT_PHA_BY_IDX(DT_DRV_INST(inst), pha, idx, cell) 2194 2195 /** 2196 * @brief Like DT_INST_PHA_BY_IDX(), but with a fallback to default_value 2197 * @param inst instance number 2198 * @param pha lowercase-and-underscores property with type "phandle-array" 2199 * @param idx logical index into the property "pha" 2200 * @param cell binding's cell name within the specifier at index "idx" 2201 * @param default_value a fallback value to expand to 2202 * @return DT_INST_PHA_BY_IDX(inst, pha, idx, cell) or default_value 2203 */ 2204 #define DT_INST_PHA_BY_IDX_OR(inst, pha, idx, cell, default_value) \ 2205 DT_PHA_BY_IDX_OR(DT_DRV_INST(inst), pha, idx, cell, default_value) 2206 2207 /** 2208 * @brief Get a DT_DRV_COMPAT instance's phandle-array specifier value 2209 * Equivalent to DT_INST_PHA_BY_IDX(inst, pha, 0, cell) 2210 * @param inst instance number 2211 * @param pha lowercase-and-underscores property with type "phandle-array" 2212 * @param cell binding's cell name for the specifier at "pha" index 0 2213 * @return the cell value 2214 */ 2215 #define DT_INST_PHA(inst, pha, cell) DT_INST_PHA_BY_IDX(inst, pha, 0, cell) 2216 2217 /** 2218 * @brief Like DT_INST_PHA(), but with a fallback to default_value 2219 * @param inst instance number 2220 * @param pha lowercase-and-underscores property with type "phandle-array" 2221 * @param cell binding's cell name for the specifier at "pha" index 0 2222 * @param default_value a fallback value to expand to 2223 * @return DT_INST_PHA(inst, pha, cell) or default_value 2224 */ 2225 #define DT_INST_PHA_OR(inst, pha, cell, default_value) \ 2226 DT_INST_PHA_BY_IDX_OR(inst, pha, 0, cell, default_value) 2227 2228 /** 2229 * @brief Get a DT_DRV_COMPAT instance's value within a phandle-array 2230 * specifier by name 2231 * @param inst instance number 2232 * @param pha lowercase-and-underscores property with type "phandle-array" 2233 * @param name lowercase-and-underscores name of a specifier in "pha" 2234 * @param cell binding's cell name for the named specifier 2235 * @return the cell value 2236 */ 2237 #define DT_INST_PHA_BY_NAME(inst, pha, name, cell) \ 2238 DT_PHA_BY_NAME(DT_DRV_INST(inst), pha, name, cell) 2239 2240 /** 2241 * @brief Like DT_INST_PHA_BY_NAME(), but with a fallback to default_value 2242 * @param inst instance number 2243 * @param pha lowercase-and-underscores property with type "phandle-array" 2244 * @param name lowercase-and-underscores name of a specifier in "pha" 2245 * @param cell binding's cell name for the named specifier 2246 * @param default_value a fallback value to expand to 2247 * @return DT_INST_PHA_BY_NAME(inst, pha, name, cell) or default_value 2248 */ 2249 #define DT_INST_PHA_BY_NAME_OR(inst, pha, name, cell, default_value) \ 2250 DT_PHA_BY_NAME_OR(DT_DRV_INST(inst), pha, name, cell, default_value) 2251 2252 /** 2253 * @brief Get a DT_DRV_COMPAT instance's phandle node identifier from a 2254 * phandle array by name 2255 * @param inst instance number 2256 * @param pha lowercase-and-underscores property with type "phandle-array" 2257 * @param name lowercase-and-underscores name of an element in "pha" 2258 * @return node identifier for the phandle at the element named "name" 2259 */ 2260 #define DT_INST_PHANDLE_BY_NAME(inst, pha, name) \ 2261 DT_PHANDLE_BY_NAME(DT_DRV_INST(inst), pha, name) \ 2262 2263 /** 2264 * @brief Get a DT_DRV_COMPAT instance's node identifier for a phandle in 2265 * a property. 2266 * @param inst instance number 2267 * @param prop lowercase-and-underscores property name in "inst" 2268 * with type "phandle", "phandles" or "phandle-array" 2269 * @param idx index into "prop" 2270 * @return a node identifier for the phandle at index "idx" in "prop" 2271 */ 2272 #define DT_INST_PHANDLE_BY_IDX(inst, prop, idx) \ 2273 DT_PHANDLE_BY_IDX(DT_DRV_INST(inst), prop, idx) 2274 2275 /** 2276 * @brief Get a DT_DRV_COMPAT instance's node identifier for a phandle 2277 * property's value 2278 * @param inst instance number 2279 * @param prop lowercase-and-underscores property of "inst" 2280 * with type "phandle" 2281 * @return a node identifier for the node pointed to by "ph" 2282 */ 2283 #define DT_INST_PHANDLE(inst, prop) DT_INST_PHANDLE_BY_IDX(inst, prop, 0) 2284 2285 /** 2286 * @brief is "idx" a valid register block index on a DT_DRV_COMPAT instance? 2287 * @param inst instance number 2288 * @param idx index to check 2289 * @return 1 if "idx" is a valid register block index, 2290 * 0 otherwise. 2291 */ 2292 #define DT_INST_REG_HAS_IDX(inst, idx) DT_REG_HAS_IDX(DT_DRV_INST(inst), idx) 2293 2294 /** 2295 * @brief Get a DT_DRV_COMPAT instance's idx-th register block's address 2296 * @param inst instance number 2297 * @param idx index of the register whose address to return 2298 * @return address of the instance's idx-th register block 2299 */ 2300 #define DT_INST_REG_ADDR_BY_IDX(inst, idx) DT_REG_ADDR_BY_IDX(DT_DRV_INST(inst), idx) 2301 2302 /** 2303 * @brief Get a DT_DRV_COMPAT instance's idx-th register block's size 2304 * @param inst instance number 2305 * @param idx index of the register whose size to return 2306 * @return size of the instance's idx-th register block 2307 */ 2308 #define DT_INST_REG_SIZE_BY_IDX(inst, idx) \ 2309 DT_REG_SIZE_BY_IDX(DT_DRV_INST(inst), idx) 2310 2311 /** 2312 * @brief Get a DT_DRV_COMPAT's register block address by name 2313 * @param inst instance number 2314 * @param name lowercase-and-underscores register specifier name 2315 * @return address of the register block with the given name 2316 */ 2317 #define DT_INST_REG_ADDR_BY_NAME(inst, name) \ 2318 DT_REG_ADDR_BY_NAME(DT_DRV_INST(inst), name) 2319 2320 /** 2321 * @brief Get a DT_DRV_COMPAT's register block size by name 2322 * @param inst instance number 2323 * @param name lowercase-and-underscores register specifier name 2324 * @return size of the register block with the given name 2325 */ 2326 #define DT_INST_REG_SIZE_BY_NAME(inst, name) \ 2327 DT_REG_SIZE_BY_NAME(DT_DRV_INST(inst), name) 2328 2329 /** 2330 * @brief Get a DT_DRV_COMPAT's (only) register block address 2331 * @param inst instance number 2332 * @return instance's register block address 2333 */ 2334 #define DT_INST_REG_ADDR(inst) DT_INST_REG_ADDR_BY_IDX(inst, 0) 2335 2336 /** 2337 * @brief Get a DT_DRV_COMPAT's (only) register block size 2338 * @param inst instance number 2339 * @return instance's register block size 2340 */ 2341 #define DT_INST_REG_SIZE(inst) DT_INST_REG_SIZE_BY_IDX(inst, 0) 2342 2343 /** 2344 * @brief Get a DT_DRV_COMPAT interrupt specifier value at an index 2345 * @param inst instance number 2346 * @param idx logical index into the interrupt specifier array 2347 * @param cell cell name specifier 2348 * @return the named value at the specifier given by the index 2349 */ 2350 #define DT_INST_IRQ_BY_IDX(inst, idx, cell) \ 2351 DT_IRQ_BY_IDX(DT_DRV_INST(inst), idx, cell) 2352 2353 /** 2354 * @brief Get a DT_DRV_COMPAT interrupt specifier value by name 2355 * @param inst instance number 2356 * @param name lowercase-and-underscores interrupt specifier name 2357 * @param cell cell name specifier 2358 * @return the named value at the specifier given by the index 2359 */ 2360 #define DT_INST_IRQ_BY_NAME(inst, name, cell) \ 2361 DT_IRQ_BY_NAME(DT_DRV_INST(inst), name, cell) 2362 2363 /** 2364 * @brief Get a DT_DRV_COMPAT interrupt specifier's value 2365 * @param inst instance number 2366 * @param cell cell name specifier 2367 * @return the named value at that index 2368 */ 2369 #define DT_INST_IRQ(inst, cell) DT_INST_IRQ_BY_IDX(inst, 0, cell) 2370 2371 /** 2372 * @brief Get a DT_DRV_COMPAT's (only) irq number 2373 * @param inst instance number 2374 * @return the interrupt number for the node's only interrupt 2375 */ 2376 #define DT_INST_IRQN(inst) DT_INST_IRQ(inst, irq) 2377 2378 /** 2379 * @brief Get a DT_DRV_COMPAT's bus node identifier 2380 * @param inst instance number 2381 * @return node identifier for the instance's bus node 2382 */ 2383 #define DT_INST_BUS(inst) DT_BUS(DT_DRV_INST(inst)) 2384 2385 /** 2386 * @brief Get a DT_DRV_COMPAT's bus node's label property 2387 * @param inst instance number 2388 * @return the label property of the instance's bus controller 2389 */ 2390 #define DT_INST_BUS_LABEL(inst) DT_BUS_LABEL(DT_DRV_INST(inst)) 2391 2392 /** 2393 * @brief Test if a DT_DRV_COMPAT's bus type is a given type 2394 * @param inst instance number 2395 * @param bus a binding's bus type as a C token, lowercased and without quotes 2396 * @return 1 if the given instance is on a bus of the given type, 2397 * 0 otherwise 2398 */ 2399 #define DT_INST_ON_BUS(inst, bus) DT_ON_BUS(DT_DRV_INST(inst), bus) 2400 2401 /** 2402 * @brief Test if any DT_DRV_COMPAT node is on a bus of a given type 2403 * and has status okay 2404 * 2405 * This is a special-purpose macro which can be useful when writing 2406 * drivers for devices which can appear on multiple buses. One example 2407 * is a sensor device which may be wired on an I2C or SPI bus. 2408 * 2409 * Example devicetree overlay: 2410 * 2411 * &i2c0 { 2412 * temp: temperature-sensor@76 { 2413 * compatible = "vnd,some-sensor"; 2414 * reg = <0x76>; 2415 * }; 2416 * }; 2417 * 2418 * Example usage, assuming "i2c0" is an I2C bus controller node, and 2419 * therefore "temp" is on an I2C bus: 2420 * 2421 * #define DT_DRV_COMPAT vnd_some_sensor 2422 * 2423 * DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) // 1 2424 * 2425 * @param bus a binding's bus type as a C token, lowercased and without quotes 2426 * @return 1 if any enabled node with that compatible is on that bus type, 2427 * 0 otherwise 2428 */ 2429 #define DT_ANY_INST_ON_BUS_STATUS_OKAY(bus) \ 2430 DT_COMPAT_ON_BUS_INTERNAL(DT_DRV_COMPAT, bus) 2431 2432 /** 2433 * @brief Call "fn" on all nodes with compatible DT_DRV_COMPAT 2434 * and status "okay" 2435 * 2436 * This macro calls "fn(inst)" on each "inst" number that refers to a 2437 * node with status "okay". Whitespace is added between invocations. 2438 * 2439 * Example devicetree fragment: 2440 * 2441 * a { 2442 * compatible = "vnd,device"; 2443 * status = "okay"; 2444 * label = "DEV_A"; 2445 * }; 2446 * 2447 * b { 2448 * compatible = "vnd,device"; 2449 * status = "okay"; 2450 * label = "DEV_B"; 2451 * }; 2452 * 2453 * c { 2454 * compatible = "vnd,device"; 2455 * status = "disabled"; 2456 * label = "DEV_C"; 2457 * }; 2458 * 2459 * Example usage: 2460 * 2461 * #define DT_DRV_COMPAT vnd_device 2462 * #define MY_FN(inst) DT_INST_LABEL(inst), 2463 * 2464 * DT_INST_FOREACH_STATUS_OKAY(MY_FN) 2465 * 2466 * This expands to: 2467 * 2468 * MY_FN(0) MY_FN(1) 2469 * 2470 * and from there, to either this: 2471 * 2472 * "DEV_A", "DEV_B", 2473 * 2474 * or this: 2475 * 2476 * "DEV_B", "DEV_A", 2477 * 2478 * No guarantees are made about the order that a and b appear in the 2479 * expansion. 2480 * 2481 * Note that "fn" is responsible for adding commas, semicolons, or 2482 * other separators or terminators. 2483 * 2484 * Device drivers should use this macro whenever possible to 2485 * instantiate a struct device for each enabled node in the devicetree 2486 * of the driver's compatible DT_DRV_COMPAT. 2487 * 2488 * @param fn Macro to call for each enabled node. Must accept an 2489 * instance number as its only parameter. 2490 */ 2491 #define DT_INST_FOREACH_STATUS_OKAY(fn) \ 2492 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), \ 2493 (UTIL_CAT(DT_FOREACH_OKAY_INST_, \ 2494 DT_DRV_COMPAT)(fn)), \ 2495 ()) 2496 2497 /** 2498 * @brief Call "fn" on all nodes with compatible DT_DRV_COMPAT 2499 * and status "okay" with multiple arguments 2500 * 2501 * 2502 * @param fn Macro to call for each enabled node. Must accept an 2503 * instance number as its only parameter. 2504 * @param ... variable number of arguments to pass to fn 2505 * 2506 * @see DT_INST_FOREACH_STATUS_OKAY 2507 */ 2508 #define DT_INST_FOREACH_STATUS_OKAY_VARGS(fn, ...) \ 2509 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), \ 2510 (UTIL_CAT(DT_FOREACH_OKAY_INST_VARGS_, \ 2511 DT_DRV_COMPAT)(fn, __VA_ARGS__)), \ 2512 ()) 2513 2514 /** 2515 * @brief Invokes "fn" for each element of property "prop" for 2516 * a DT_DRV_COMPAT instance. 2517 * 2518 * Equivalent to DT_FOREACH_PROP_ELEM(DT_DRV_INST(inst), prop, fn). 2519 * 2520 * @param inst instance number 2521 * @param prop lowercase-and-underscores property name 2522 * @param fn macro to invoke 2523 */ 2524 #define DT_INST_FOREACH_PROP_ELEM(inst, prop, fn) \ 2525 DT_FOREACH_PROP_ELEM(DT_DRV_INST(inst), prop, fn) 2526 2527 /** 2528 * @brief Invokes "fn" for each element of property "prop" for 2529 * a DT_DRV_COMPAT instance with multiple arguments. 2530 * 2531 * Equivalent to 2532 * DT_FOREACH_PROP_ELEM_VARGS(DT_DRV_INST(inst), prop, fn, __VA_ARGS__) 2533 * 2534 * @param inst instance number 2535 * @param prop lowercase-and-underscores property name 2536 * @param fn macro to invoke 2537 * @param ... variable number of arguments to pass to fn 2538 * 2539 * @see DT_INST_FOREACH_PROP_ELEM 2540 */ 2541 #define DT_INST_FOREACH_PROP_ELEM_VARGS(inst, prop, fn, ...) \ 2542 DT_FOREACH_PROP_ELEM_VARGS(DT_DRV_INST(inst), prop, fn, __VA_ARGS__) 2543 2544 /** 2545 * @brief Does a DT_DRV_COMPAT instance have a property? 2546 * @param inst instance number 2547 * @param prop lowercase-and-underscores property name 2548 * @return 1 if the instance has the property, 0 otherwise. 2549 */ 2550 #define DT_INST_NODE_HAS_PROP(inst, prop) \ 2551 DT_NODE_HAS_PROP(DT_DRV_INST(inst), prop) 2552 2553 /** 2554 * @brief Does a phandle array have a named cell specifier at an index 2555 * for a DT_DRV_COMPAT instance? 2556 * @param inst instance number 2557 * @param pha lowercase-and-underscores property with type "phandle-array" 2558 * @param idx index to check 2559 * @param cell named cell value whose existence to check 2560 * @return 1 if the named cell exists in the specifier at index idx, 2561 * 0 otherwise. 2562 */ 2563 #define DT_INST_PHA_HAS_CELL_AT_IDX(inst, pha, idx, cell) \ 2564 DT_PHA_HAS_CELL_AT_IDX(DT_DRV_INST(inst), pha, idx, cell) 2565 2566 /** 2567 * @brief Does a phandle array have a named cell specifier at index 0 2568 * for a DT_DRV_COMPAT instance? 2569 * @param inst instance number 2570 * @param pha lowercase-and-underscores property with type "phandle-array" 2571 * @param cell named cell value whose existence to check 2572 * @return 1 if the named cell exists in the specifier at index 0, 2573 * 0 otherwise. 2574 */ 2575 #define DT_INST_PHA_HAS_CELL(inst, pha, cell) \ 2576 DT_INST_PHA_HAS_CELL_AT_IDX(inst, pha, 0, cell) 2577 2578 /** 2579 * @brief is index valid for interrupt property on a DT_DRV_COMPAT instance? 2580 * @param inst instance number 2581 * @param idx logical index into the interrupt specifier array 2582 * @return 1 if the idx is valid for the interrupt property 2583 * 0 otherwise. 2584 */ 2585 #define DT_INST_IRQ_HAS_IDX(inst, idx) DT_IRQ_HAS_IDX(DT_DRV_INST(inst), idx) 2586 2587 /** 2588 * @brief Does a DT_DRV_COMPAT instance have an interrupt named cell specifier? 2589 * @param inst instance number 2590 * @param idx index to check 2591 * @param cell named cell value whose existence to check 2592 * @return 1 if the named cell exists in the interrupt specifier at index idx 2593 * 0 otherwise. 2594 */ 2595 #define DT_INST_IRQ_HAS_CELL_AT_IDX(inst, idx, cell) \ 2596 DT_IRQ_HAS_CELL_AT_IDX(DT_DRV_INST(inst), idx, cell) 2597 2598 /** 2599 * @brief Does a DT_DRV_COMPAT instance have an interrupt value? 2600 * @param inst instance number 2601 * @param cell named cell value whose existence to check 2602 * @return 1 if the named cell exists in the interrupt specifier at index 0 2603 * 0 otherwise. 2604 */ 2605 #define DT_INST_IRQ_HAS_CELL(inst, cell) \ 2606 DT_INST_IRQ_HAS_CELL_AT_IDX(inst, 0, cell) 2607 2608 /** 2609 * @brief Does a DT_DRV_COMPAT instance have an interrupt value? 2610 * @param inst instance number 2611 * @param name lowercase-and-underscores interrupt specifier name 2612 * @return 1 if "name" is a valid named specifier 2613 */ 2614 #define DT_INST_IRQ_HAS_NAME(inst, name) \ 2615 DT_IRQ_HAS_NAME(DT_DRV_INST(inst), name) 2616 2617 /** 2618 * @} 2619 */ 2620 2621 /** @internal pay no attention to the man behind the curtain! */ 2622 #define DT_PATH_INTERNAL(...) \ 2623 UTIL_CAT(DT_ROOT, MACRO_MAP_CAT(DT_S_PREFIX, __VA_ARGS__)) 2624 /** @internal helper for DT_PATH(): prepends _S_ to a node name */ 2625 #define DT_S_PREFIX(name) _S_##name 2626 2627 /** 2628 * @internal concatenation helper, 2 arguments 2629 * 2630 * This and the following macros are used to paste things together 2631 * with "##" *after* forcing expansion on each argument. 2632 * 2633 * We could try to use something like UTIL_CAT(), but the compiler 2634 * error messages from the util macros can be extremely long when they 2635 * are misused. This unfortunately happens often with devicetree.h, 2636 * since its macro-based API is fiddly and can be hard to get right. 2637 * 2638 * Keeping things brutally simple here hopefully makes some errors 2639 * easier to read. 2640 */ 2641 #define DT_CAT(a1, a2) a1 ## a2 2642 /** @internal concatenation helper, 3 arguments */ 2643 #define DT_CAT3(a1, a2, a3) a1 ## a2 ## a3 2644 /** @internal concatenation helper, 4 arguments */ 2645 #define DT_CAT4(a1, a2, a3, a4) a1 ## a2 ## a3 ## a4 2646 /** @internal concatenation helper, 5 arguments */ 2647 #define DT_CAT5(a1, a2, a3, a4, a5) a1 ## a2 ## a3 ## a4 ## a5 2648 /** @internal concatenation helper, 6 arguments */ 2649 #define DT_CAT6(a1, a2, a3, a4, a5, a6) a1 ## a2 ## a3 ## a4 ## a5 ## a6 2650 /* 2651 * If you need to define a bigger DT_CATN(), do so here. Don't leave 2652 * any "holes" of undefined macros, please. 2653 */ 2654 2655 /** @internal helper for node identifier macros to expand args */ 2656 #define DT_DASH(...) MACRO_MAP_CAT(DT_DASH_PREFIX, __VA_ARGS__) 2657 /** @internal helper for DT_DASH(): prepends _ to a name */ 2658 #define DT_DASH_PREFIX(name) _##name 2659 /** @internal helper for DT_NODE_HAS_STATUS */ 2660 #define DT_NODE_HAS_STATUS_INTERNAL(node_id, status) \ 2661 IS_ENABLED(DT_CAT(node_id, _STATUS_ ## status)) 2662 /** @internal helper for test cases and DT_ANY_INST_ON_BUS_STATUS_OKAY() */ 2663 #define DT_COMPAT_ON_BUS_INTERNAL(compat, bus) \ 2664 IS_ENABLED(UTIL_CAT(DT_CAT(DT_COMPAT_, compat), _BUS_##bus)) 2665 2666 /* have these last so they have access to all previously defined macros */ 2667 #include <devicetree/io-channels.h> 2668 #include <devicetree/clocks.h> 2669 #include <devicetree/gpio.h> 2670 #include <devicetree/spi.h> 2671 #include <devicetree/dma.h> 2672 #include <devicetree/pwms.h> 2673 #include <devicetree/fixed-partitions.h> 2674 #include <devicetree/zephyr.h> 2675 #include <devicetree/ordinals.h> 2676 #include <devicetree/pinctrl.h> 2677 2678 #endif /* DEVICETREE_H */ 2679