property.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_graph.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/string.h>
  27. #include <linux/moduleparam.h>
  28. #include "of_private.h"
  29. /**
  30. * of_graph_is_present() - check graph's presence
  31. * @node: pointer to device_node containing graph port
  32. *
  33. * Return: True if @node has a port or ports (with a port) sub-node,
  34. * false otherwise.
  35. */
  36. bool of_graph_is_present(const struct device_node *node)
  37. {
  38. struct device_node *ports, *port;
  39. ports = of_get_child_by_name(node, "ports");
  40. if (ports)
  41. node = ports;
  42. port = of_get_child_by_name(node, "port");
  43. of_node_put(ports);
  44. of_node_put(port);
  45. return !!port;
  46. }
  47. EXPORT_SYMBOL(of_graph_is_present);
  48. /**
  49. * of_property_count_elems_of_size - Count the number of elements in a property
  50. *
  51. * @np: device node from which the property value is to be read.
  52. * @propname: name of the property to be searched.
  53. * @elem_size: size of the individual element
  54. *
  55. * Search for a property in a device node and count the number of elements of
  56. * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
  57. * property does not exist or its length does not match a multiple of elem_size
  58. * and -ENODATA if the property does not have a value.
  59. */
  60. int of_property_count_elems_of_size(const struct device_node *np,
  61. const char *propname, int elem_size)
  62. {
  63. struct property *prop = of_find_property(np, propname, NULL);
  64. if (!prop)
  65. return -EINVAL;
  66. if (!prop->value)
  67. return -ENODATA;
  68. if (prop->length % elem_size != 0) {
  69. pr_err("size of %s in node %pOF is not a multiple of %d\n",
  70. propname, np, elem_size);
  71. return -EINVAL;
  72. }
  73. return prop->length / elem_size;
  74. }
  75. EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
  76. /**
  77. * of_find_property_value_of_size
  78. *
  79. * @np: device node from which the property value is to be read.
  80. * @propname: name of the property to be searched.
  81. * @min: minimum allowed length of property value
  82. * @max: maximum allowed length of property value (0 means unlimited)
  83. * @len: if !=NULL, actual length is written to here
  84. *
  85. * Search for a property in a device node and valid the requested size.
  86. * Returns the property value on success, -EINVAL if the property does not
  87. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  88. * property data is too small or too large.
  89. *
  90. */
  91. static void *of_find_property_value_of_size(const struct device_node *np,
  92. const char *propname, u32 min, u32 max, size_t *len)
  93. {
  94. struct property *prop = of_find_property(np, propname, NULL);
  95. if (!prop)
  96. return ERR_PTR(-EINVAL);
  97. if (!prop->value)
  98. return ERR_PTR(-ENODATA);
  99. if (prop->length < min)
  100. return ERR_PTR(-EOVERFLOW);
  101. if (max && prop->length > max)
  102. return ERR_PTR(-EOVERFLOW);
  103. if (len)
  104. *len = prop->length;
  105. return prop->value;
  106. }
  107. /**
  108. * of_property_read_u32_index - Find and read a u32 from a multi-value property.
  109. *
  110. * @np: device node from which the property value is to be read.
  111. * @propname: name of the property to be searched.
  112. * @index: index of the u32 in the list of values
  113. * @out_value: pointer to return value, modified only if no error.
  114. *
  115. * Search for a property in a device node and read nth 32-bit value from
  116. * it. Returns 0 on success, -EINVAL if the property does not exist,
  117. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  118. * property data isn't large enough.
  119. *
  120. * The out_value is modified only if a valid u32 value can be decoded.
  121. */
  122. int of_property_read_u32_index(const struct device_node *np,
  123. const char *propname,
  124. u32 index, u32 *out_value)
  125. {
  126. const u32 *val = of_find_property_value_of_size(np, propname,
  127. ((index + 1) * sizeof(*out_value)),
  128. 0,
  129. NULL);
  130. if (IS_ERR(val))
  131. return PTR_ERR(val);
  132. *out_value = be32_to_cpup(((__be32 *)val) + index);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL_GPL(of_property_read_u32_index);
  136. /**
  137. * of_property_read_u64_index - Find and read a u64 from a multi-value property.
  138. *
  139. * @np: device node from which the property value is to be read.
  140. * @propname: name of the property to be searched.
  141. * @index: index of the u64 in the list of values
  142. * @out_value: pointer to return value, modified only if no error.
  143. *
  144. * Search for a property in a device node and read nth 64-bit value from
  145. * it. Returns 0 on success, -EINVAL if the property does not exist,
  146. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  147. * property data isn't large enough.
  148. *
  149. * The out_value is modified only if a valid u64 value can be decoded.
  150. */
  151. int of_property_read_u64_index(const struct device_node *np,
  152. const char *propname,
  153. u32 index, u64 *out_value)
  154. {
  155. const u64 *val = of_find_property_value_of_size(np, propname,
  156. ((index + 1) * sizeof(*out_value)),
  157. 0, NULL);
  158. if (IS_ERR(val))
  159. return PTR_ERR(val);
  160. *out_value = be64_to_cpup(((__be64 *)val) + index);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(of_property_read_u64_index);
  164. /**
  165. * of_property_read_variable_u8_array - Find and read an array of u8 from a
  166. * property, with bounds on the minimum and maximum array size.
  167. *
  168. * @np: device node from which the property value is to be read.
  169. * @propname: name of the property to be searched.
  170. * @out_values: pointer to found values.
  171. * @sz_min: minimum number of array elements to read
  172. * @sz_max: maximum number of array elements to read, if zero there is no
  173. * upper limit on the number of elements in the dts entry but only
  174. * sz_min will be read.
  175. *
  176. * Search for a property in a device node and read 8-bit value(s) from
  177. * it. Returns number of elements read on success, -EINVAL if the property
  178. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  179. * if the property data is smaller than sz_min or longer than sz_max.
  180. *
  181. * dts entry of array should be like:
  182. * property = /bits/ 8 <0x50 0x60 0x70>;
  183. *
  184. * The out_values is modified only if a valid u8 value can be decoded.
  185. */
  186. int of_property_read_variable_u8_array(const struct device_node *np,
  187. const char *propname, u8 *out_values,
  188. size_t sz_min, size_t sz_max)
  189. {
  190. size_t sz, count;
  191. const u8 *val = of_find_property_value_of_size(np, propname,
  192. (sz_min * sizeof(*out_values)),
  193. (sz_max * sizeof(*out_values)),
  194. &sz);
  195. if (IS_ERR(val))
  196. return PTR_ERR(val);
  197. if (!sz_max)
  198. sz = sz_min;
  199. else
  200. sz /= sizeof(*out_values);
  201. count = sz;
  202. while (count--)
  203. *out_values++ = *val++;
  204. return sz;
  205. }
  206. EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
  207. /**
  208. * of_property_read_variable_u16_array - Find and read an array of u16 from a
  209. * property, with bounds on the minimum and maximum array size.
  210. *
  211. * @np: device node from which the property value is to be read.
  212. * @propname: name of the property to be searched.
  213. * @out_values: pointer to found values.
  214. * @sz_min: minimum number of array elements to read
  215. * @sz_max: maximum number of array elements to read, if zero there is no
  216. * upper limit on the number of elements in the dts entry but only
  217. * sz_min will be read.
  218. *
  219. * Search for a property in a device node and read 16-bit value(s) from
  220. * it. Returns number of elements read on success, -EINVAL if the property
  221. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  222. * if the property data is smaller than sz_min or longer than sz_max.
  223. *
  224. * dts entry of array should be like:
  225. * property = /bits/ 16 <0x5000 0x6000 0x7000>;
  226. *
  227. * The out_values is modified only if a valid u16 value can be decoded.
  228. */
  229. int of_property_read_variable_u16_array(const struct device_node *np,
  230. const char *propname, u16 *out_values,
  231. size_t sz_min, size_t sz_max)
  232. {
  233. size_t sz, count;
  234. const __be16 *val = of_find_property_value_of_size(np, propname,
  235. (sz_min * sizeof(*out_values)),
  236. (sz_max * sizeof(*out_values)),
  237. &sz);
  238. if (IS_ERR(val))
  239. return PTR_ERR(val);
  240. if (!sz_max)
  241. sz = sz_min;
  242. else
  243. sz /= sizeof(*out_values);
  244. count = sz;
  245. while (count--)
  246. *out_values++ = be16_to_cpup(val++);
  247. return sz;
  248. }
  249. EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
  250. /**
  251. * of_property_read_variable_u32_array - Find and read an array of 32 bit
  252. * integers from a property, with bounds on the minimum and maximum array size.
  253. *
  254. * @np: device node from which the property value is to be read.
  255. * @propname: name of the property to be searched.
  256. * @out_values: pointer to return found values.
  257. * @sz_min: minimum number of array elements to read
  258. * @sz_max: maximum number of array elements to read, if zero there is no
  259. * upper limit on the number of elements in the dts entry but only
  260. * sz_min will be read.
  261. *
  262. * Search for a property in a device node and read 32-bit value(s) from
  263. * it. Returns number of elements read on success, -EINVAL if the property
  264. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  265. * if the property data is smaller than sz_min or longer than sz_max.
  266. *
  267. * The out_values is modified only if a valid u32 value can be decoded.
  268. */
  269. int of_property_read_variable_u32_array(const struct device_node *np,
  270. const char *propname, u32 *out_values,
  271. size_t sz_min, size_t sz_max)
  272. {
  273. size_t sz, count;
  274. const __be32 *val = of_find_property_value_of_size(np, propname,
  275. (sz_min * sizeof(*out_values)),
  276. (sz_max * sizeof(*out_values)),
  277. &sz);
  278. if (IS_ERR(val))
  279. return PTR_ERR(val);
  280. if (!sz_max)
  281. sz = sz_min;
  282. else
  283. sz /= sizeof(*out_values);
  284. count = sz;
  285. while (count--)
  286. *out_values++ = be32_to_cpup(val++);
  287. return sz;
  288. }
  289. EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
  290. /**
  291. * of_property_read_u64 - Find and read a 64 bit integer from a property
  292. * @np: device node from which the property value is to be read.
  293. * @propname: name of the property to be searched.
  294. * @out_value: pointer to return value, modified only if return value is 0.
  295. *
  296. * Search for a property in a device node and read a 64-bit value from
  297. * it. Returns 0 on success, -EINVAL if the property does not exist,
  298. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  299. * property data isn't large enough.
  300. *
  301. * The out_value is modified only if a valid u64 value can be decoded.
  302. */
  303. int of_property_read_u64(const struct device_node *np, const char *propname,
  304. u64 *out_value)
  305. {
  306. const __be32 *val = of_find_property_value_of_size(np, propname,
  307. sizeof(*out_value),
  308. 0,
  309. NULL);
  310. if (IS_ERR(val))
  311. return PTR_ERR(val);
  312. *out_value = of_read_number(val, 2);
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(of_property_read_u64);
  316. /**
  317. * of_property_read_variable_u64_array - Find and read an array of 64 bit
  318. * integers from a property, with bounds on the minimum and maximum array size.
  319. *
  320. * @np: device node from which the property value is to be read.
  321. * @propname: name of the property to be searched.
  322. * @out_values: pointer to found values.
  323. * @sz_min: minimum number of array elements to read
  324. * @sz_max: maximum number of array elements to read, if zero there is no
  325. * upper limit on the number of elements in the dts entry but only
  326. * sz_min will be read.
  327. *
  328. * Search for a property in a device node and read 64-bit value(s) from
  329. * it. Returns number of elements read on success, -EINVAL if the property
  330. * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
  331. * if the property data is smaller than sz_min or longer than sz_max.
  332. *
  333. * The out_values is modified only if a valid u64 value can be decoded.
  334. */
  335. int of_property_read_variable_u64_array(const struct device_node *np,
  336. const char *propname, u64 *out_values,
  337. size_t sz_min, size_t sz_max)
  338. {
  339. size_t sz, count;
  340. const __be32 *val = of_find_property_value_of_size(np, propname,
  341. (sz_min * sizeof(*out_values)),
  342. (sz_max * sizeof(*out_values)),
  343. &sz);
  344. if (IS_ERR(val))
  345. return PTR_ERR(val);
  346. if (!sz_max)
  347. sz = sz_min;
  348. else
  349. sz /= sizeof(*out_values);
  350. count = sz;
  351. while (count--) {
  352. *out_values++ = of_read_number(val, 2);
  353. val += 2;
  354. }
  355. return sz;
  356. }
  357. EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
  358. /**
  359. * of_property_read_string - Find and read a string from a property
  360. * @np: device node from which the property value is to be read.
  361. * @propname: name of the property to be searched.
  362. * @out_string: pointer to null terminated return string, modified only if
  363. * return value is 0.
  364. *
  365. * Search for a property in a device tree node and retrieve a null
  366. * terminated string value (pointer to data, not a copy). Returns 0 on
  367. * success, -EINVAL if the property does not exist, -ENODATA if property
  368. * does not have a value, and -EILSEQ if the string is not null-terminated
  369. * within the length of the property data.
  370. *
  371. * The out_string pointer is modified only if a valid string can be decoded.
  372. */
  373. int of_property_read_string(const struct device_node *np, const char *propname,
  374. const char **out_string)
  375. {
  376. const struct property *prop = of_find_property(np, propname, NULL);
  377. if (!prop)
  378. return -EINVAL;
  379. if (!prop->value)
  380. return -ENODATA;
  381. if (strnlen(prop->value, prop->length) >= prop->length)
  382. return -EILSEQ;
  383. *out_string = prop->value;
  384. return 0;
  385. }
  386. EXPORT_SYMBOL_GPL(of_property_read_string);
  387. /**
  388. * of_property_match_string() - Find string in a list and return index
  389. * @np: pointer to node containing string list property
  390. * @propname: string list property name
  391. * @string: pointer to string to search for in string list
  392. *
  393. * This function searches a string list property and returns the index
  394. * of a specific string value.
  395. */
  396. int of_property_match_string(const struct device_node *np, const char *propname,
  397. const char *string)
  398. {
  399. const struct property *prop = of_find_property(np, propname, NULL);
  400. size_t l;
  401. int i;
  402. const char *p, *end;
  403. if (!prop)
  404. return -EINVAL;
  405. if (!prop->value)
  406. return -ENODATA;
  407. p = prop->value;
  408. end = p + prop->length;
  409. for (i = 0; p < end; i++, p += l) {
  410. l = strnlen(p, end - p) + 1;
  411. if (p + l > end)
  412. return -EILSEQ;
  413. pr_debug("comparing %s with %s\n", string, p);
  414. if (strcmp(string, p) == 0)
  415. return i; /* Found it; return index */
  416. }
  417. return -ENODATA;
  418. }
  419. EXPORT_SYMBOL_GPL(of_property_match_string);
  420. /**
  421. * of_property_read_string_helper() - Utility helper for parsing string properties
  422. * @np: device node from which the property value is to be read.
  423. * @propname: name of the property to be searched.
  424. * @out_strs: output array of string pointers.
  425. * @sz: number of array elements to read.
  426. * @skip: Number of strings to skip over at beginning of list.
  427. *
  428. * Don't call this function directly. It is a utility helper for the
  429. * of_property_read_string*() family of functions.
  430. */
  431. int of_property_read_string_helper(const struct device_node *np,
  432. const char *propname, const char **out_strs,
  433. size_t sz, int skip)
  434. {
  435. const struct property *prop = of_find_property(np, propname, NULL);
  436. int l = 0, i = 0;
  437. const char *p, *end;
  438. if (!prop)
  439. return -EINVAL;
  440. if (!prop->value)
  441. return -ENODATA;
  442. p = prop->value;
  443. end = p + prop->length;
  444. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
  445. l = strnlen(p, end - p) + 1;
  446. if (p + l > end)
  447. return -EILSEQ;
  448. if (out_strs && i >= skip)
  449. *out_strs++ = p;
  450. }
  451. i -= skip;
  452. return i <= 0 ? -ENODATA : i;
  453. }
  454. EXPORT_SYMBOL_GPL(of_property_read_string_helper);
  455. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  456. u32 *pu)
  457. {
  458. const void *curv = cur;
  459. if (!prop)
  460. return NULL;
  461. if (!cur) {
  462. curv = prop->value;
  463. goto out_val;
  464. }
  465. curv += sizeof(*cur);
  466. if (curv >= prop->value + prop->length)
  467. return NULL;
  468. out_val:
  469. *pu = be32_to_cpup(curv);
  470. return curv;
  471. }
  472. EXPORT_SYMBOL_GPL(of_prop_next_u32);
  473. const char *of_prop_next_string(struct property *prop, const char *cur)
  474. {
  475. const void *curv = cur;
  476. if (!prop)
  477. return NULL;
  478. if (!cur)
  479. return prop->value;
  480. curv += strlen(cur) + 1;
  481. if (curv >= prop->value + prop->length)
  482. return NULL;
  483. return curv;
  484. }
  485. EXPORT_SYMBOL_GPL(of_prop_next_string);
  486. /**
  487. * of_graph_parse_endpoint() - parse common endpoint node properties
  488. * @node: pointer to endpoint device_node
  489. * @endpoint: pointer to the OF endpoint data structure
  490. *
  491. * The caller should hold a reference to @node.
  492. */
  493. int of_graph_parse_endpoint(const struct device_node *node,
  494. struct of_endpoint *endpoint)
  495. {
  496. struct device_node *port_node = of_get_parent(node);
  497. WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
  498. __func__, node);
  499. memset(endpoint, 0, sizeof(*endpoint));
  500. endpoint->local_node = node;
  501. /*
  502. * It doesn't matter whether the two calls below succeed.
  503. * If they don't then the default value 0 is used.
  504. */
  505. of_property_read_u32(port_node, "reg", &endpoint->port);
  506. of_property_read_u32(node, "reg", &endpoint->id);
  507. of_node_put(port_node);
  508. return 0;
  509. }
  510. EXPORT_SYMBOL(of_graph_parse_endpoint);
  511. /**
  512. * of_graph_get_port_by_id() - get the port matching a given id
  513. * @parent: pointer to the parent device node
  514. * @id: id of the port
  515. *
  516. * Return: A 'port' node pointer with refcount incremented. The caller
  517. * has to use of_node_put() on it when done.
  518. */
  519. struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
  520. {
  521. struct device_node *node, *port;
  522. node = of_get_child_by_name(parent, "ports");
  523. if (node)
  524. parent = node;
  525. for_each_child_of_node(parent, port) {
  526. u32 port_id = 0;
  527. if (!of_node_name_eq(port, "port"))
  528. continue;
  529. of_property_read_u32(port, "reg", &port_id);
  530. if (id == port_id)
  531. break;
  532. }
  533. of_node_put(node);
  534. return port;
  535. }
  536. EXPORT_SYMBOL(of_graph_get_port_by_id);
  537. /**
  538. * of_graph_get_next_endpoint() - get next endpoint node
  539. * @parent: pointer to the parent device node
  540. * @prev: previous endpoint node, or NULL to get first
  541. *
  542. * Return: An 'endpoint' node pointer with refcount incremented. Refcount
  543. * of the passed @prev node is decremented.
  544. */
  545. struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
  546. struct device_node *prev)
  547. {
  548. struct device_node *endpoint;
  549. struct device_node *port;
  550. if (!parent)
  551. return NULL;
  552. /*
  553. * Start by locating the port node. If no previous endpoint is specified
  554. * search for the first port node, otherwise get the previous endpoint
  555. * parent port node.
  556. */
  557. if (!prev) {
  558. struct device_node *node;
  559. node = of_get_child_by_name(parent, "ports");
  560. if (node)
  561. parent = node;
  562. port = of_get_child_by_name(parent, "port");
  563. of_node_put(node);
  564. if (!port) {
  565. pr_err("graph: no port node found in %pOF\n", parent);
  566. return NULL;
  567. }
  568. } else {
  569. port = of_get_parent(prev);
  570. if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
  571. __func__, prev))
  572. return NULL;
  573. }
  574. while (1) {
  575. /*
  576. * Now that we have a port node, get the next endpoint by
  577. * getting the next child. If the previous endpoint is NULL this
  578. * will return the first child.
  579. */
  580. endpoint = of_get_next_child(port, prev);
  581. if (endpoint) {
  582. of_node_put(port);
  583. return endpoint;
  584. }
  585. /* No more endpoints under this port, try the next one. */
  586. prev = NULL;
  587. do {
  588. port = of_get_next_child(parent, port);
  589. if (!port)
  590. return NULL;
  591. } while (!of_node_name_eq(port, "port"));
  592. }
  593. }
  594. EXPORT_SYMBOL(of_graph_get_next_endpoint);
  595. /**
  596. * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
  597. * @parent: pointer to the parent device node
  598. * @port_reg: identifier (value of reg property) of the parent port node
  599. * @reg: identifier (value of reg property) of the endpoint node
  600. *
  601. * Return: An 'endpoint' node pointer which is identified by reg and at the same
  602. * is the child of a port node identified by port_reg. reg and port_reg are
  603. * ignored when they are -1. Use of_node_put() on the pointer when done.
  604. */
  605. struct device_node *of_graph_get_endpoint_by_regs(
  606. const struct device_node *parent, int port_reg, int reg)
  607. {
  608. struct of_endpoint endpoint;
  609. struct device_node *node = NULL;
  610. for_each_endpoint_of_node(parent, node) {
  611. of_graph_parse_endpoint(node, &endpoint);
  612. if (((port_reg == -1) || (endpoint.port == port_reg)) &&
  613. ((reg == -1) || (endpoint.id == reg)))
  614. return node;
  615. }
  616. return NULL;
  617. }
  618. EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
  619. /**
  620. * of_graph_get_remote_endpoint() - get remote endpoint node
  621. * @node: pointer to a local endpoint device_node
  622. *
  623. * Return: Remote endpoint node associated with remote endpoint node linked
  624. * to @node. Use of_node_put() on it when done.
  625. */
  626. struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
  627. {
  628. /* Get remote endpoint node. */
  629. return of_parse_phandle(node, "remote-endpoint", 0);
  630. }
  631. EXPORT_SYMBOL(of_graph_get_remote_endpoint);
  632. /**
  633. * of_graph_get_port_parent() - get port's parent node
  634. * @node: pointer to a local endpoint device_node
  635. *
  636. * Return: device node associated with endpoint node linked
  637. * to @node. Use of_node_put() on it when done.
  638. */
  639. struct device_node *of_graph_get_port_parent(struct device_node *node)
  640. {
  641. unsigned int depth;
  642. if (!node)
  643. return NULL;
  644. /*
  645. * Preserve usecount for passed in node as of_get_next_parent()
  646. * will do of_node_put() on it.
  647. */
  648. of_node_get(node);
  649. /* Walk 3 levels up only if there is 'ports' node. */
  650. for (depth = 3; depth && node; depth--) {
  651. node = of_get_next_parent(node);
  652. if (depth == 2 && !of_node_name_eq(node, "ports"))
  653. break;
  654. }
  655. return node;
  656. }
  657. EXPORT_SYMBOL(of_graph_get_port_parent);
  658. /**
  659. * of_graph_get_remote_port_parent() - get remote port's parent node
  660. * @node: pointer to a local endpoint device_node
  661. *
  662. * Return: Remote device node associated with remote endpoint node linked
  663. * to @node. Use of_node_put() on it when done.
  664. */
  665. struct device_node *of_graph_get_remote_port_parent(
  666. const struct device_node *node)
  667. {
  668. struct device_node *np, *pp;
  669. /* Get remote endpoint node. */
  670. np = of_graph_get_remote_endpoint(node);
  671. pp = of_graph_get_port_parent(np);
  672. of_node_put(np);
  673. return pp;
  674. }
  675. EXPORT_SYMBOL(of_graph_get_remote_port_parent);
  676. /**
  677. * of_graph_get_remote_port() - get remote port node
  678. * @node: pointer to a local endpoint device_node
  679. *
  680. * Return: Remote port node associated with remote endpoint node linked
  681. * to @node. Use of_node_put() on it when done.
  682. */
  683. struct device_node *of_graph_get_remote_port(const struct device_node *node)
  684. {
  685. struct device_node *np;
  686. /* Get remote endpoint node. */
  687. np = of_graph_get_remote_endpoint(node);
  688. if (!np)
  689. return NULL;
  690. return of_get_next_parent(np);
  691. }
  692. EXPORT_SYMBOL(of_graph_get_remote_port);
  693. int of_graph_get_endpoint_count(const struct device_node *np)
  694. {
  695. struct device_node *endpoint;
  696. int num = 0;
  697. for_each_endpoint_of_node(np, endpoint)
  698. num++;
  699. return num;
  700. }
  701. EXPORT_SYMBOL(of_graph_get_endpoint_count);
  702. /**
  703. * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
  704. * @node: pointer to parent device_node containing graph port/endpoint
  705. * @port: identifier (value of reg property) of the parent port node
  706. * @endpoint: identifier (value of reg property) of the endpoint node
  707. *
  708. * Return: Remote device node associated with remote endpoint node linked
  709. * to @node. Use of_node_put() on it when done.
  710. */
  711. struct device_node *of_graph_get_remote_node(const struct device_node *node,
  712. u32 port, u32 endpoint)
  713. {
  714. struct device_node *endpoint_node, *remote;
  715. endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
  716. if (!endpoint_node) {
  717. pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
  718. port, endpoint, node);
  719. return NULL;
  720. }
  721. remote = of_graph_get_remote_port_parent(endpoint_node);
  722. of_node_put(endpoint_node);
  723. if (!remote) {
  724. pr_debug("no valid remote node\n");
  725. return NULL;
  726. }
  727. if (!of_device_is_available(remote)) {
  728. pr_debug("not available for remote node\n");
  729. of_node_put(remote);
  730. return NULL;
  731. }
  732. return remote;
  733. }
  734. EXPORT_SYMBOL(of_graph_get_remote_node);
  735. static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
  736. {
  737. return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
  738. }
  739. static void of_fwnode_put(struct fwnode_handle *fwnode)
  740. {
  741. of_node_put(to_of_node(fwnode));
  742. }
  743. static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
  744. {
  745. return of_device_is_available(to_of_node(fwnode));
  746. }
  747. static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
  748. const char *propname)
  749. {
  750. return of_property_read_bool(to_of_node(fwnode), propname);
  751. }
  752. static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
  753. const char *propname,
  754. unsigned int elem_size, void *val,
  755. size_t nval)
  756. {
  757. const struct device_node *node = to_of_node(fwnode);
  758. if (!val)
  759. return of_property_count_elems_of_size(node, propname,
  760. elem_size);
  761. switch (elem_size) {
  762. case sizeof(u8):
  763. return of_property_read_u8_array(node, propname, val, nval);
  764. case sizeof(u16):
  765. return of_property_read_u16_array(node, propname, val, nval);
  766. case sizeof(u32):
  767. return of_property_read_u32_array(node, propname, val, nval);
  768. case sizeof(u64):
  769. return of_property_read_u64_array(node, propname, val, nval);
  770. }
  771. return -ENXIO;
  772. }
  773. static int
  774. of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  775. const char *propname, const char **val,
  776. size_t nval)
  777. {
  778. const struct device_node *node = to_of_node(fwnode);
  779. return val ?
  780. of_property_read_string_array(node, propname, val, nval) :
  781. of_property_count_strings(node, propname);
  782. }
  783. static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
  784. {
  785. return kbasename(to_of_node(fwnode)->full_name);
  786. }
  787. static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
  788. {
  789. /* Root needs no prefix here (its name is "/"). */
  790. if (!to_of_node(fwnode)->parent)
  791. return "";
  792. return "/";
  793. }
  794. static struct fwnode_handle *
  795. of_fwnode_get_parent(const struct fwnode_handle *fwnode)
  796. {
  797. return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
  798. }
  799. static struct fwnode_handle *
  800. of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
  801. struct fwnode_handle *child)
  802. {
  803. return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
  804. to_of_node(child)));
  805. }
  806. static struct fwnode_handle *
  807. of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
  808. const char *childname)
  809. {
  810. const struct device_node *node = to_of_node(fwnode);
  811. struct device_node *child;
  812. for_each_available_child_of_node(node, child)
  813. if (of_node_name_eq(child, childname))
  814. return of_fwnode_handle(child);
  815. return NULL;
  816. }
  817. static int
  818. of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
  819. const char *prop, const char *nargs_prop,
  820. unsigned int nargs, unsigned int index,
  821. struct fwnode_reference_args *args)
  822. {
  823. struct of_phandle_args of_args;
  824. unsigned int i;
  825. int ret;
  826. if (nargs_prop)
  827. ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
  828. nargs_prop, index, &of_args);
  829. else
  830. ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
  831. nargs, index, &of_args);
  832. if (ret < 0)
  833. return ret;
  834. if (!args)
  835. return 0;
  836. args->nargs = of_args.args_count;
  837. args->fwnode = of_fwnode_handle(of_args.np);
  838. for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
  839. args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
  840. return 0;
  841. }
  842. static struct fwnode_handle *
  843. of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
  844. struct fwnode_handle *prev)
  845. {
  846. return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
  847. to_of_node(prev)));
  848. }
  849. static struct fwnode_handle *
  850. of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
  851. {
  852. return of_fwnode_handle(
  853. of_graph_get_remote_endpoint(to_of_node(fwnode)));
  854. }
  855. static struct fwnode_handle *
  856. of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
  857. {
  858. struct device_node *np;
  859. /* Get the parent of the port */
  860. np = of_get_parent(to_of_node(fwnode));
  861. if (!np)
  862. return NULL;
  863. /* Is this the "ports" node? If not, it's the port parent. */
  864. if (!of_node_name_eq(np, "ports"))
  865. return of_fwnode_handle(np);
  866. return of_fwnode_handle(of_get_next_parent(np));
  867. }
  868. static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  869. struct fwnode_endpoint *endpoint)
  870. {
  871. const struct device_node *node = to_of_node(fwnode);
  872. struct device_node *port_node = of_get_parent(node);
  873. endpoint->local_fwnode = fwnode;
  874. of_property_read_u32(port_node, "reg", &endpoint->port);
  875. of_property_read_u32(node, "reg", &endpoint->id);
  876. of_node_put(port_node);
  877. return 0;
  878. }
  879. static const void *
  880. of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
  881. const struct device *dev)
  882. {
  883. return of_device_get_match_data(dev);
  884. }
  885. static bool of_is_ancestor_of(struct device_node *test_ancestor,
  886. struct device_node *child)
  887. {
  888. of_node_get(child);
  889. while (child) {
  890. if (child == test_ancestor) {
  891. of_node_put(child);
  892. return true;
  893. }
  894. child = of_get_next_parent(child);
  895. }
  896. return false;
  897. }
  898. /**
  899. * of_link_to_phandle - Add fwnode link to supplier from supplier phandle
  900. * @con_np: consumer device tree node
  901. * @sup_np: supplier device tree node
  902. *
  903. * Given a phandle to a supplier device tree node (@sup_np), this function
  904. * finds the device that owns the supplier device tree node and creates a
  905. * device link from @dev consumer device to the supplier device. This function
  906. * doesn't create device links for invalid scenarios such as trying to create a
  907. * link with a parent device as the consumer of its child device. In such
  908. * cases, it returns an error.
  909. *
  910. * Returns:
  911. * - 0 if fwnode link successfully created to supplier
  912. * - -EINVAL if the supplier link is invalid and should not be created
  913. * - -ENODEV if struct device will never be create for supplier
  914. */
  915. static int of_link_to_phandle(struct device_node *con_np,
  916. struct device_node *sup_np)
  917. {
  918. struct device *sup_dev;
  919. struct device_node *tmp_np = sup_np;
  920. of_node_get(sup_np);
  921. /*
  922. * Find the device node that contains the supplier phandle. It may be
  923. * @sup_np or it may be an ancestor of @sup_np.
  924. */
  925. while (sup_np) {
  926. /* Don't allow linking to a disabled supplier */
  927. if (!of_device_is_available(sup_np)) {
  928. of_node_put(sup_np);
  929. sup_np = NULL;
  930. }
  931. if (of_find_property(sup_np, "compatible", NULL))
  932. break;
  933. sup_np = of_get_next_parent(sup_np);
  934. }
  935. if (!sup_np) {
  936. pr_debug("Not linking %pOFP to %pOFP - No device\n",
  937. con_np, tmp_np);
  938. return -ENODEV;
  939. }
  940. /*
  941. * Don't allow linking a device node as a consumer of one of its
  942. * descendant nodes. By definition, a child node can't be a functional
  943. * dependency for the parent node.
  944. */
  945. if (of_is_ancestor_of(con_np, sup_np)) {
  946. pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
  947. con_np, sup_np);
  948. of_node_put(sup_np);
  949. return -EINVAL;
  950. }
  951. /*
  952. * Don't create links to "early devices" that won't have struct devices
  953. * created for them.
  954. */
  955. sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
  956. if (!sup_dev &&
  957. (of_node_check_flag(sup_np, OF_POPULATED) ||
  958. sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) {
  959. pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
  960. con_np, sup_np);
  961. of_node_put(sup_np);
  962. return -ENODEV;
  963. }
  964. put_device(sup_dev);
  965. fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
  966. of_node_put(sup_np);
  967. return 0;
  968. }
  969. /**
  970. * parse_prop_cells - Property parsing function for suppliers
  971. *
  972. * @np: Pointer to device tree node containing a list
  973. * @prop_name: Name of property to be parsed. Expected to hold phandle values
  974. * @index: For properties holding a list of phandles, this is the index
  975. * into the list.
  976. * @list_name: Property name that is known to contain list of phandle(s) to
  977. * supplier(s)
  978. * @cells_name: property name that specifies phandles' arguments count
  979. *
  980. * This is a helper function to parse properties that have a known fixed name
  981. * and are a list of phandles and phandle arguments.
  982. *
  983. * Returns:
  984. * - phandle node pointer with refcount incremented. Caller must of_node_put()
  985. * on it when done.
  986. * - NULL if no phandle found at index
  987. */
  988. static struct device_node *parse_prop_cells(struct device_node *np,
  989. const char *prop_name, int index,
  990. const char *list_name,
  991. const char *cells_name)
  992. {
  993. struct of_phandle_args sup_args;
  994. if (strcmp(prop_name, list_name))
  995. return NULL;
  996. if (of_parse_phandle_with_args(np, list_name, cells_name, index,
  997. &sup_args))
  998. return NULL;
  999. return sup_args.np;
  1000. }
  1001. #define DEFINE_SIMPLE_PROP(fname, name, cells) \
  1002. static struct device_node *parse_##fname(struct device_node *np, \
  1003. const char *prop_name, int index) \
  1004. { \
  1005. return parse_prop_cells(np, prop_name, index, name, cells); \
  1006. }
  1007. static int strcmp_suffix(const char *str, const char *suffix)
  1008. {
  1009. unsigned int len, suffix_len;
  1010. len = strlen(str);
  1011. suffix_len = strlen(suffix);
  1012. if (len <= suffix_len)
  1013. return -1;
  1014. return strcmp(str + len - suffix_len, suffix);
  1015. }
  1016. /**
  1017. * parse_suffix_prop_cells - Suffix property parsing function for suppliers
  1018. *
  1019. * @np: Pointer to device tree node containing a list
  1020. * @prop_name: Name of property to be parsed. Expected to hold phandle values
  1021. * @index: For properties holding a list of phandles, this is the index
  1022. * into the list.
  1023. * @suffix: Property suffix that is known to contain list of phandle(s) to
  1024. * supplier(s)
  1025. * @cells_name: property name that specifies phandles' arguments count
  1026. *
  1027. * This is a helper function to parse properties that have a known fixed suffix
  1028. * and are a list of phandles and phandle arguments.
  1029. *
  1030. * Returns:
  1031. * - phandle node pointer with refcount incremented. Caller must of_node_put()
  1032. * on it when done.
  1033. * - NULL if no phandle found at index
  1034. */
  1035. static struct device_node *parse_suffix_prop_cells(struct device_node *np,
  1036. const char *prop_name, int index,
  1037. const char *suffix,
  1038. const char *cells_name)
  1039. {
  1040. struct of_phandle_args sup_args;
  1041. if (strcmp_suffix(prop_name, suffix))
  1042. return NULL;
  1043. if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
  1044. &sup_args))
  1045. return NULL;
  1046. return sup_args.np;
  1047. }
  1048. #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
  1049. static struct device_node *parse_##fname(struct device_node *np, \
  1050. const char *prop_name, int index) \
  1051. { \
  1052. return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
  1053. }
  1054. /**
  1055. * struct supplier_bindings - Property parsing functions for suppliers
  1056. *
  1057. * @parse_prop: function name
  1058. * parse_prop() finds the node corresponding to a supplier phandle
  1059. * @parse_prop.np: Pointer to device node holding supplier phandle property
  1060. * @parse_prop.prop_name: Name of property holding a phandle value
  1061. * @parse_prop.index: For properties holding a list of phandles, this is the
  1062. * index into the list
  1063. *
  1064. * Returns:
  1065. * parse_prop() return values are
  1066. * - phandle node pointer with refcount incremented. Caller must of_node_put()
  1067. * on it when done.
  1068. * - NULL if no phandle found at index
  1069. */
  1070. struct supplier_bindings {
  1071. struct device_node *(*parse_prop)(struct device_node *np,
  1072. const char *prop_name, int index);
  1073. bool optional;
  1074. };
  1075. DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
  1076. DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
  1077. DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
  1078. DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
  1079. DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
  1080. DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
  1081. DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
  1082. DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
  1083. DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
  1084. DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
  1085. DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
  1086. DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
  1087. DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
  1088. DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
  1089. DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
  1090. DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
  1091. DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
  1092. DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
  1093. DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
  1094. DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
  1095. DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
  1096. DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
  1097. DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
  1098. DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
  1099. static struct device_node *parse_gpios(struct device_node *np,
  1100. const char *prop_name, int index)
  1101. {
  1102. if (!strcmp_suffix(prop_name, ",nr-gpios"))
  1103. return NULL;
  1104. return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
  1105. "#gpio-cells");
  1106. }
  1107. static struct device_node *parse_iommu_maps(struct device_node *np,
  1108. const char *prop_name, int index)
  1109. {
  1110. if (strcmp(prop_name, "iommu-map"))
  1111. return NULL;
  1112. return of_parse_phandle(np, prop_name, (index * 4) + 1);
  1113. }
  1114. static struct device_node *parse_gpio_compat(struct device_node *np,
  1115. const char *prop_name, int index)
  1116. {
  1117. struct of_phandle_args sup_args;
  1118. if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
  1119. return NULL;
  1120. /*
  1121. * Ignore node with gpio-hog property since its gpios are all provided
  1122. * by its parent.
  1123. */
  1124. if (of_find_property(np, "gpio-hog", NULL))
  1125. return NULL;
  1126. if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
  1127. &sup_args))
  1128. return NULL;
  1129. return sup_args.np;
  1130. }
  1131. static struct device_node *parse_interrupts(struct device_node *np,
  1132. const char *prop_name, int index)
  1133. {
  1134. struct of_phandle_args sup_args;
  1135. if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
  1136. return NULL;
  1137. if (strcmp(prop_name, "interrupts") &&
  1138. strcmp(prop_name, "interrupts-extended"))
  1139. return NULL;
  1140. return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
  1141. }
  1142. static const struct supplier_bindings of_supplier_bindings[] = {
  1143. { .parse_prop = parse_clocks, },
  1144. { .parse_prop = parse_interconnects, },
  1145. { .parse_prop = parse_iommus, .optional = true, },
  1146. { .parse_prop = parse_iommu_maps, .optional = true, },
  1147. { .parse_prop = parse_mboxes, },
  1148. { .parse_prop = parse_io_channels, },
  1149. { .parse_prop = parse_interrupt_parent, },
  1150. { .parse_prop = parse_dmas, .optional = true, },
  1151. { .parse_prop = parse_power_domains, },
  1152. { .parse_prop = parse_hwlocks, },
  1153. { .parse_prop = parse_extcon, },
  1154. { .parse_prop = parse_nvmem_cells, },
  1155. { .parse_prop = parse_phys, },
  1156. { .parse_prop = parse_wakeup_parent, },
  1157. { .parse_prop = parse_pinctrl0, },
  1158. { .parse_prop = parse_pinctrl1, },
  1159. { .parse_prop = parse_pinctrl2, },
  1160. { .parse_prop = parse_pinctrl3, },
  1161. { .parse_prop = parse_pinctrl4, },
  1162. { .parse_prop = parse_pinctrl5, },
  1163. { .parse_prop = parse_pinctrl6, },
  1164. { .parse_prop = parse_pinctrl7, },
  1165. { .parse_prop = parse_pinctrl8, },
  1166. { .parse_prop = parse_gpio_compat, },
  1167. { .parse_prop = parse_interrupts, },
  1168. { .parse_prop = parse_regulators, },
  1169. { .parse_prop = parse_gpio, },
  1170. { .parse_prop = parse_gpios, },
  1171. {}
  1172. };
  1173. /**
  1174. * of_link_property - Create device links to suppliers listed in a property
  1175. * @dev: Consumer device
  1176. * @con_np: The consumer device tree node which contains the property
  1177. * @prop_name: Name of property to be parsed
  1178. *
  1179. * This function checks if the property @prop_name that is present in the
  1180. * @con_np device tree node is one of the known common device tree bindings
  1181. * that list phandles to suppliers. If @prop_name isn't one, this function
  1182. * doesn't do anything.
  1183. *
  1184. * If @prop_name is one, this function attempts to create fwnode links from the
  1185. * consumer device tree node @con_np to all the suppliers device tree nodes
  1186. * listed in @prop_name.
  1187. *
  1188. * Any failed attempt to create a fwnode link will NOT result in an immediate
  1189. * return. of_link_property() must create links to all the available supplier
  1190. * device tree nodes even when attempts to create a link to one or more
  1191. * suppliers fail.
  1192. */
  1193. static int of_link_property(struct device_node *con_np, const char *prop_name)
  1194. {
  1195. struct device_node *phandle;
  1196. const struct supplier_bindings *s = of_supplier_bindings;
  1197. unsigned int i = 0;
  1198. bool matched = false;
  1199. int ret = 0;
  1200. /* Do not stop at first failed link, link all available suppliers. */
  1201. while (!matched && s->parse_prop) {
  1202. if (s->optional && !fw_devlink_is_strict()) {
  1203. s++;
  1204. continue;
  1205. }
  1206. while ((phandle = s->parse_prop(con_np, prop_name, i))) {
  1207. matched = true;
  1208. i++;
  1209. of_link_to_phandle(con_np, phandle);
  1210. of_node_put(phandle);
  1211. }
  1212. s++;
  1213. }
  1214. return ret;
  1215. }
  1216. static int of_fwnode_add_links(struct fwnode_handle *fwnode)
  1217. {
  1218. struct property *p;
  1219. struct device_node *con_np = to_of_node(fwnode);
  1220. if (!con_np)
  1221. return -EINVAL;
  1222. for_each_property_of_node(con_np, p)
  1223. of_link_property(con_np, p->name);
  1224. return 0;
  1225. }
  1226. const struct fwnode_operations of_fwnode_ops = {
  1227. .get = of_fwnode_get,
  1228. .put = of_fwnode_put,
  1229. .device_is_available = of_fwnode_device_is_available,
  1230. .device_get_match_data = of_fwnode_device_get_match_data,
  1231. .property_present = of_fwnode_property_present,
  1232. .property_read_int_array = of_fwnode_property_read_int_array,
  1233. .property_read_string_array = of_fwnode_property_read_string_array,
  1234. .get_name = of_fwnode_get_name,
  1235. .get_name_prefix = of_fwnode_get_name_prefix,
  1236. .get_parent = of_fwnode_get_parent,
  1237. .get_next_child_node = of_fwnode_get_next_child_node,
  1238. .get_named_child_node = of_fwnode_get_named_child_node,
  1239. .get_reference_args = of_fwnode_get_reference_args,
  1240. .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
  1241. .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
  1242. .graph_get_port_parent = of_fwnode_graph_get_port_parent,
  1243. .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
  1244. .add_links = of_fwnode_add_links,
  1245. };
  1246. EXPORT_SYMBOL_GPL(of_fwnode_ops);