property.c 44 KB

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