property.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * drivers/of/property.c - Procedures for accessing and interpreting
  4. * Devicetree properties and graphs.
  5. *
  6. * Initially created by copying procedures from drivers/of/base.c. This
  7. * file contains the OF property as well as the OF graph interface
  8. * functions.
  9. *
  10. * Paul Mackerras August 1996.
  11. * Copyright (C) 1996-2005 Paul Mackerras.
  12. *
  13. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  14. * {engebret|bergner}@us.ibm.com
  15. *
  16. * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
  17. *
  18. * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
  19. * Grant Likely.
  20. */
  21. #define pr_fmt(fmt) "OF: " fmt
  22. #include <linux/ctype.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_graph.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/string.h>
  29. #include <linux/moduleparam.h>
  30. #include "of_private.h"
  31. /**
  32. * of_property_read_bool - Find a property
  33. * @np: device node from which the property value is to be read.
  34. * @propname: name of the property to be searched.
  35. *
  36. * Search for a boolean property in a device node. Usage on non-boolean
  37. * property types is deprecated.
  38. *
  39. * Return: true if the property exists false otherwise.
  40. */
  41. bool of_property_read_bool(const struct device_node *np, const char *propname)
  42. {
  43. struct property *prop = of_find_property(np, propname, NULL);
  44. /*
  45. * Boolean properties should not have a value. Testing for property
  46. * presence should either use of_property_present() or just read the
  47. * property value and check the returned error code.
  48. */
  49. if (prop && prop->length)
  50. pr_warn("%pOF: Read of boolean property '%s' with a value.\n", np, propname);
  51. return prop ? true : false;
  52. }
  53. EXPORT_SYMBOL(of_property_read_bool);
  54. /**
  55. * of_graph_is_present() - check graph's presence
  56. * @node: pointer to device_node containing graph port
  57. *
  58. * Return: True if @node has a port or ports (with a port) sub-node,
  59. * false otherwise.
  60. */
  61. bool of_graph_is_present(const struct device_node *node)
  62. {
  63. struct device_node *ports __free(device_node) = of_get_child_by_name(node, "ports");
  64. if (ports)
  65. node = ports;
  66. struct device_node *port __free(device_node) = of_get_child_by_name(node, "port");
  67. return !!port;
  68. }
  69. EXPORT_SYMBOL(of_graph_is_present);
  70. /**
  71. * of_property_count_elems_of_size - Count the number of elements in a property
  72. *
  73. * @np: device node from which the property value is to be read.
  74. * @propname: name of the property to be searched.
  75. * @elem_size: size of the individual element
  76. *
  77. * Search for a property in a device node and count the number of elements of
  78. * size elem_size in it.
  79. *
  80. * Return: The number of elements on sucess, -EINVAL if the property does not
  81. * exist or its length does not match a multiple of elem_size and -ENODATA if
  82. * the property does not have a value.
  83. */
  84. int of_property_count_elems_of_size(const struct device_node *np,
  85. const char *propname, int elem_size)
  86. {
  87. const struct property *prop = of_find_property(np, propname, NULL);
  88. if (!prop)
  89. return -EINVAL;
  90. if (!prop->value)
  91. return -ENODATA;
  92. if (prop->length % elem_size != 0) {
  93. pr_err("size of %s in node %pOF is not a multiple of %d\n",
  94. propname, np, elem_size);
  95. return -EINVAL;
  96. }
  97. return prop->length / elem_size;
  98. }
  99. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  100. /**
  101. * of_find_property_value_of_size
  102. *
  103. * @np: device node from which the property value is to be read.
  104. * @propname: name of the property to be searched.
  105. * @min: minimum allowed length of property value
  106. * @max: maximum allowed length of property value (0 means unlimited)
  107. * @len: if !=NULL, actual length is written to here
  108. *
  109. * Search for a property in a device node and valid the requested size.
  110. *
  111. * Return: The property value on success, -EINVAL if the property does not
  112. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  113. * property data is too small or too large.
  114. *
  115. */
  116. static void *of_find_property_value_of_size(const struct device_node *np,
  117. const char *propname, u32 min, u32 max, size_t *len)
  118. {
  119. const struct property *prop = of_find_property(np, propname, NULL);
  120. if (!prop)
  121. return ERR_PTR(-EINVAL);
  122. if (!prop->value)
  123. return ERR_PTR(-ENODATA);
  124. if (prop->length < min)
  125. return ERR_PTR(-EOVERFLOW);
  126. if (max && prop->length > max)
  127. return ERR_PTR(-EOVERFLOW);
  128. if (len)
  129. *len = prop->length;
  130. return prop->value;
  131. }
  132. /**
  133. * of_property_read_u8_index - Find and read a u8 from a multi-value property.
  134. *
  135. * @np: device node from which the property value is to be read.
  136. * @propname: name of the property to be searched.
  137. * @index: index of the u8 in the list of values
  138. * @out_value: pointer to return value, modified only if no error.
  139. *
  140. * Search for a property in a device node and read nth 8-bit value from
  141. * it.
  142. *
  143. * Return: 0 on success, -EINVAL if the property does not exist,
  144. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  145. * property data isn't large enough.
  146. *
  147. * The out_value is modified only if a valid u8 value can be decoded.
  148. */
  149. int of_property_read_u8_index(const struct device_node *np,
  150. const char *propname,
  151. u32 index, u8 *out_value)
  152. {
  153. const u8 *val = of_find_property_value_of_size(np, propname,
  154. ((index + 1) * sizeof(*out_value)),
  155. 0, NULL);
  156. if (IS_ERR(val))
  157. return PTR_ERR(val);
  158. *out_value = val[index];
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(of_property_read_u8_index);
  162. /**
  163. * of_property_read_u16_index - Find and read a u16 from a multi-value property.
  164. *
  165. * @np: device node from which the property value is to be read.
  166. * @propname: name of the property to be searched.
  167. * @index: index of the u16 in the list of values
  168. * @out_value: pointer to return value, modified only if no error.
  169. *
  170. * Search for a property in a device node and read nth 16-bit value from
  171. * it.
  172. *
  173. * Return: 0 on success, -EINVAL if the property does not exist,
  174. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  175. * property data isn't large enough.
  176. *
  177. * The out_value is modified only if a valid u16 value can be decoded.
  178. */
  179. int of_property_read_u16_index(const struct device_node *np,
  180. const char *propname,
  181. u32 index, u16 *out_value)
  182. {
  183. const u16 *val = of_find_property_value_of_size(np, propname,
  184. ((index + 1) * sizeof(*out_value)),
  185. 0, NULL);
  186. if (IS_ERR(val))
  187. return PTR_ERR(val);
  188. *out_value = be16_to_cpup(((__be16 *)val) + index);
  189. return 0;
  190. }
  191. EXPORT_SYMBOL_GPL(of_property_read_u16_index);
  192. /**
  193. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  194. *
  195. * @np: device node from which the property value is to be read.
  196. * @propname: name of the property to be searched.
  197. * @index: index of the u32 in the list of values
  198. * @out_value: pointer to return value, modified only if no error.
  199. *
  200. * Search for a property in a device node and read nth 32-bit value from
  201. * it.
  202. *
  203. * Return: 0 on success, -EINVAL if the property does not exist,
  204. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  205. * property data isn't large enough.
  206. *
  207. * The out_value is modified only if a valid u32 value can be decoded.
  208. */
  209. int of_property_read_u32_index(const struct device_node *np,
  210. const char *propname,
  211. u32 index, u32 *out_value)
  212. {
  213. const u32 *val = of_find_property_value_of_size(np, propname,
  214. ((index + 1) * sizeof(*out_value)),
  215. 0,
  216. NULL);
  217. if (IS_ERR(val))
  218. return PTR_ERR(val);
  219. *out_value = be32_to_cpup(((__be32 *)val) + index);
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  223. /**
  224. * of_property_read_u64_index - Find and read a u64 from a multi-value property.
  225. *
  226. * @np: device node from which the property value is to be read.
  227. * @propname: name of the property to be searched.
  228. * @index: index of the u64 in the list of values
  229. * @out_value: pointer to return value, modified only if no error.
  230. *
  231. * Search for a property in a device node and read nth 64-bit value from
  232. * it.
  233. *
  234. * Return: 0 on success, -EINVAL if the property does not exist,
  235. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  236. * property data isn't large enough.
  237. *
  238. * The out_value is modified only if a valid u64 value can be decoded.
  239. */
  240. int of_property_read_u64_index(const struct device_node *np,
  241. const char *propname,
  242. u32 index, u64 *out_value)
  243. {
  244. const u64 *val = of_find_property_value_of_size(np, propname,
  245. ((index + 1) * sizeof(*out_value)),
  246. 0, NULL);
  247. if (IS_ERR(val))
  248. return PTR_ERR(val);
  249. *out_value = be64_to_cpup(((__be64 *)val) + index);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL_GPL(of_property_read_u64_index);
  253. /**
  254. * of_property_read_variable_u8_array - Find and read an array of u8 from a
  255. * property, with bounds on the minimum and maximum array size.
  256. *
  257. * @np: device node from which the property value is to be read.
  258. * @propname: name of the property to be searched.
  259. * @out_values: pointer to found values.
  260. * @sz_min: minimum number of array elements to read
  261. * @sz_max: maximum number of array elements to read, if zero there is no
  262. * upper limit on the number of elements in the dts entry but only
  263. * sz_min will be read.
  264. *
  265. * Search for a property in a device node and read 8-bit value(s) from
  266. * it.
  267. *
  268. * dts entry of array should be like:
  269. * ``property = /bits/ 8 <0x50 0x60 0x70>;``
  270. *
  271. * Return: The number of elements read on success, -EINVAL if the property
  272. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  273. * if the property data is smaller than sz_min or longer than sz_max.
  274. *
  275. * The out_values is modified only if a valid u8 value can be decoded.
  276. */
  277. int of_property_read_variable_u8_array(const struct device_node *np,
  278. const char *propname, u8 *out_values,
  279. size_t sz_min, size_t sz_max)
  280. {
  281. size_t sz, count;
  282. const u8 *val = of_find_property_value_of_size(np, propname,
  283. (sz_min * sizeof(*out_values)),
  284. (sz_max * sizeof(*out_values)),
  285. &sz);
  286. if (IS_ERR(val))
  287. return PTR_ERR(val);
  288. if (!sz_max)
  289. sz = sz_min;
  290. else
  291. sz /= sizeof(*out_values);
  292. count = sz;
  293. while (count--)
  294. *out_values++ = *val++;
  295. return sz;
  296. }
  297. EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
  298. /**
  299. * of_property_read_variable_u16_array - Find and read an array of u16 from a
  300. * property, with bounds on the minimum and maximum array size.
  301. *
  302. * @np: device node from which the property value is to be read.
  303. * @propname: name of the property to be searched.
  304. * @out_values: pointer to found values.
  305. * @sz_min: minimum number of array elements to read
  306. * @sz_max: maximum number of array elements to read, if zero there is no
  307. * upper limit on the number of elements in the dts entry but only
  308. * sz_min will be read.
  309. *
  310. * Search for a property in a device node and read 16-bit value(s) from
  311. * it.
  312. *
  313. * dts entry of array should be like:
  314. * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
  315. *
  316. * Return: The number of elements read on success, -EINVAL if the property
  317. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  318. * if the property data is smaller than sz_min or longer than sz_max.
  319. *
  320. * The out_values is modified only if a valid u16 value can be decoded.
  321. */
  322. int of_property_read_variable_u16_array(const struct device_node *np,
  323. const char *propname, u16 *out_values,
  324. size_t sz_min, size_t sz_max)
  325. {
  326. size_t sz, count;
  327. const __be16 *val = of_find_property_value_of_size(np, propname,
  328. (sz_min * sizeof(*out_values)),
  329. (sz_max * sizeof(*out_values)),
  330. &sz);
  331. if (IS_ERR(val))
  332. return PTR_ERR(val);
  333. if (!sz_max)
  334. sz = sz_min;
  335. else
  336. sz /= sizeof(*out_values);
  337. count = sz;
  338. while (count--)
  339. *out_values++ = be16_to_cpup(val++);
  340. return sz;
  341. }
  342. EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
  343. /**
  344. * of_property_read_variable_u32_array - Find and read an array of 32 bit
  345. * integers from a property, with bounds on the minimum and maximum array size.
  346. *
  347. * @np: device node from which the property value is to be read.
  348. * @propname: name of the property to be searched.
  349. * @out_values: pointer to return found values.
  350. * @sz_min: minimum number of array elements to read
  351. * @sz_max: maximum number of array elements to read, if zero there is no
  352. * upper limit on the number of elements in the dts entry but only
  353. * sz_min will be read.
  354. *
  355. * Search for a property in a device node and read 32-bit value(s) from
  356. * it.
  357. *
  358. * Return: The number of elements read on success, -EINVAL if the property
  359. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  360. * if the property data is smaller than sz_min or longer than sz_max.
  361. *
  362. * The out_values is modified only if a valid u32 value can be decoded.
  363. */
  364. int of_property_read_variable_u32_array(const struct device_node *np,
  365. const char *propname, u32 *out_values,
  366. size_t sz_min, size_t sz_max)
  367. {
  368. size_t sz, count;
  369. const __be32 *val = of_find_property_value_of_size(np, propname,
  370. (sz_min * sizeof(*out_values)),
  371. (sz_max * sizeof(*out_values)),
  372. &sz);
  373. if (IS_ERR(val))
  374. return PTR_ERR(val);
  375. if (!sz_max)
  376. sz = sz_min;
  377. else
  378. sz /= sizeof(*out_values);
  379. count = sz;
  380. while (count--)
  381. *out_values++ = be32_to_cpup(val++);
  382. return sz;
  383. }
  384. EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
  385. /**
  386. * of_property_read_u64 - Find and read a 64 bit integer from a property
  387. * @np: device node from which the property value is to be read.
  388. * @propname: name of the property to be searched.
  389. * @out_value: pointer to return value, modified only if return value is 0.
  390. *
  391. * Search for a property in a device node and read a 64-bit value from
  392. * it.
  393. *
  394. * Return: 0 on success, -EINVAL if the property does not exist,
  395. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  396. * property data isn't large enough.
  397. *
  398. * The out_value is modified only if a valid u64 value can be decoded.
  399. */
  400. int of_property_read_u64(const struct device_node *np, const char *propname,
  401. u64 *out_value)
  402. {
  403. const __be32 *val = of_find_property_value_of_size(np, propname,
  404. sizeof(*out_value),
  405. 0,
  406. NULL);
  407. if (IS_ERR(val))
  408. return PTR_ERR(val);
  409. *out_value = of_read_number(val, 2);
  410. return 0;
  411. }
  412. EXPORT_SYMBOL_GPL(of_property_read_u64);
  413. /**
  414. * of_property_read_variable_u64_array - Find and read an array of 64 bit
  415. * integers from a property, with bounds on the minimum and maximum array size.
  416. *
  417. * @np: device node from which the property value is to be read.
  418. * @propname: name of the property to be searched.
  419. * @out_values: pointer to found values.
  420. * @sz_min: minimum number of array elements to read
  421. * @sz_max: maximum number of array elements to read, if zero there is no
  422. * upper limit on the number of elements in the dts entry but only
  423. * sz_min will be read.
  424. *
  425. * Search for a property in a device node and read 64-bit value(s) from
  426. * it.
  427. *
  428. * Return: The number of elements read on success, -EINVAL if the property
  429. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  430. * if the property data is smaller than sz_min or longer than sz_max.
  431. *
  432. * The out_values is modified only if a valid u64 value can be decoded.
  433. */
  434. int of_property_read_variable_u64_array(const struct device_node *np,
  435. const char *propname, u64 *out_values,
  436. size_t sz_min, size_t sz_max)
  437. {
  438. size_t sz, count;
  439. const __be32 *val = of_find_property_value_of_size(np, propname,
  440. (sz_min * sizeof(*out_values)),
  441. (sz_max * sizeof(*out_values)),
  442. &sz);
  443. if (IS_ERR(val))
  444. return PTR_ERR(val);
  445. if (!sz_max)
  446. sz = sz_min;
  447. else
  448. sz /= sizeof(*out_values);
  449. count = sz;
  450. while (count--) {
  451. *out_values++ = of_read_number(val, 2);
  452. val += 2;
  453. }
  454. return sz;
  455. }
  456. EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
  457. /**
  458. * of_property_read_string - Find and read a string from a property
  459. * @np: device node from which the property value is to be read.
  460. * @propname: name of the property to be searched.
  461. * @out_string: pointer to null terminated return string, modified only if
  462. * return value is 0.
  463. *
  464. * Search for a property in a device tree node and retrieve a null
  465. * terminated string value (pointer to data, not a copy).
  466. *
  467. * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
  468. * property does not have a value, and -EILSEQ if the string is not
  469. * null-terminated within the length of the property data.
  470. *
  471. * Note that the empty string "" has length of 1, thus -ENODATA cannot
  472. * be interpreted as an empty string.
  473. *
  474. * The out_string pointer is modified only if a valid string can be decoded.
  475. */
  476. int of_property_read_string(const struct device_node *np, const char *propname,
  477. const char **out_string)
  478. {
  479. const struct property *prop = of_find_property(np, propname, NULL);
  480. if (!prop)
  481. return -EINVAL;
  482. if (!prop->length)
  483. return -ENODATA;
  484. if (strnlen(prop->value, prop->length) >= prop->length)
  485. return -EILSEQ;
  486. *out_string = prop->value;
  487. return 0;
  488. }
  489. EXPORT_SYMBOL_GPL(of_property_read_string);
  490. /**
  491. * of_property_match_string() - Find string in a list and return index
  492. * @np: pointer to the node containing the string list property
  493. * @propname: string list property name
  494. * @string: pointer to the string to search for in the string list
  495. *
  496. * Search for an exact match of string in a device node property which is a
  497. * string of lists.
  498. *
  499. * Return: the index of the first occurrence of the string on success, -EINVAL
  500. * if the property does not exist, -ENODATA if the property does not have a
  501. * value, and -EILSEQ if the string is not null-terminated within the length of
  502. * the property data.
  503. */
  504. int of_property_match_string(const struct device_node *np, const char *propname,
  505. const char *string)
  506. {
  507. const struct property *prop = of_find_property(np, propname, NULL);
  508. size_t l;
  509. int i;
  510. const char *p, *end;
  511. if (!prop)
  512. return -EINVAL;
  513. if (!prop->value)
  514. return -ENODATA;
  515. p = prop->value;
  516. end = p + prop->length;
  517. for (i = 0; p < end; i++, p += l) {
  518. l = strnlen(p, end - p) + 1;
  519. if (p + l > end)
  520. return -EILSEQ;
  521. pr_debug("comparing %s with %s\n", string, p);
  522. if (strcmp(string, p) == 0)
  523. return i; /* Found it; return index */
  524. }
  525. return -ENODATA;
  526. }
  527. EXPORT_SYMBOL_GPL(of_property_match_string);
  528. /**
  529. * of_property_read_string_helper() - Utility helper for parsing string properties
  530. * @np: device node from which the property value is to be read.
  531. * @propname: name of the property to be searched.
  532. * @out_strs: output array of string pointers.
  533. * @sz: number of array elements to read.
  534. * @skip: Number of strings to skip over at beginning of list.
  535. *
  536. * Don't call this function directly. It is a utility helper for the
  537. * of_property_read_string*() family of functions.
  538. */
  539. int of_property_read_string_helper(const struct device_node *np,
  540. const char *propname, const char **out_strs,
  541. size_t sz, int skip)
  542. {
  543. const struct property *prop = of_find_property(np, propname, NULL);
  544. int l = 0, i = 0;
  545. const char *p, *end;
  546. if (!prop)
  547. return -EINVAL;
  548. if (!prop->value)
  549. return -ENODATA;
  550. p = prop->value;
  551. end = p + prop->length;
  552. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  553. l = strnlen(p, end - p) + 1;
  554. if (p + l > end)
  555. return -EILSEQ;
  556. if (out_strs && i >= skip)
  557. *out_strs++ = p;
  558. }
  559. i -= skip;
  560. return i <= 0 ? -ENODATA : i;
  561. }
  562. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  563. const __be32 *of_prop_next_u32(const struct property *prop, const __be32 *cur,
  564. u32 *pu)
  565. {
  566. const void *curv = cur;
  567. if (!prop)
  568. return NULL;
  569. if (!cur) {
  570. curv = prop->value;
  571. goto out_val;
  572. }
  573. curv += sizeof(*cur);
  574. if (curv >= prop->value + prop->length)
  575. return NULL;
  576. out_val:
  577. *pu = be32_to_cpup(curv);
  578. return curv;
  579. }
  580. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  581. const char *of_prop_next_string(const struct property *prop, const char *cur)
  582. {
  583. const void *curv = cur;
  584. if (!prop)
  585. return NULL;
  586. if (!cur)
  587. return prop->value;
  588. curv += strlen(cur) + 1;
  589. if (curv >= prop->value + prop->length)
  590. return NULL;
  591. return curv;
  592. }
  593. EXPORT_SYMBOL_GPL(of_prop_next_string);
  594. /**
  595. * of_graph_parse_endpoint() - parse common endpoint node properties
  596. * @node: pointer to endpoint device_node
  597. * @endpoint: pointer to the OF endpoint data structure
  598. *
  599. * The caller should hold a reference to @node.
  600. */
  601. int of_graph_parse_endpoint(const struct device_node *node,
  602. struct of_endpoint *endpoint)
  603. {
  604. struct device_node *port_node __free(device_node) =
  605. of_get_parent(node);
  606. WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
  607. __func__, node);
  608. memset(endpoint, 0, sizeof(*endpoint));
  609. endpoint->local_node = node;
  610. /*
  611. * It doesn't matter whether the two calls below succeed.
  612. * If they don't then the default value 0 is used.
  613. */
  614. of_property_read_u32(port_node, "reg", &endpoint->port);
  615. of_property_read_u32(node, "reg", &endpoint->id);
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(of_graph_parse_endpoint);
  619. /**
  620. * of_graph_get_port_by_id() - get the port matching a given id
  621. * @parent: pointer to the parent device node
  622. * @id: id of the port
  623. *
  624. * Return: A 'port' node pointer with refcount incremented. The caller
  625. * has to use of_node_put() on it when done.
  626. */
  627. struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
  628. {
  629. struct device_node *node __free(device_node) = of_get_child_by_name(parent, "ports");
  630. if (node)
  631. parent = node;
  632. for_each_child_of_node_scoped(parent, port) {
  633. u32 port_id = 0;
  634. if (!of_node_name_eq(port, "port"))
  635. continue;
  636. of_property_read_u32(port, "reg", &port_id);
  637. if (id == port_id)
  638. return_ptr(port);
  639. }
  640. return NULL;
  641. }
  642. EXPORT_SYMBOL(of_graph_get_port_by_id);
  643. /**
  644. * of_graph_get_next_port() - get next port node.
  645. * @parent: pointer to the parent device node, or parent ports node
  646. * @prev: previous port node, or NULL to get first
  647. *
  648. * Parent device node can be used as @parent whether device node has ports node
  649. * or not. It will work same as ports@0 node.
  650. *
  651. * Return: A 'port' node pointer with refcount incremented. Refcount
  652. * of the passed @prev node is decremented.
  653. */
  654. struct device_node *of_graph_get_next_port(const struct device_node *parent,
  655. struct device_node *prev)
  656. {
  657. if (!parent)
  658. return NULL;
  659. if (!prev) {
  660. struct device_node *node __free(device_node) =
  661. of_get_child_by_name(parent, "ports");
  662. if (node)
  663. parent = node;
  664. return of_get_child_by_name(parent, "port");
  665. }
  666. do {
  667. prev = of_get_next_child(parent, prev);
  668. if (!prev)
  669. break;
  670. } while (!of_node_name_eq(prev, "port"));
  671. return prev;
  672. }
  673. EXPORT_SYMBOL(of_graph_get_next_port);
  674. /**
  675. * of_graph_get_next_port_endpoint() - get next endpoint node in port.
  676. * If it reached to end of the port, it will return NULL.
  677. * @port: pointer to the target port node
  678. * @prev: previous endpoint node, or NULL to get first
  679. *
  680. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  681. * of the passed @prev node is decremented.
  682. */
  683. struct device_node *of_graph_get_next_port_endpoint(const struct device_node *port,
  684. struct device_node *prev)
  685. {
  686. while (1) {
  687. prev = of_get_next_child(port, prev);
  688. if (!prev)
  689. break;
  690. if (WARN(!of_node_name_eq(prev, "endpoint"),
  691. "non endpoint node is used (%pOF)", prev))
  692. continue;
  693. break;
  694. }
  695. return prev;
  696. }
  697. EXPORT_SYMBOL(of_graph_get_next_port_endpoint);
  698. /**
  699. * of_graph_get_next_endpoint() - get next endpoint node
  700. * @parent: pointer to the parent device node
  701. * @prev: previous endpoint node, or NULL to get first
  702. *
  703. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  704. * of the passed @prev node is decremented.
  705. */
  706. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  707. struct device_node *prev)
  708. {
  709. struct device_node *endpoint;
  710. struct device_node *port;
  711. if (!parent)
  712. return NULL;
  713. /*
  714. * Start by locating the port node. If no previous endpoint is specified
  715. * search for the first port node, otherwise get the previous endpoint
  716. * parent port node.
  717. */
  718. if (!prev) {
  719. port = of_graph_get_next_port(parent, NULL);
  720. if (!port) {
  721. pr_debug("graph: no port node found in %pOF\n", parent);
  722. return NULL;
  723. }
  724. } else {
  725. port = of_get_parent(prev);
  726. if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
  727. __func__, prev))
  728. return NULL;
  729. }
  730. while (1) {
  731. /*
  732. * Now that we have a port node, get the next endpoint by
  733. * getting the next child. If the previous endpoint is NULL this
  734. * will return the first child.
  735. */
  736. endpoint = of_graph_get_next_port_endpoint(port, prev);
  737. if (endpoint) {
  738. of_node_put(port);
  739. return endpoint;
  740. }
  741. /* No more endpoints under this port, try the next one. */
  742. prev = NULL;
  743. port = of_graph_get_next_port(parent, port);
  744. if (!port)
  745. return NULL;
  746. }
  747. }
  748. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  749. /**
  750. * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
  751. * @parent: pointer to the parent device node
  752. * @port_reg: identifier (value of reg property) of the parent port node
  753. * @reg: identifier (value of reg property) of the endpoint node
  754. *
  755. * Return: An 'endpoint' node pointer which is identified by reg and at the same
  756. * is the child of a port node identified by port_reg. reg and port_reg are
  757. * ignored when they are -1. Use of_node_put() on the pointer when done.
  758. */
  759. struct device_node *of_graph_get_endpoint_by_regs(
  760. const struct device_node *parent, int port_reg, int reg)
  761. {
  762. struct of_endpoint endpoint;
  763. struct device_node *node = NULL;
  764. for_each_endpoint_of_node(parent, node) {
  765. of_graph_parse_endpoint(node, &endpoint);
  766. if (((port_reg == -1) || (endpoint.port == port_reg)) &&
  767. ((reg == -1) || (endpoint.id == reg)))
  768. return node;
  769. }
  770. return NULL;
  771. }
  772. EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
  773. /**
  774. * of_graph_get_remote_endpoint() - get remote endpoint node
  775. * @node: pointer to a local endpoint device_node
  776. *
  777. * Return: Remote endpoint node associated with remote endpoint node linked
  778. * to @node. Use of_node_put() on it when done.
  779. */
  780. struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
  781. {
  782. /* Get remote endpoint node. */
  783. return of_parse_phandle(node, "remote-endpoint", 0);
  784. }
  785. EXPORT_SYMBOL(of_graph_get_remote_endpoint);
  786. /**
  787. * of_graph_get_port_parent() - get port's parent node
  788. * @node: pointer to a local endpoint device_node
  789. *
  790. * Return: device node associated with endpoint node linked
  791. * to @node. Use of_node_put() on it when done.
  792. */
  793. struct device_node *of_graph_get_port_parent(struct device_node *node)
  794. {
  795. unsigned int depth;
  796. if (!node)
  797. return NULL;
  798. /*
  799. * Preserve usecount for passed in node as of_get_next_parent()
  800. * will do of_node_put() on it.
  801. */
  802. of_node_get(node);
  803. /* Walk 3 levels up only if there is 'ports' node. */
  804. for (depth = 3; depth && node; depth--) {
  805. node = of_get_next_parent(node);
  806. if (depth == 2 && !of_node_name_eq(node, "ports") &&
  807. !of_node_name_eq(node, "in-ports") &&
  808. !of_node_name_eq(node, "out-ports"))
  809. break;
  810. }
  811. return node;
  812. }
  813. EXPORT_SYMBOL(of_graph_get_port_parent);
  814. /**
  815. * of_graph_get_remote_port_parent() - get remote port's parent node
  816. * @node: pointer to a local endpoint device_node
  817. *
  818. * Return: Remote device node associated with remote endpoint node linked
  819. * to @node. Use of_node_put() on it when done.
  820. */
  821. struct device_node *of_graph_get_remote_port_parent(
  822. const struct device_node *node)
  823. {
  824. /* Get remote endpoint node. */
  825. struct device_node *np __free(device_node) =
  826. of_graph_get_remote_endpoint(node);
  827. return of_graph_get_port_parent(np);
  828. }
  829. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  830. /**
  831. * of_graph_get_remote_port() - get remote port node
  832. * @node: pointer to a local endpoint device_node
  833. *
  834. * Return: Remote port node associated with remote endpoint node linked
  835. * to @node. Use of_node_put() on it when done.
  836. */
  837. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  838. {
  839. struct device_node *np;
  840. /* Get remote endpoint node. */
  841. np = of_graph_get_remote_endpoint(node);
  842. if (!np)
  843. return NULL;
  844. return of_get_next_parent(np);
  845. }
  846. EXPORT_SYMBOL(of_graph_get_remote_port);
  847. /**
  848. * of_graph_get_endpoint_count() - get the number of endpoints in a device node
  849. * @np: parent device node containing ports and endpoints
  850. *
  851. * Return: count of endpoint of this device node
  852. */
  853. unsigned int of_graph_get_endpoint_count(const struct device_node *np)
  854. {
  855. struct device_node *endpoint;
  856. unsigned int num = 0;
  857. for_each_endpoint_of_node(np, endpoint)
  858. num++;
  859. return num;
  860. }
  861. EXPORT_SYMBOL(of_graph_get_endpoint_count);
  862. /**
  863. * of_graph_get_port_count() - get the number of port in a device or ports node
  864. * @np: pointer to the device or ports node
  865. *
  866. * Return: count of port of this device or ports node
  867. */
  868. unsigned int of_graph_get_port_count(struct device_node *np)
  869. {
  870. unsigned int num = 0;
  871. for_each_of_graph_port(np, port)
  872. num++;
  873. return num;
  874. }
  875. EXPORT_SYMBOL(of_graph_get_port_count);
  876. /**
  877. * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
  878. * @node: pointer to parent device_node containing graph port/endpoint
  879. * @port: identifier (value of reg property) of the parent port node
  880. * @endpoint: identifier (value of reg property) of the endpoint node
  881. *
  882. * Return: Remote device node associated with remote endpoint node linked
  883. * to @node. Use of_node_put() on it when done.
  884. */
  885. struct device_node *of_graph_get_remote_node(const struct device_node *node,
  886. u32 port, u32 endpoint)
  887. {
  888. struct device_node *endpoint_node, *remote;
  889. endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
  890. if (!endpoint_node) {
  891. pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
  892. port, endpoint, node);
  893. return NULL;
  894. }
  895. remote = of_graph_get_remote_port_parent(endpoint_node);
  896. of_node_put(endpoint_node);
  897. if (!remote) {
  898. pr_debug("no valid remote node\n");
  899. return NULL;
  900. }
  901. if (!of_device_is_available(remote)) {
  902. pr_debug("not available for remote node\n");
  903. of_node_put(remote);
  904. return NULL;
  905. }
  906. return remote;
  907. }
  908. EXPORT_SYMBOL(of_graph_get_remote_node);
  909. static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
  910. {
  911. return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
  912. }
  913. static void of_fwnode_put(struct fwnode_handle *fwnode)
  914. {
  915. of_node_put(to_of_node(fwnode));
  916. }
  917. static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
  918. {
  919. return of_device_is_available(to_of_node(fwnode));
  920. }
  921. static bool of_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
  922. {
  923. return true;
  924. }
  925. static enum dev_dma_attr
  926. of_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
  927. {
  928. if (of_dma_is_coherent(to_of_node(fwnode)))
  929. return DEV_DMA_COHERENT;
  930. else
  931. return DEV_DMA_NON_COHERENT;
  932. }
  933. static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
  934. const char *propname)
  935. {
  936. return of_property_present(to_of_node(fwnode), propname);
  937. }
  938. static bool of_fwnode_property_read_bool(const struct fwnode_handle *fwnode,
  939. const char *propname)
  940. {
  941. return of_property_read_bool(to_of_node(fwnode), propname);
  942. }
  943. static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
  944. const char *propname,
  945. unsigned int elem_size, void *val,
  946. size_t nval)
  947. {
  948. const struct device_node *node = to_of_node(fwnode);
  949. if (!val)
  950. return of_property_count_elems_of_size(node, propname,
  951. elem_size);
  952. switch (elem_size) {
  953. case sizeof(u8):
  954. return of_property_read_u8_array(node, propname, val, nval);
  955. case sizeof(u16):
  956. return of_property_read_u16_array(node, propname, val, nval);
  957. case sizeof(u32):
  958. return of_property_read_u32_array(node, propname, val, nval);
  959. case sizeof(u64):
  960. return of_property_read_u64_array(node, propname, val, nval);
  961. }
  962. return -ENXIO;
  963. }
  964. static int
  965. of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  966. const char *propname, const char **val,
  967. size_t nval)
  968. {
  969. const struct device_node *node = to_of_node(fwnode);
  970. return val ?
  971. of_property_read_string_array(node, propname, val, nval) :
  972. of_property_count_strings(node, propname);
  973. }
  974. static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
  975. {
  976. return kbasename(to_of_node(fwnode)->full_name);
  977. }
  978. static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
  979. {
  980. /* Root needs no prefix here (its name is "/"). */
  981. if (!to_of_node(fwnode)->parent)
  982. return "";
  983. return "/";
  984. }
  985. static struct fwnode_handle *
  986. of_fwnode_get_parent(const struct fwnode_handle *fwnode)
  987. {
  988. return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
  989. }
  990. static struct fwnode_handle *
  991. of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
  992. struct fwnode_handle *child)
  993. {
  994. return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
  995. to_of_node(child)));
  996. }
  997. static struct fwnode_handle *
  998. of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
  999. const char *childname)
  1000. {
  1001. const struct device_node *node = to_of_node(fwnode);
  1002. struct device_node *child;
  1003. for_each_available_child_of_node(node, child)
  1004. if (of_node_name_eq(child, childname))
  1005. return of_fwnode_handle(child);
  1006. return NULL;
  1007. }
  1008. static int
  1009. of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
  1010. const char *prop, const char *nargs_prop,
  1011. unsigned int nargs, unsigned int index,
  1012. struct fwnode_reference_args *args)
  1013. {
  1014. struct of_phandle_args of_args;
  1015. unsigned int i;
  1016. int ret;
  1017. if (nargs_prop)
  1018. ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
  1019. nargs_prop, index, &of_args);
  1020. else
  1021. ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
  1022. nargs, index, &of_args);
  1023. if (ret < 0)
  1024. return ret;
  1025. if (!args) {
  1026. of_node_put(of_args.np);
  1027. return 0;
  1028. }
  1029. args->nargs = of_args.args_count;
  1030. args->fwnode = of_fwnode_handle(of_args.np);
  1031. for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
  1032. args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
  1033. return 0;
  1034. }
  1035. static struct fwnode_handle *
  1036. of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
  1037. struct fwnode_handle *prev)
  1038. {
  1039. return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
  1040. to_of_node(prev)));
  1041. }
  1042. static struct fwnode_handle *
  1043. of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
  1044. {
  1045. return of_fwnode_handle(
  1046. of_graph_get_remote_endpoint(to_of_node(fwnode)));
  1047. }
  1048. static struct fwnode_handle *
  1049. of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
  1050. {
  1051. struct device_node *np;
  1052. /* Get the parent of the port */
  1053. np = of_get_parent(to_of_node(fwnode));
  1054. if (!np)
  1055. return NULL;
  1056. /* Is this the "ports" node? If not, it's the port parent. */
  1057. if (!of_node_name_eq(np, "ports"))
  1058. return of_fwnode_handle(np);
  1059. return of_fwnode_handle(of_get_next_parent(np));
  1060. }
  1061. static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  1062. struct fwnode_endpoint *endpoint)
  1063. {
  1064. const struct device_node *node = to_of_node(fwnode);
  1065. struct device_node *port_node __free(device_node) = of_get_parent(node);
  1066. endpoint->local_fwnode = fwnode;
  1067. of_property_read_u32(port_node, "reg", &endpoint->port);
  1068. of_property_read_u32(node, "reg", &endpoint->id);
  1069. return 0;
  1070. }
  1071. static const void *
  1072. of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
  1073. const struct device *dev)
  1074. {
  1075. return of_device_get_match_data(dev);
  1076. }
  1077. static void of_link_to_phandle(struct device_node *con_np,
  1078. struct device_node *sup_np,
  1079. u8 flags)
  1080. {
  1081. struct device_node *tmp_np __free(device_node) = of_node_get(sup_np);
  1082. /* Check that sup_np and its ancestors are available. */
  1083. while (tmp_np) {
  1084. if (of_fwnode_handle(tmp_np)->dev)
  1085. break;
  1086. if (!of_device_is_available(tmp_np))
  1087. return;
  1088. tmp_np = of_get_next_parent(tmp_np);
  1089. }
  1090. fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np), flags);
  1091. }
  1092. /**
  1093. * parse_prop_cells - Property parsing function for suppliers
  1094. *
  1095. * @np: Pointer to device tree node containing a list
  1096. * @prop_name: Name of property to be parsed. Expected to hold phandle values
  1097. * @index: For properties holding a list of phandles, this is the index
  1098. * into the list.
  1099. * @list_name: Property name that is known to contain list of phandle(s) to
  1100. * supplier(s)
  1101. * @cells_name: property name that specifies phandles' arguments count
  1102. *
  1103. * This is a helper function to parse properties that have a known fixed name
  1104. * and are a list of phandles and phandle arguments.
  1105. *
  1106. * Returns:
  1107. * - phandle node pointer with refcount incremented. Caller must of_node_put()
  1108. * on it when done.
  1109. * - NULL if no phandle found at index
  1110. */
  1111. static struct device_node *parse_prop_cells(struct device_node *np,
  1112. const char *prop_name, int index,
  1113. const char *list_name,
  1114. const char *cells_name)
  1115. {
  1116. struct of_phandle_args sup_args;
  1117. if (strcmp(prop_name, list_name))
  1118. return NULL;
  1119. if (__of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
  1120. &sup_args))
  1121. return NULL;
  1122. return sup_args.np;
  1123. }
  1124. #define DEFINE_SIMPLE_PROP(fname, name, cells) \
  1125. static struct device_node *parse_##fname(struct device_node *np, \
  1126. const char *prop_name, int index) \
  1127. { \
  1128. return parse_prop_cells(np, prop_name, index, name, cells); \
  1129. }
  1130. /**
  1131. * parse_suffix_prop_cells - Suffix property parsing function for suppliers
  1132. *
  1133. * @np: Pointer to device tree node containing a list
  1134. * @prop_name: Name of property to be parsed. Expected to hold phandle values
  1135. * @index: For properties holding a list of phandles, this is the index
  1136. * into the list.
  1137. * @suffix: Property suffix that is known to contain list of phandle(s) to
  1138. * supplier(s)
  1139. * @cells_name: property name that specifies phandles' arguments count
  1140. *
  1141. * This is a helper function to parse properties that have a known fixed suffix
  1142. * and are a list of phandles and phandle arguments.
  1143. *
  1144. * Returns:
  1145. * - phandle node pointer with refcount incremented. Caller must of_node_put()
  1146. * on it when done.
  1147. * - NULL if no phandle found at index
  1148. */
  1149. static struct device_node *parse_suffix_prop_cells(struct device_node *np,
  1150. const char *prop_name, int index,
  1151. const char *suffix,
  1152. const char *cells_name)
  1153. {
  1154. struct of_phandle_args sup_args;
  1155. if (!strends(prop_name, suffix))
  1156. return NULL;
  1157. if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
  1158. &sup_args))
  1159. return NULL;
  1160. return sup_args.np;
  1161. }
  1162. #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
  1163. static struct device_node *parse_##fname(struct device_node *np, \
  1164. const char *prop_name, int index) \
  1165. { \
  1166. return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
  1167. }
  1168. /**
  1169. * struct supplier_bindings - Property parsing functions for suppliers
  1170. *
  1171. * @parse_prop: function name
  1172. * parse_prop() finds the node corresponding to a supplier phandle
  1173. * parse_prop.np: Pointer to device node holding supplier phandle property
  1174. * parse_prop.prop_name: Name of property holding a phandle value
  1175. * parse_prop.index: For properties holding a list of phandles, this is the
  1176. * index into the list
  1177. * @get_con_dev: If the consumer node containing the property is never converted
  1178. * to a struct device, implement this ops so fw_devlink can use it
  1179. * to find the true consumer.
  1180. * @optional: Describes whether a supplier is mandatory or not
  1181. * @fwlink_flags: Optional fwnode link flags to use when creating a fwnode link
  1182. * for this property.
  1183. *
  1184. * Returns:
  1185. * parse_prop() return values are
  1186. * - phandle node pointer with refcount incremented. Caller must of_node_put()
  1187. * on it when done.
  1188. * - NULL if no phandle found at index
  1189. */
  1190. struct supplier_bindings {
  1191. struct device_node *(*parse_prop)(struct device_node *np,
  1192. const char *prop_name, int index);
  1193. struct device_node *(*get_con_dev)(struct device_node *np);
  1194. bool optional;
  1195. u8 fwlink_flags;
  1196. };
  1197. DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
  1198. DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
  1199. DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
  1200. DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
  1201. DEFINE_SIMPLE_PROP(io_channels, "io-channels", "#io-channel-cells")
  1202. DEFINE_SIMPLE_PROP(io_backends, "io-backends", "#io-backend-cells")
  1203. DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
  1204. DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
  1205. DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
  1206. DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
  1207. DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", "#nvmem-cell-cells")
  1208. DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
  1209. DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
  1210. DEFINE_SIMPLE_PROP(pwms, "pwms", "#pwm-cells")
  1211. DEFINE_SIMPLE_PROP(resets, "resets", "#reset-cells")
  1212. DEFINE_SIMPLE_PROP(leds, "leds", NULL)
  1213. DEFINE_SIMPLE_PROP(backlight, "backlight", NULL)
  1214. DEFINE_SIMPLE_PROP(panel, "panel", NULL)
  1215. DEFINE_SIMPLE_PROP(msi_parent, "msi-parent", "#msi-cells")
  1216. DEFINE_SIMPLE_PROP(post_init_providers, "post-init-providers", NULL)
  1217. DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller-cells")
  1218. DEFINE_SIMPLE_PROP(pses, "pses", "#pse-cells")
  1219. DEFINE_SIMPLE_PROP(power_supplies, "power-supplies", NULL)
  1220. DEFINE_SIMPLE_PROP(mmc_pwrseq, "mmc-pwrseq", NULL)
  1221. DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
  1222. DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
  1223. static struct device_node *parse_pinctrl_n(struct device_node *np,
  1224. const char *prop_name, int index)
  1225. {
  1226. if (!strstarts(prop_name, "pinctrl-"))
  1227. return NULL;
  1228. if (!isdigit(prop_name[strlen("pinctrl-")]))
  1229. return NULL;
  1230. return of_parse_phandle(np, prop_name, index);
  1231. }
  1232. static struct device_node *parse_gpios(struct device_node *np,
  1233. const char *prop_name, int index)
  1234. {
  1235. if (strends(prop_name, ",nr-gpios"))
  1236. return NULL;
  1237. return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
  1238. "#gpio-cells");
  1239. }
  1240. static struct device_node *parse_iommu_maps(struct device_node *np,
  1241. const char *prop_name, int index)
  1242. {
  1243. if (strcmp(prop_name, "iommu-map"))
  1244. return NULL;
  1245. return of_parse_phandle(np, prop_name, (index * 4) + 1);
  1246. }
  1247. static struct device_node *parse_gpio_compat(struct device_node *np,
  1248. const char *prop_name, int index)
  1249. {
  1250. struct of_phandle_args sup_args;
  1251. if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
  1252. return NULL;
  1253. /*
  1254. * Ignore node with gpio-hog property since its gpios are all provided
  1255. * by its parent.
  1256. */
  1257. if (of_property_read_bool(np, "gpio-hog"))
  1258. return NULL;
  1259. if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
  1260. &sup_args))
  1261. return NULL;
  1262. return sup_args.np;
  1263. }
  1264. static struct device_node *parse_interrupts(struct device_node *np,
  1265. const char *prop_name, int index)
  1266. {
  1267. struct of_phandle_args sup_args;
  1268. if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
  1269. return NULL;
  1270. if (strcmp(prop_name, "interrupts") &&
  1271. strcmp(prop_name, "interrupts-extended"))
  1272. return NULL;
  1273. return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
  1274. }
  1275. static struct device_node *parse_interrupt_map(struct device_node *np,
  1276. const char *prop_name, int index)
  1277. {
  1278. const __be32 *imap, *imap_end;
  1279. struct of_phandle_args sup_args;
  1280. u32 addrcells, intcells;
  1281. int imaplen;
  1282. if (!IS_ENABLED(CONFIG_OF_IRQ))
  1283. return NULL;
  1284. if (strcmp(prop_name, "interrupt-map"))
  1285. return NULL;
  1286. if (of_property_read_u32(np, "#interrupt-cells", &intcells))
  1287. return NULL;
  1288. addrcells = of_bus_n_addr_cells(np);
  1289. imap = of_get_property(np, "interrupt-map", &imaplen);
  1290. if (!imap)
  1291. return NULL;
  1292. imaplen /= sizeof(*imap);
  1293. imap_end = imap + imaplen;
  1294. for (int i = 0; imap + addrcells + intcells + 1 < imap_end; i++) {
  1295. imap += addrcells + intcells;
  1296. imap = of_irq_parse_imap_parent(imap, imap_end - imap, &sup_args);
  1297. if (!imap)
  1298. return NULL;
  1299. if (i == index)
  1300. return sup_args.np;
  1301. of_node_put(sup_args.np);
  1302. }
  1303. return NULL;
  1304. }
  1305. static struct device_node *parse_remote_endpoint(struct device_node *np,
  1306. const char *prop_name,
  1307. int index)
  1308. {
  1309. /* Return NULL for index > 0 to signify end of remote-endpoints. */
  1310. if (index > 0 || strcmp(prop_name, "remote-endpoint"))
  1311. return NULL;
  1312. return of_graph_get_remote_port_parent(np);
  1313. }
  1314. static const struct supplier_bindings of_supplier_bindings[] = {
  1315. { .parse_prop = parse_clocks, },
  1316. { .parse_prop = parse_interconnects, },
  1317. { .parse_prop = parse_iommus, .optional = true, },
  1318. { .parse_prop = parse_iommu_maps, .optional = true, },
  1319. { .parse_prop = parse_mboxes, },
  1320. { .parse_prop = parse_io_channels, },
  1321. { .parse_prop = parse_io_backends, },
  1322. { .parse_prop = parse_dmas, .optional = true, },
  1323. { .parse_prop = parse_power_domains, },
  1324. { .parse_prop = parse_hwlocks, },
  1325. { .parse_prop = parse_extcon, },
  1326. { .parse_prop = parse_nvmem_cells, },
  1327. { .parse_prop = parse_phys, },
  1328. { .parse_prop = parse_wakeup_parent, },
  1329. { .parse_prop = parse_pinctrl_n, },
  1330. {
  1331. .parse_prop = parse_remote_endpoint,
  1332. .get_con_dev = of_graph_get_port_parent,
  1333. },
  1334. { .parse_prop = parse_pwms, },
  1335. { .parse_prop = parse_resets, },
  1336. { .parse_prop = parse_leds, },
  1337. { .parse_prop = parse_backlight, },
  1338. { .parse_prop = parse_panel, },
  1339. { .parse_prop = parse_msi_parent, },
  1340. { .parse_prop = parse_pses, },
  1341. { .parse_prop = parse_power_supplies, },
  1342. { .parse_prop = parse_mmc_pwrseq, },
  1343. { .parse_prop = parse_gpio_compat, },
  1344. { .parse_prop = parse_interrupts, },
  1345. { .parse_prop = parse_interrupt_map, },
  1346. { .parse_prop = parse_access_controllers, },
  1347. { .parse_prop = parse_regulators, },
  1348. { .parse_prop = parse_gpio, },
  1349. { .parse_prop = parse_gpios, },
  1350. {
  1351. .parse_prop = parse_post_init_providers,
  1352. .fwlink_flags = FWLINK_FLAG_IGNORE,
  1353. },
  1354. {}
  1355. };
  1356. /**
  1357. * of_link_property - Create device links to suppliers listed in a property
  1358. * @con_np: The consumer device tree node which contains the property
  1359. * @prop_name: Name of property to be parsed
  1360. *
  1361. * This function checks if the property @prop_name that is present in the
  1362. * @con_np device tree node is one of the known common device tree bindings
  1363. * that list phandles to suppliers. If @prop_name isn't one, this function
  1364. * doesn't do anything.
  1365. *
  1366. * If @prop_name is one, this function attempts to create fwnode links from the
  1367. * consumer device tree node @con_np to all the suppliers device tree nodes
  1368. * listed in @prop_name.
  1369. *
  1370. * Any failed attempt to create a fwnode link will NOT result in an immediate
  1371. * return. of_link_property() must create links to all the available supplier
  1372. * device tree nodes even when attempts to create a link to one or more
  1373. * suppliers fail.
  1374. */
  1375. static int of_link_property(struct device_node *con_np, const char *prop_name)
  1376. {
  1377. struct device_node *phandle;
  1378. const struct supplier_bindings *s = of_supplier_bindings;
  1379. unsigned int i = 0;
  1380. bool matched = false;
  1381. /* Do not stop at first failed link, link all available suppliers. */
  1382. while (!matched && s->parse_prop) {
  1383. if (s->optional && !fw_devlink_is_strict()) {
  1384. s++;
  1385. continue;
  1386. }
  1387. while ((phandle = s->parse_prop(con_np, prop_name, i))) {
  1388. struct device_node *con_dev_np __free(device_node) =
  1389. s->get_con_dev ? s->get_con_dev(con_np) : of_node_get(con_np);
  1390. matched = true;
  1391. i++;
  1392. of_link_to_phandle(con_dev_np, phandle, s->fwlink_flags);
  1393. of_node_put(phandle);
  1394. }
  1395. s++;
  1396. }
  1397. return 0;
  1398. }
  1399. static void __iomem *of_fwnode_iomap(struct fwnode_handle *fwnode, int index)
  1400. {
  1401. #ifdef CONFIG_OF_ADDRESS
  1402. return of_iomap(to_of_node(fwnode), index);
  1403. #else
  1404. return NULL;
  1405. #endif
  1406. }
  1407. static int of_fwnode_irq_get(const struct fwnode_handle *fwnode,
  1408. unsigned int index)
  1409. {
  1410. return of_irq_get(to_of_node(fwnode), index);
  1411. }
  1412. static int of_fwnode_add_links(struct fwnode_handle *fwnode)
  1413. {
  1414. const struct property *p;
  1415. struct device_node *con_np = to_of_node(fwnode);
  1416. if (IS_ENABLED(CONFIG_X86))
  1417. return 0;
  1418. if (!con_np)
  1419. return -EINVAL;
  1420. for_each_property_of_node(con_np, p)
  1421. of_link_property(con_np, p->name);
  1422. return 0;
  1423. }
  1424. const struct fwnode_operations of_fwnode_ops = {
  1425. .get = of_fwnode_get,
  1426. .put = of_fwnode_put,
  1427. .device_is_available = of_fwnode_device_is_available,
  1428. .device_get_match_data = of_fwnode_device_get_match_data,
  1429. .device_dma_supported = of_fwnode_device_dma_supported,
  1430. .device_get_dma_attr = of_fwnode_device_get_dma_attr,
  1431. .property_present = of_fwnode_property_present,
  1432. .property_read_bool = of_fwnode_property_read_bool,
  1433. .property_read_int_array = of_fwnode_property_read_int_array,
  1434. .property_read_string_array = of_fwnode_property_read_string_array,
  1435. .get_name = of_fwnode_get_name,
  1436. .get_name_prefix = of_fwnode_get_name_prefix,
  1437. .get_parent = of_fwnode_get_parent,
  1438. .get_next_child_node = of_fwnode_get_next_child_node,
  1439. .get_named_child_node = of_fwnode_get_named_child_node,
  1440. .get_reference_args = of_fwnode_get_reference_args,
  1441. .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
  1442. .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
  1443. .graph_get_port_parent = of_fwnode_graph_get_port_parent,
  1444. .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
  1445. .iomap = of_fwnode_iomap,
  1446. .irq_get = of_fwnode_irq_get,
  1447. .add_links = of_fwnode_add_links,
  1448. };
  1449. EXPORT_SYMBOL_GPL(of_fwnode_ops);