devicetree.h 85 KB

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