property.c 42 KB

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